CloudFormation 给出 "Invalid template property or properties" 错误

CloudFormation gives "Invalid template property or properties" error

我从以下 cloudformation 模板中得到 "Invalid template property or properties [TestLambda]"。我已经使用在线 json 验证器验证了 json。我试过一个一个地删除属性,但仍然出现错误。错误信息对诊断问题毫无用处。

谁能看出是什么问题?

谢谢。

{
  "Parameters": {
    "DeploymentBucket": {
      "Type": "String",
      "Description": "S3 bucket name where built artifacts are deployed"
    },
    "ProjectVersion": {
      "Type": "String",
      "Description": "Project Version"
    },
    "DeploymentTime": {
      "Type": "String",
      "Description": "It is a timestamp value which shows the deployment time. Used to rotate sources."
    },
    "DomainName": {
      "Type": "String",
      "Description": "Domain Name to serve the application"
    },
    "CloudSearchDomain": {
      "Type": "String",
      "Description": "Endpoint Name for CloudSearch domain"
    }
  },
  "Resources": {
    "LambdaExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "Path": "/",
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com",
                  "apigateway.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "ManagedPolicyArns": [
          "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
        ]
      }
    },
    "LambdaCustomPolicy": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "LambdaCustomPolicy",
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "s3:ListBuckets"
              ],
              "Resource": "*"
            }
          ]
        },
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ]
      }
    }
  },
  "TestLambda": {
    "Type": "AWS::Lambda::Function",
    "Properties": {
      "Handler": "com.serverlessbook.lambda.test.Handler",
      "Runtime": "java8",
      "Timeout": "300",
      "MemorySize": "1024",
      "Description": "Test lambda",
      "Role": {
        "Fn::GetAtt": [
          "LambdaExecutionRole",
          "Arn"
        ]
      },
      "Code": {
        "S3Bucket": {
          "Ref": "DeploymentBucket"
        },
        "S3Key": {
          "Fn::Sub": "artifacts/lambda-test/${ProjectVersion}/${DeploymentTime}.jar"
        }
      }
    }
  }
}

TestLambda 资源实际上在 resources JSON 对象之外。因此,它在具有意外属性的 AWS 端未通过 JSON 验证。

TestLambda 移动到 resources 内将解决问题。

{
  "Parameters": {
    "DeploymentBucket": {
      "Type": "String",
      "Description": "S3 bucket name where built artifacts are deployed"
    },
    "ProjectVersion": {
      "Type": "String",
      "Description": "Project Version"
    },
    "DeploymentTime": {
      "Type": "String",
      "Description": "It is a timestamp value which shows the deployment time. Used to rotate sources."
    },
    "DomainName": {
      "Type": "String",
      "Description": "Domain Name to serve the application"
    },
    "CloudSearchDomain": {
      "Type": "String",
      "Description": "Endpoint Name for CloudSearch domain"
    }
  },
  "Resources": {
    "LambdaExecutionRole": {
      "Type": "AWS::IAM::Role",
      "Properties": {
        "Path": "/",
        "AssumeRolePolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Principal": {
                "Service": [
                  "lambda.amazonaws.com",
                  "apigateway.amazonaws.com"
                ]
              },
              "Action": [
                "sts:AssumeRole"
              ]
            }
          ]
        },
        "ManagedPolicyArns": [
          "arn:aws:iam::aws:policy/service-role/AWSLambdaVPCAccessExecutionRole"
        ]
      }
    },
    "LambdaCustomPolicy": {
      "Type": "AWS::IAM::Policy",
      "Properties": {
        "PolicyName": "LambdaCustomPolicy",
        "PolicyDocument": {
          "Version": "2012-10-17",
          "Statement": [
            {
              "Effect": "Allow",
              "Action": [
                "s3:ListBuckets"
              ],
              "Resource": "*"
            }
          ]
        },
        "Roles": [
          {
            "Ref": "LambdaExecutionRole"
          }
        ]
      }
    },
    "TestLambda": {
      "Type": "AWS::Lambda::Function",
      "Properties": {
        "Handler": "com.serverlessbook.lambda.test.Handler",
        "Runtime": "java8",
        "Timeout": "300",
        "MemorySize": "1024",
        "Description": "Test lambda",
        "Role": {
          "Fn::GetAtt": [
            "LambdaExecutionRole",
            "Arn"
          ]
        },
        "Code": {
          "S3Bucket": {
            "Ref": "DeploymentBucket"
          },
          "S3Key": {
            "Fn::Sub": "artifacts/lambda-test/${ProjectVersion}/${DeploymentTime}.jar"
          }
        }
      }
    }
  }
}

希望这对您有所帮助。