Dean Schulze
Dean Schulze

Reputation: 10303

CloudFormation gives "Invalid template property or properties" error

I get "Invalid template property or properties [TestLambda]" from the following cloudformation template. I've validated the json with an online json validator. I've tried removing the properties one by one, but still get the error. The error message is useless in diagnosing the problem.

Can anyone see what the problem is?

Thanks.

{
  "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"
        }
      }
    }
  }
}

Upvotes: 1

Views: 3791

Answers (1)

krisnik
krisnik

Reputation: 1436

the TestLambda resource is actually outside the resources JSON object. So, it failed the JSON validation at the AWS End with unexpected attributes.

Moving TestLambda inside resources will resolve the issue.

{
  "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"
          }
        }
      }
    }
  }
}

Hope this helps.

Upvotes: 2

Related Questions