How to use create_launch_template method in localstack

Best Python code snippet using localstack_python

test_launch_templates.py

Source:test_launch_templates.py Github

copy

Full Screen

...5from moto import mock_ec26@mock_ec27def test_launch_template_create():8 cli = boto3.client("ec2", region_name="us-east-1")9 resp = cli.create_launch_template(10 LaunchTemplateName="test-template",11 # the absolute minimum needed to create a template without other resources12 LaunchTemplateData={13 "TagSpecifications": [{14 "ResourceType": "instance",15 "Tags": [{16 "Key": "test",17 "Value": "value",18 }],19 }],20 },21 )22 resp.should.have.key("LaunchTemplate")23 lt = resp["LaunchTemplate"]24 lt["LaunchTemplateName"].should.equal("test-template")25 lt["DefaultVersionNumber"].should.equal(1)26 lt["LatestVersionNumber"].should.equal(1)27 with assert_raises(ClientError) as ex:28 cli.create_launch_template(29 LaunchTemplateName="test-template",30 LaunchTemplateData={31 "TagSpecifications": [{32 "ResourceType": "instance",33 "Tags": [{34 "Key": "test",35 "Value": "value",36 }],37 }],38 },39 )40 str(ex.exception).should.equal(41 'An error occurred (InvalidLaunchTemplateName.AlreadyExistsException) when calling the CreateLaunchTemplate operation: Launch template name already in use.')42@mock_ec243def test_describe_launch_template_versions():44 template_data = {45 "ImageId": "ami-abc123",46 "DisableApiTermination": False,47 "TagSpecifications": [{48 "ResourceType": "instance",49 "Tags": [{50 "Key": "test",51 "Value": "value",52 }],53 }],54 "SecurityGroupIds": [55 "sg-1234",56 "sg-ab5678",57 ],58 }59 cli = boto3.client("ec2", region_name="us-east-1")60 create_resp = cli.create_launch_template(61 LaunchTemplateName="test-template",62 LaunchTemplateData=template_data)63 # test using name64 resp = cli.describe_launch_template_versions(65 LaunchTemplateName="test-template",66 Versions=['1'])67 templ = resp["LaunchTemplateVersions"][0]["LaunchTemplateData"]68 templ.should.equal(template_data)69 # test using id70 resp = cli.describe_launch_template_versions(71 LaunchTemplateId=create_resp["LaunchTemplate"]["LaunchTemplateId"],72 Versions=['1'])73 templ = resp["LaunchTemplateVersions"][0]["LaunchTemplateData"]74 templ.should.equal(template_data)75@mock_ec276def test_create_launch_template_version():77 cli = boto3.client("ec2", region_name="us-east-1")78 create_resp = cli.create_launch_template(79 LaunchTemplateName="test-template",80 LaunchTemplateData={81 "ImageId": "ami-abc123"82 })83 version_resp = cli.create_launch_template_version(84 LaunchTemplateName="test-template",85 LaunchTemplateData={86 "ImageId": "ami-def456"87 },88 VersionDescription="new ami")89 version_resp.should.have.key("LaunchTemplateVersion")90 version = version_resp["LaunchTemplateVersion"]91 version["DefaultVersion"].should.equal(False)92 version["LaunchTemplateId"].should.equal(create_resp["LaunchTemplate"]["LaunchTemplateId"])93 version["VersionDescription"].should.equal("new ami")94 version["VersionNumber"].should.equal(2)95@mock_ec296def test_create_launch_template_version_by_id():97 cli = boto3.client("ec2", region_name="us-east-1")98 create_resp = cli.create_launch_template(99 LaunchTemplateName="test-template",100 LaunchTemplateData={101 "ImageId": "ami-abc123"102 })103 version_resp = cli.create_launch_template_version(104 LaunchTemplateId=create_resp["LaunchTemplate"]["LaunchTemplateId"],105 LaunchTemplateData={106 "ImageId": "ami-def456"107 },108 VersionDescription="new ami")109 version_resp.should.have.key("LaunchTemplateVersion")110 version = version_resp["LaunchTemplateVersion"]111 version["DefaultVersion"].should.equal(False)112 version["LaunchTemplateId"].should.equal(create_resp["LaunchTemplate"]["LaunchTemplateId"])113 version["VersionDescription"].should.equal("new ami")114 version["VersionNumber"].should.equal(2)115@mock_ec2116def test_describe_launch_template_versions_with_multiple_versions():117 cli = boto3.client("ec2", region_name="us-east-1")118 cli.create_launch_template(119 LaunchTemplateName="test-template",120 LaunchTemplateData={121 "ImageId": "ami-abc123"122 })123 cli.create_launch_template_version(124 LaunchTemplateName="test-template",125 LaunchTemplateData={126 "ImageId": "ami-def456"127 },128 VersionDescription="new ami")129 resp = cli.describe_launch_template_versions(130 LaunchTemplateName="test-template")131 resp["LaunchTemplateVersions"].should.have.length_of(2)132 resp["LaunchTemplateVersions"][0]["LaunchTemplateData"]["ImageId"].should.equal("ami-abc123")133 resp["LaunchTemplateVersions"][1]["LaunchTemplateData"]["ImageId"].should.equal("ami-def456")134@mock_ec2135def test_describe_launch_template_versions_with_versions_option():136 cli = boto3.client("ec2", region_name="us-east-1")137 cli.create_launch_template(138 LaunchTemplateName="test-template",139 LaunchTemplateData={140 "ImageId": "ami-abc123"141 })142 cli.create_launch_template_version(143 LaunchTemplateName="test-template",144 LaunchTemplateData={145 "ImageId": "ami-def456"146 },147 VersionDescription="new ami")148 cli.create_launch_template_version(149 LaunchTemplateName="test-template",150 LaunchTemplateData={151 "ImageId": "ami-hij789"152 },153 VersionDescription="new ami, again")154 resp = cli.describe_launch_template_versions(155 LaunchTemplateName="test-template",156 Versions=["2", "3"])157 resp["LaunchTemplateVersions"].should.have.length_of(2)158 resp["LaunchTemplateVersions"][0]["LaunchTemplateData"]["ImageId"].should.equal("ami-def456")159 resp["LaunchTemplateVersions"][1]["LaunchTemplateData"]["ImageId"].should.equal("ami-hij789")160@mock_ec2161def test_describe_launch_template_versions_with_min():162 cli = boto3.client("ec2", region_name="us-east-1")163 cli.create_launch_template(164 LaunchTemplateName="test-template",165 LaunchTemplateData={166 "ImageId": "ami-abc123"167 })168 cli.create_launch_template_version(169 LaunchTemplateName="test-template",170 LaunchTemplateData={171 "ImageId": "ami-def456"172 },173 VersionDescription="new ami")174 cli.create_launch_template_version(175 LaunchTemplateName="test-template",176 LaunchTemplateData={177 "ImageId": "ami-hij789"178 },179 VersionDescription="new ami, again")180 resp = cli.describe_launch_template_versions(181 LaunchTemplateName="test-template",182 MinVersion="2")183 resp["LaunchTemplateVersions"].should.have.length_of(2)184 resp["LaunchTemplateVersions"][0]["LaunchTemplateData"]["ImageId"].should.equal("ami-def456")185 resp["LaunchTemplateVersions"][1]["LaunchTemplateData"]["ImageId"].should.equal("ami-hij789")186@mock_ec2187def test_describe_launch_template_versions_with_max():188 cli = boto3.client("ec2", region_name="us-east-1")189 cli.create_launch_template(190 LaunchTemplateName="test-template",191 LaunchTemplateData={192 "ImageId": "ami-abc123"193 })194 cli.create_launch_template_version(195 LaunchTemplateName="test-template",196 LaunchTemplateData={197 "ImageId": "ami-def456"198 },199 VersionDescription="new ami")200 cli.create_launch_template_version(201 LaunchTemplateName="test-template",202 LaunchTemplateData={203 "ImageId": "ami-hij789"204 },205 VersionDescription="new ami, again")206 resp = cli.describe_launch_template_versions(207 LaunchTemplateName="test-template",208 MaxVersion="2")209 resp["LaunchTemplateVersions"].should.have.length_of(2)210 resp["LaunchTemplateVersions"][0]["LaunchTemplateData"]["ImageId"].should.equal("ami-abc123")211 resp["LaunchTemplateVersions"][1]["LaunchTemplateData"]["ImageId"].should.equal("ami-def456")212@mock_ec2213def test_describe_launch_template_versions_with_min_and_max():214 cli = boto3.client("ec2", region_name="us-east-1")215 cli.create_launch_template(216 LaunchTemplateName="test-template",217 LaunchTemplateData={218 "ImageId": "ami-abc123"219 })220 cli.create_launch_template_version(221 LaunchTemplateName="test-template",222 LaunchTemplateData={223 "ImageId": "ami-def456"224 },225 VersionDescription="new ami")226 cli.create_launch_template_version(227 LaunchTemplateName="test-template",228 LaunchTemplateData={229 "ImageId": "ami-hij789"230 },231 VersionDescription="new ami, again")232 cli.create_launch_template_version(233 LaunchTemplateName="test-template",234 LaunchTemplateData={235 "ImageId": "ami-345abc"236 },237 VersionDescription="new ami, because why not")238 resp = cli.describe_launch_template_versions(239 LaunchTemplateName="test-template",240 MinVersion="2",241 MaxVersion="3")242 resp["LaunchTemplateVersions"].should.have.length_of(2)243 resp["LaunchTemplateVersions"][0]["LaunchTemplateData"]["ImageId"].should.equal("ami-def456")244 resp["LaunchTemplateVersions"][1]["LaunchTemplateData"]["ImageId"].should.equal("ami-hij789")245@mock_ec2246def test_describe_launch_templates():247 cli = boto3.client("ec2", region_name="us-east-1")248 lt_ids = []249 r = cli.create_launch_template(250 LaunchTemplateName="test-template",251 LaunchTemplateData={252 "ImageId": "ami-abc123"253 })254 lt_ids.append(r["LaunchTemplate"]["LaunchTemplateId"])255 r = cli.create_launch_template(256 LaunchTemplateName="test-template2",257 LaunchTemplateData={258 "ImageId": "ami-abc123"259 })260 lt_ids.append(r["LaunchTemplate"]["LaunchTemplateId"])261 # general call, all templates262 resp = cli.describe_launch_templates()263 resp.should.have.key("LaunchTemplates")264 resp["LaunchTemplates"].should.have.length_of(2)265 resp["LaunchTemplates"][0]["LaunchTemplateName"].should.equal("test-template")266 resp["LaunchTemplates"][1]["LaunchTemplateName"].should.equal("test-template2")267 # filter by names268 resp = cli.describe_launch_templates(269 LaunchTemplateNames=["test-template2", "test-template"])270 resp.should.have.key("LaunchTemplates")271 resp["LaunchTemplates"].should.have.length_of(2)272 resp["LaunchTemplates"][0]["LaunchTemplateName"].should.equal("test-template2")273 resp["LaunchTemplates"][1]["LaunchTemplateName"].should.equal("test-template")274 # filter by ids275 resp = cli.describe_launch_templates(LaunchTemplateIds=lt_ids)276 resp.should.have.key("LaunchTemplates")277 resp["LaunchTemplates"].should.have.length_of(2)278 resp["LaunchTemplates"][0]["LaunchTemplateName"].should.equal("test-template")279 resp["LaunchTemplates"][1]["LaunchTemplateName"].should.equal("test-template2")280@mock_ec2281def test_describe_launch_templates_with_filters():282 cli = boto3.client("ec2", region_name="us-east-1")283 r = cli.create_launch_template(284 LaunchTemplateName="test-template",285 LaunchTemplateData={286 "ImageId": "ami-abc123"287 })288 cli.create_tags(289 Resources=[r["LaunchTemplate"]["LaunchTemplateId"]],290 Tags=[291 {"Key": "tag1", "Value": "a value"},292 {"Key": "another-key", "Value": "this value"},293 ])294 cli.create_launch_template(295 LaunchTemplateName="no-tags",296 LaunchTemplateData={297 "ImageId": "ami-abc123"298 })299 resp = cli.describe_launch_templates(Filters=[{300 "Name": "tag:tag1", "Values": ["a value"]301 }])302 resp["LaunchTemplates"].should.have.length_of(1)303 resp["LaunchTemplates"][0]["LaunchTemplateName"].should.equal("test-template")304 resp = cli.describe_launch_templates(Filters=[{305 "Name": "launch-template-name", "Values": ["no-tags"]306 }])307 resp["LaunchTemplates"].should.have.length_of(1)308 resp["LaunchTemplates"][0]["LaunchTemplateName"].should.equal("no-tags")309@mock_ec2310def test_create_launch_template_with_tag_spec():311 cli = boto3.client("ec2", region_name="us-east-1")312 cli.create_launch_template(313 LaunchTemplateName="test-template",314 LaunchTemplateData={"ImageId": "ami-abc123"},315 TagSpecifications=[{316 "ResourceType": "instance",317 "Tags": [318 {"Key": "key", "Value": "value"}319 ]320 }],321 )322 resp = cli.describe_launch_template_versions(323 LaunchTemplateName="test-template",324 Versions=["1"])325 version = resp["LaunchTemplateVersions"][0]326 version["LaunchTemplateData"].should.have.key("TagSpecifications")...

Full Screen

Full Screen

create_launch_template.py

Source:create_launch_template.py Github

copy

Full Screen

...9import sys10import base6411from prod_build_config import aws_profile, inst_profiles, EC2_instance, sec_groups12from aws_cred_objects import AWS_CREDS13def create_launch_template(user_data_file, template_name=None):14 aws = AWS_CREDS(aws_profile)15 ec2_inst = EC2_instance()16 """ These are all the groups """17 """ Needs to be different for FlyWheel """18 if template_name:19 fly_id = aws.ec2_res.meta.client.describe_security_groups(20 Filters=[{"Name": "group-name", "Values": ["FlyWheel"]}]21 )["SecurityGroups"][0]["GroupId"]22 ssh_id = aws.ec2_res.meta.client.describe_security_groups(23 Filters=[{"Name": "group-name", "Values": ["SSH"]}]24 )["SecurityGroups"][0]["GroupId"]25 if template_name == "FlyWheel":26 sec_group_ids = [fly_id, ssh_id]27 else:28 sec_group_ids = [29 x["GroupId"]30 for x in aws.ec2_res.meta.client.describe_security_groups()[31 "SecurityGroups"32 ]33 if x["GroupId"] != fly_id34 ]35 try:36 user_data = open(user_data_file, "r").read().encode("utf-8")37 except OSError as e:38 print("Try another file: ", e)39 sys.exit(1)40 encoded_user_data = base64.b64encode(user_data)41 str_encoded_user_data = encoded_user_data.decode("ascii")42 INSTANCE_PROFILE = inst_profiles[0]43 AMI = ec2_inst.ami44 LAUNCH_TEMPLATE = template_name or ec2_inst.lt_name45 INSTANCE_TYPE = ec2_inst.type46 VPCID = aws.ec2_res.meta.client.describe_vpcs()["Vpcs"][0]["VpcId"]47 KEYNAME = f"{VPCID}-{aws_profile}.pem"48 return aws.ec2_res.meta.client.create_launch_template(49 LaunchTemplateName=LAUNCH_TEMPLATE,50 LaunchTemplateData={51 "EbsOptimized": False,52 "IamInstanceProfile": {"Name": INSTANCE_PROFILE},53 "ImageId": AMI,54 "InstanceType": INSTANCE_TYPE,55 "KeyName": KEYNAME,56 "Monitoring": {"Enabled": True},57 "SecurityGroupIds": sec_group_ids,58 "UserData": str_encoded_user_data,59 },60 )61if __name__ == "__main__":62 # template_name = "HTTP"63 # user_data_file="../../../DockerStocksWeb/data/user_data.http.AWS.sh"64 template_name = "Crawler-flywheel"65 user_data_file = "../../../DockerStocksWeb/data/user_data.crawler.flywheel.AWS.sh"66 print(create_launch_template(user_data_file, template_name))67 template_name = "FlyWheel"68 user_data_file = "../../../DockerStocksWeb/data/user_data.flywheel.sh"69 print(create_launch_template(user_data_file, template_name))70 template_name = "Crawler-all-date"71 user_data_file = "../../../DockerStocksWeb/data/user_data.crawler.all.date.AWS.sh"...

Full Screen

Full Screen

mount-efs-template.py

Source:mount-efs-template.py Github

copy

Full Screen

1#!python2import boto33import base644def create_launch_template(aws_region, template_name, mime_file):5 with open(mime_file, "rb") as f:6 read_data = f.read()7 user_data = base64.b64encode(read_data)8 print(read_data)9 print(user_data)10 launch_template_json = {11 "LaunchTemplateName": template_name,12 "LaunchTemplateData": {13 "UserData": str(user_data)[2:-1]14 }15 } 16 ## REMEMBER TO CHANGE THIS IF YOU HAVE MORE THAN ONE PROFILE##17 ec2_client = boto3.client('ec2', region_name=aws_region)18 #ec2_client.create_launch_template(**launch_template_json)19if __name__ == "__main__":...

Full Screen

Full Screen

Automation Testing Tutorials

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.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run localstack automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful