You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在AWS CloudFormation中使用启动模板最新版本的问题

问题:CloudFormation创建Auto Scaling Group时,如何引用外部传入的Launch Template最新版本?

我正在编写CloudFormation模板来创建Auto Scaling Group,已提前准备好Launch Template,希望在ASG中使用它的最新版本。我将LaunchTemplateId作为参数传入模板,但不确定如何正确调用最新版本。当前模板相关内容如下:

--- 
AWSTemplateFormatVersion: 2010-09-09
Description: Create Auto Scaling Group with 2 min, 2 desired, 4 max
Parameters:
...
  TemplateID: 
    Description: Launch Template for ASG EC2s 
    Type: String
    Default: lt-xxxxxxxxxxxxxxxx
Resources:  
  ASG:
    Type: AWS::AutoScaling::AutoScalingGroup
    Properties:
...
      LaunchTemplate:
        LaunchTemplateId: !Ref TemplateID
        Version: !GetAtt 
          - !Ref TemplateID
          - LatestVersionNumber
...

使用taskcat测试时,始终出现以下linting错误:

[ERROR ] : Linting detected issues in: /Users/zaidafaneh/Desktop/RealBlocks/repos/cloudformation-temaplates/templates/saas/asg.yml

[ERROR ] : line 23 [1010] [GetAtt validation of parameters] Invalid GetAtt TemplateID.LatestVersionNumber for resource ASG

我考虑过用Lambda自定义资源作为替代方案,但觉得过于复杂。想知道是否可以通过CloudFormation原生方式解决该问题?

解决方案

可以直接使用CloudFormation原生的特殊版本标识符$Latest来指定使用Launch Template的最新版本,无需使用!GetAtt——因为!GetAtt仅能获取当前模板内已定义资源的属性,而你是通过参数传入的外部Launch Template,无法用!GetAtt获取它的属性。

修改后的LaunchTemplate配置部分如下:

LaunchTemplate:
  LaunchTemplateId: !Ref TemplateID
  Version: "$Latest"

$Latest是AWS预定义的特殊值,会自动关联到目标Launch Template的最新版本,无论是手动创建还是通过其他方式更新的版本,ASG都会使用当前最新的那个。

这样修改后,taskcat的linting错误会消失,同时满足你使用最新版本的需求,完全不需要依赖Lambda自定义资源。

内容的提问来源于stack exchange,提问作者Zaid Afaneh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.19 17:30:56