CloudFormation创建Lambda栈时Role参数期望字符串却收到JSONObject错误如何解决
错误原因与修复方案
你遇到的两个验证错误分别来自两处配置错误:
1. S3Bucket字段配置错误
AWS::Lambda::Function的Code.S3Bucket配置项仅需要填写S3桶的名称,不需要填写完整ARN。你当前填写的arn:aws:s3:::flex-fit-lambda-functions-source不符合字段正则校验规则,去掉ARN前缀仅保留桶名flex-fit-lambda-functions-source即可解决该报错。
2. 内置函数语法错误
你使用的是JSON格式的CloudFormation模板,JSON模板不支持YAML格式专属的!Ref/!GetAtt这类带感叹号的快捷函数语法。你写的{"!Ref" : "LambdaExecutionRole"}会被CloudFormation识别为普通JSON对象而非函数调用,因此触发期望字符串、实际找到JSON对象的类型报错。
针对角色ARN的获取,你可以任选以下两种标准JSON写法中的一种:
- 使用标准Ref函数(IAM角色的Ref返回值本身就是角色ARN):
"Role": {"Ref": "LambdaExecutionRole"}
- 使用标准Fn::GetAtt函数获取ARN:
"Role": {"Fn::GetAtt": ["LambdaExecutionRole", "Arn"]}
修正后的完整模板
{ "AWSTemplateFormatVersion" : "2010-09-09", "Description" : "Stack to create the get-EC lambda", "Resources" : { "LambdaExecutionRole": { "Type": "AWS::IAM::Role", "Properties": { "AssumeRolePolicyDocument": { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Principal": {"Service": ["lambda.amazonaws.com"]}, "Action": ["sts:AssumeRole"] }] }, "Path": "/", "Policies": [{ "PolicyName": "root", "PolicyDocument": { "Version": "2012-10-17", "Statement": [{ "Effect": "Allow", "Action": ["logs:*"], "Resource": "arn:aws:logs:*:*:*" }] } }] } }, "GetECLambda" : { "Type" : "AWS::Lambda::Function", "Properties" : { "FunctionName": "get-ecs", "Role": {"Ref": "LambdaExecutionRole"}, "Runtime": "nodejs12.x", "Code": { "S3Bucket" : "flex-fit-lambda-functions-source", "S3Key": "get-ecs.zip" } } } } }
内容的提问来源于stack exchange,提问作者Ollie Pugh
相关产品推荐
相关产品推荐

