我想通过 AWS CloudFormation 创建 Amazon Elastic Compute Cloud (Amazon EC2) 实例。但我针对 RunInstances 的 AWS Identity and Access Management (IAM) 策略具有基于标签的限制。
简短描述
您可以使用启动模板通过 CloudFormation 来创建 Amazon EC2 实例。
AWS::EC2::Instance 资源的标签属性不会扩展到通过 CloudFormation 创建的卷中。如果与用户或角色关联的 IAM policy 对卷标签有限制,则您会收到以下错误:
"You are not authorized to perform this operation."(您没有执行此操作的权限。)
要通过 CloudFormation 将标签传递给 ec2:CreateVolume,您必须在 CloudFormation 模板中的 AWS::EC2::LaunchTemplate 资源中定义标签。
解决方法
1. 使用 IAM policy 要求的标签在堆栈中定义启动模板。例如:
RequiredTagsLaunchTemplate:
Type: 'AWS::EC2::LaunchTemplate'
Properties:
LaunchTemplateData:
TagSpecifications:
- ResourceType: volume
Tags:
- Key: Env
Value: Dev
2. 将启动模板附加到 EC2 实例资源。例如:
Instance:
Type: 'AWS::EC2::Instance'
Properties:
LaunchTemplate:
LaunchTemplateId: !Ref RequiredTagsLaunchTemplate
Version: 1
InstanceType: r4.xlarge
.
.
RequiredTagsLaunchTemplate:
Type: 'AWS::EC2::LaunchTemplate'
Properties:
LaunchTemplateData:
TagSpecifications:
- ResourceType: volume
Tags:
- Key: Env
Value: Dev
3. 确认您的启动模板具有所有必要的标签。
**重要提示:**您必须确认创建堆栈的角色或用户具有创建和使用启动模板的权限,而没有标记限制。您可以使用 aws:CalledVia 条件键来创建一个新语句,以使 CloudFormation API 调用免受标记要求的限制。