简体   繁体   English

在 Cloudformation 模板中引用动态角色名称

[英]Reference a dynamic role name in a Cloudformation template

In one Cloudformation template I create the following role:在一个 Cloudformation 模板中,我创建了以下角色:

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

In another Cloudformation template for an EC2 instance I am attempting to attach that role to my EC2 instance, however I am unsure how to reference a dynamic role name.在 EC2 实例的另一个 Cloudformation 模板中,我尝试将该角色附加到我的 EC2 实例,但是我不确定如何引用动态角色名称。

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

Can this be done?这可以做到吗?

When I attempt to validate the template I get an error:当我尝试验证模板时出现错误:

An error occurred (ValidationError) when calling the ValidateTemplate operation: Template format error: Unresolved resource dependencies [crm-${Environment}-register] in the Resources block of the template调用ValidateTemplate操作时发生错误(ValidationError): Template format error: Unresolved resource dependencies [crm-${Environment}-register] 在模板的Resources块中

Ref does not work across stacks. Ref不能跨堆栈工作。 Assuming you are using same account and region , instead you have to use Export and ImporValue functions.假设您使用相同的 account 和 region ,则必须使用ExportImporValue函数。

So in your first stack you would have:因此,在您的第一个堆栈中,您将拥有:

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

Outputs:

   MyCRMPiccoRole:
     Value: !Ref CRMPiccoRole
     Export:
        Name: !Sub 'crm-${Environment}-register'

In the second stack :第二个堆栈中:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM