【问题标题】:Reference a dynamic role name in a Cloudformation template在 Cloudformation 模板中引用动态角色名称
【发布时间】:2021-06-07 01:34:49
【问题描述】:

在一个 Cloudformation 模板中,我创建了以下角色:

  CRMPiccoRole:
    Type: 'AWS::IAM::Role'
    Properties:
      RoleName: !Sub 'crm-${Environment}-register'

在另一个 EC2 实例的 Cloudformation 模板中,我尝试将该角色附加到我的 EC2 实例,但是我不确定如何引用 动态 角色名称。

Resources:
  InstanceProfile:
    Type: 'AWS::IAM::InstanceProfile'
    Properties:
      Path: /
      Roles:
        - !Ref 'crm-${Environment}-register'

这个可以吗?

当我尝试验证模板时出现错误:

调用 ValidateTemplate 时发生错误 (ValidationError) 操作:模板格式错误:未解决的资源依赖关系 [crm-${Environment}-register] 在模板的 Resources 块中

【问题讨论】:

    标签: amazon-web-services amazon-ec2 amazon-cloudformation amazon-iam iaas


    【解决方案1】:

    Ref 不能跨堆栈工作。假设您使用的是相同的帐户和地区,则必须使用ExportImporValue 函数。

    因此,在您的第一个堆栈中,您将拥有:

      CRMPiccoRole:
        Type: 'AWS::IAM::Role'
        Properties:
          RoleName: !Sub 'crm-${Environment}-register'
    
    Outputs:
    
       MyCRMPiccoRole:
         Value: !Ref CRMPiccoRole
         Export:
            Name: !Sub 'crm-${Environment}-register'
    

    第二个堆栈

    Resources:
      InstanceProfile:
        Type: 'AWS::IAM::InstanceProfile'
        Properties:
          Path: /
          Roles:
            - Fn::ImportValue:
                !Sub 'crm-${Environment}-register'
    

    【讨论】:

      猜你喜欢
      • 2017-03-01
      • 2015-12-03
      • 2020-09-30
      • 2017-06-18
      • 2020-07-12
      • 2019-11-22
      • 2017-05-16
      • 2023-03-25
      • 1970-01-01
      相关资源
      最近更新 更多