Best Python code snippet using localstack_python
views.py
Source:views.py
...6import json7##########################################################8# AWS Resources9##########################################################10def get_boto3_session():11 if settings.USE_IAM_ROLE:12 session = boto3.session.Session(region_name='ap-northeast-2')13 else:14 session = boto3.session.Session(profile_name=settings.AWS_PROFILE, region_name='ap-northeast-2')15 return session16@ensure_csrf_cookie17def index(request):18 context = {}19 return render(request, "console/ec2_instances.html", context=context)20def describe_instances(request):21 ec2 = get_boto3_session().resource('ec2')22 response_data = []23 instances = ec2.instances.all()24 for instance in instances:25 if instance.tags is None:26 return None27 for tag in instance.tags:28 if tag['Key'] == 'Name':29 instance_name = tag['Value']30 response_data.append({31 "id": instance.id,32 "name": instance_name,33 "launch_time": instance.launch_time,34 "instance_type": instance.instance_type,35 "public_ip_address": instance.public_ip_address,36 "image": instance.image.id,37 "state": instance.state["Name"]38 })39 return JsonResponse({40 'message': '2000',41 'data': response_data,42 }, json_dumps_params={'ensure_ascii': True})43def start_instances(request):44 json_data = json.loads(request.body.decode("utf-8"))45 instance_ids = json_data['instance_ids']46 ec2 = get_boto3_session().resource('ec2')47 ec2.instances.filter(InstanceIds=instance_ids).start()48 response_data = []49 return JsonResponse({50 'message': '2000',51 'data': response_data,52 }, json_dumps_params={'ensure_ascii': True})53def stop_instances(request):54 json_data = json.loads(request.body.decode("utf-8"))55 instance_ids = json_data['instance_ids']56 ec2 = get_boto3_session().resource('ec2')57 ec2.instances.filter(InstanceIds=instance_ids).stop()58 response_data = []59 return JsonResponse({60 'message': '2000',61 'data': response_data,62 }, json_dumps_params={'ensure_ascii': True})63def reboot_instances(request):64 json_data = json.loads(request.body.decode("utf-8"))65 instance_ids = json_data['instance_ids']66 ec2 = get_boto3_session().resource('ec2')67 ec2.instances.filter(InstanceIds=instance_ids).reboot()68 response_data = []69 return JsonResponse({70 'message': '2000',71 'data': response_data,72 }, json_dumps_params={'ensure_ascii': True})73##########################################################74# Terraform75##########################################################76def tf_cicd_asg(request):77 context = {}78 return render(request, "console/tf_cicd_asg.html", context=context)79def tf_cicd_ec2(request):80 return render(request, "console/tf_cicd_ec2.html")81##########################################################82# Samples83##########################################################84def get_instances_by_tag_value(tag, value):85 ec2 = get_boto3_session().resource('ec2')86 instances = ec2.instances.filter(Filters=[{'Name': 'tag:' + tag, 'Values': [value]}])87 for instance in instances:88 print(89 "Id: {0}\nPlatform: {1}\nType: {2}\nPublic IPv4: {3}\nAMI: {4}\nState: {5}\n".format(90 instance.id, instance.platform, instance.instance_type, instance.public_ip_address, instance.image.id,91 instance.state92 )93 )94 return instances95def describe_instances_by_client(request):96 ec2 = get_boto3_session().client('ec2')97 response = ec2.describe_instances()98 instances = response['Reservations']99 return JsonResponse({100 'message': '2000',101 'items': instances,...
AWSClient.py
Source:AWSClient.py
...3class AWSClient(AWSEntity):4 """ Represents an aws client. """5 def __init__(self, client_name):6 super().__init__()7 session = self.get_boto3_session()8 self.client = session.client(client_name)9 def get_boto3_session(self):10 """11 Returns authenticated boto3 session.12 :return:13 """14 session = boto3.Session(aws_access_key_id=self.aws_access_key_id,15 aws_secret_access_key=self.aws_secret_access_key,16 region_name=self.aws_region)...
utils.py
Source:utils.py
...5import logging6logging.basicConfig(level=logging.DEBUG)7logger = logging.getLogger(__name__)8logger.setLevel(logging.INFO)9def get_boto3_session():10 """11 Function to create boto3 session object.12 """13 session = boto3.Session()14 return session15def get_s3_resource_object():16 """17 Function to create boto3 s3 resource object.18 """19 boto3_session = get_boto3_session()20 return boto3_session.resource('s3')21def replace_special_char(input_str):...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!