以下 IAM 强制策略附加到我担任 SSO 用户的 AWS IAM 角色。当我使用 AWS CLI 时,我可以创建安全组。但是,当我尝试使用具有相同标签名称/值的 CloudFormation 创建此安全组并使用相同的 AWS 配置文件运行时,出现错误
You are not authorized to perform this operation
。
我担任的角色附加了 IAM 执行政策:
{
"Effect": "Allow",
"Action": "ec2:CreateSecurityGroup",
"Resource": "arn:aws:ec2:*:*:security-group/*",
"Condition": {
"StringLike": {
"aws:RequestTag/Name": "*UserDefined*"
}
}
}
AWS CLI 脚本。安全组创建成功:
VPC_ID='vpc-12345678901234567'
AWS_PROFILE='my-aws-profile'
AWS_REGION='eu-west-1'
aws ec2 create-security-group \
--profile ${AWS_PROFILE} \
--region ${AWS_REGION} \
--description "For Testing" \
--group-name "my-test-security-group" \
--vpc-id ${VPC_ID} \
--tag-specifications 'ResourceType=security-group,Tags=[{Key=Name,Value=UserDefined-my-test-sg}]'
CloudFormation 模板使用相同的 AWS 配置文件创建相同的安全组,但失败:
AWSTemplateFormatVersion: '2010-09-09'
Parameters:
VPC:
Type: AWS::EC2::VPC::Id
Default: vpc-12345678901234567
Resources:
MySecurityGroup:
Type: AWS::EC2::SecurityGroup
Properties:
GroupName: my-test-security-group
GroupDescription: For Testing
VpcId: !Ref VPC
Tags:
- Key: Name
Value: UserDefined-my-test-sg
尝试使用上述模板部署 CloudFormation 堆栈时出现错误:
You are not authorized to perform this operation. User: arn:aws:sts:::assumed-role/AWSRstrictedSSO_Ops_7f991be0d78c1285/[email protected] is not authorized to perform: ec2:CreateSecurityGroup on resource: arn:aws:ec2:eu-west-1:12345678012:security-group/* because no identity-based policy allows the ec2:CreateSecurityGroup action.
使用 CloudFormation 时我缺少什么?
可能和Cloudformation底层流程有关
CLI 调用的操作是对 EC2 服务的一次调用,请求中包含标签。在这种情况下,验证 aws:RequestTag/Name 的条件返回 true,并且您可以创建 SG。
使用 Cloudformation,情况就不那么明显了,资源创建操作可能与资源标记操作是分开的。