You need to enable JavaScript to run this app. CloudFormation创建Lambda栈时Role参数期望字符串却收到JSONObject错误如何解决
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.11 16:27:02