CloudFormation: Update AWS::EC2::LaunchTemplate by physical ID rather than by name (#7149)

This commit is contained in:
Timothy Klopotoski 2023-12-21 05:33:42 -05:00 committed by GitHub
parent 3246b477c7
commit 9098554903
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 82 additions and 2 deletions

View File

@ -167,11 +167,10 @@ class LaunchTemplate(TaggedEC2Resource, CloudFormationModel):
properties = cloudformation_json["Properties"]
name = properties.get("LaunchTemplateName")
data = properties.get("LaunchTemplateData")
description = properties.get("VersionDescription")
launch_template = backend.get_launch_template_by_name(name)
launch_template = backend.get_launch_template(original_resource.id)
launch_template.create_version(data, description)

View File

@ -297,3 +297,84 @@ def test_two_launch_templates():
launch_templates["LaunchTemplates"][0]["LaunchTemplateName"]
!= launch_templates["LaunchTemplates"][1]["LaunchTemplateName"]
)
@mock_autoscaling
@mock_cloudformation
@mock_ec2
def test_launch_template_unnamed_update():
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 a LaunchTemplate",
"Resources": {
"LaunchTemplate": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": EXAMPLE_AMI_ID,
"InstanceType": "t3.small",
"UserData": "",
},
},
},
},
}
)
cf_client.create_stack(
StackName=stack_name,
TemplateBody=template_json,
Capabilities=["CAPABILITY_NAMED_IAM"],
OnFailure="DELETE",
)
launch_template_resource = cf_client.describe_stack_resources(
StackName=stack_name, LogicalResourceId="LaunchTemplate"
)["StackResources"][0]
launch_template_id = launch_template_resource["PhysicalResourceId"]
launch_template = ec2_client.describe_launch_templates(
LaunchTemplateIds=[launch_template_id]
)["LaunchTemplates"][0]
assert launch_template["LatestVersionNumber"] == 1
template_json = json.dumps(
{
"AWSTemplateFormatVersion": "2010-09-09",
"Description": "AWS CloudFormation Template to create a LaunchTemplate",
"Resources": {
"LaunchTemplate": {
"Type": "AWS::EC2::LaunchTemplate",
"Properties": {
"LaunchTemplateData": {
"ImageId": EXAMPLE_AMI_ID,
"InstanceType": "t3.medium",
"UserData": "",
},
},
},
},
}
)
cf_client.update_stack(
StackName=stack_name,
TemplateBody=template_json,
Capabilities=["CAPABILITY_NAMED_IAM"],
)
updated_launch_template_resource = cf_client.describe_stack_resources(
StackName=stack_name, LogicalResourceId="LaunchTemplate"
)["StackResources"][0]
updated_launch_template_id = updated_launch_template_resource["PhysicalResourceId"]
updated_launch_template = ec2_client.describe_launch_templates(
LaunchTemplateIds=[updated_launch_template_id]
)["LaunchTemplates"][0]
assert launch_template_id == updated_launch_template_id
assert updated_launch_template["LatestVersionNumber"] == 2