使用 IAM 策略创建堆栈时出现 InsufficientCapabilitiesException [CAPABILITY_NAMED_IAM]

InsufficientCapabilitiesException [CAPABILITY_NAMED_IAM] when creating a stack with IAM policies

当我 运行 为包含 IAM 策略的 cloudformation 模板创建堆栈时出现此错误。

aws cloudformation create-stack --stack-name iam-stack --template-body file://./iam.yml --capabilities CAPABILITY_IAM --profile dev

调用 CreateStack 操作时发生错误 (InsufficientCapabilitiesException):需要功能:[CAPABILITY_NAMED_IAM]

--capabilities更改为CAPABILITY_NAMED_IAM

If you have IAM resources with custom names, you must specify CAPABILITY_NAMED_IAM. If you don't specify this parameter, this action returns an InsufficientCapabilities error.

https://docs.aws.amazon.com/AWSCloudFormation/latest/APIReference/API_CreateStack.html

在我的例子中,我需要 CAPABILITY_IAMCAPABILITY_NAMED_IAM 能力来获得“AWS::IAM::Role”类型的资源。

https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/CloudFormation.html#createStack-property

根据 AWS 文档, 如果在云形成中指定角色名称,则必须指定 CAPABILITY_NAMED_IAM 值以确认模板的功能 Link

所以你的命令应该是

aws cloudformation create-stack --stack-name iam-stack --template-body file://./iam.yml --capabilities CAPABILITY_NAMED_IAM --profile dev

如果您使用 AWS CodePipeline 通过 CloudFormation 堆栈部署 EC2,则有一个名为“功能”的选项,您可以从中 select CAPABILITY_NAMED_IAM.