[英]How can I reference an existing role in my new CloudFormation template?
[英]Reference a dynamic role name in a Cloudformation template
在一个 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): Template format error: Unresolved resource dependencies [crm-${Environment}-register] 在模板的Resources块中
Ref
不能跨堆栈工作。 假设您使用相同的 account 和 region ,则必须使用Export和ImporValue函数。
因此,在您的第一个堆栈中,您将拥有:
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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.