引用IamInstanceProfile的Cloudformation LaunchTemplate无法创建

时间:2018-12-19 15:18:16

标签: amazon-cloudformation amazon-iam

我正在尝试在Cloudformation堆栈中创建一个 LaunchTemplate ,该模板引用 IamInstanceProfile 。这是代码-我省略了不相关的部分:

...
            Resources:
              ServerLaunchTemplate:
                Type: 'AWS::EC2::LaunchTemplate'
                Properties:
                  LaunchTemplateData:
                    InstanceType: !Ref InstanceType
                    SecurityGroups:
                      - !Ref SecGroup
                    IamInstanceProfile: !Ref ServerProfile
                    UserData:
        ...
              ServerProfile:
                Type: 'AWS::IAM::InstanceProfile'
                Properties:
                  Path: /
                  Roles:
                    - !Ref ServerRole
...

ServerProfile 成功创建。但是,当堆栈创建过程到达创建 ServerLaunchTemplate 的步骤时,它将失败并显示错误:

Property validation failure: [Value of property {/LaunchTemplateData/IamInstanceProfile} does not match type {Object}]

如果我省略对 IamInstanceProfile 的引用,则会成功创建 LaunchTemplate

根据documentation和一些示例,这应该可以工作...根据我理解的错误, LaunchTemplate InstanceType 字段需要引用对象,但是“ !Ref InstanceType ”返回资源ID。

我该如何解决?我该如何检索“ / LaunchTemplateData / IamInstanceProfile ”字段可能需要的对象?

谢谢

1 个答案:

答案 0 :(得分:3)

在文档中容易错过:IamInstanceProfile需要一个IamInstanceProfile Cloudformation对象,其中所引用的IamInstanceProfile的Arn是它的属性。

请参见https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-launchtemplatedata.html#cfn-ec2-launchtemplate-launchtemplatedata-iaminstanceprofilehttps://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-ec2-launchtemplate-iaminstanceprofile.html

这应该有效:

  PortalLaunchTemplate:
    Type: 'AWS::EC2::LaunchTemplate'
    Properties:
      LaunchTemplateName: !Sub ${InstanceName}-launch-template
      LaunchTemplateData:
        ImageId: !Ref AmiId
        ...
        IamInstanceProfile:
          Arn: !GetAtt InstanceProfile.Arn