EC2: launch templates created by CloudFormation have a generated name if not provided (#7135)

This commit is contained in:
Timothy Klopotoski 2023-12-18 16:31:12 -05:00 committed by GitHub
parent d44d05c4a1
commit 40891c686a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 60 additions and 0 deletions

View File

@ -13,6 +13,7 @@ from ..utils import (
convert_tag_spec,
generic_filter,
random_launch_template_id,
random_launch_template_name,
utc_date_and_time,
)
from .core import TaggedEC2Resource
@ -141,6 +142,9 @@ class LaunchTemplate(TaggedEC2Resource, CloudFormationModel):
properties.get("TagSpecifications", {}), tag_key="Tags"
)
if name is None:
name = random_launch_template_name()
launch_template = backend.create_launch_template(
name, description, data, tag_spec
)

View File

@ -233,6 +233,10 @@ def random_launch_template_id() -> str:
return random_id(prefix=EC2_RESOURCE_TO_PREFIX["launch-template"], size=17)
def random_launch_template_name() -> str:
return f"LaunchTemplate_{random_resource_id(size=12)}"
def random_iam_instance_profile_association_id() -> str:
return random_id(prefix=EC2_RESOURCE_TO_PREFIX["iam-instance-profile-association"])

View File

@ -245,3 +245,55 @@ def test_asg_with_default_launch_template_version():
== launch_template_name
)
assert autoscaling_group["LaunchTemplate"]["Version"] == "1"
@mock_autoscaling
@mock_cloudformation
@mock_ec2
def test_two_launch_templates():
cf_client = boto3.client("cloudformation", region_name="us-west-1")
ec2_client = boto3.client("ec2", region_name="us-west-1")
stack_name = str(uuid4())
template_json = json.dumps(
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Template to create two LaunchTemplate",
"Resources": {
"LaunchTemplate0": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": EXAMPLE_AMI_ID,
"InstanceType": "t3.small",
"UserData": "",
},
},
},
"LaunchTemplate1": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": EXAMPLE_AMI_ID,
"InstanceType": "t3.medium",
"UserData": "",
},
},
},
},
}
)
cf_client.create_stack(
StackName=stack_name,
TemplateBody=template_json,
Capabilities=["CAPABILITY_NAMED_IAM"],
OnFailure="DELETE",
)
launch_templates = ec2_client.describe_launch_templates()
assert (
launch_templates["LaunchTemplates"][0]["LaunchTemplateName"]
!= launch_templates["LaunchTemplates"][1]["LaunchTemplateName"]
)