Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing multiple parameters from external file to cloudformation template and using values with ref

I am getting following error when trying to create a cloudformation stack using below cli command.

aws cloudformation create-stack --stack-name subodh-local-stack --template-url s3URL/template.json --parameters s3URL/params.json

Error: awscli.argprocess.ParamError: Error parsing parameter '--parameters': Unable to retrieve https://s3.amazonaws.com///params.json: received non 200 status code of 403 2017-08-18 01:32:31,309 - MainThread - awscli.clidriver - DEBUG - Exiting with rc 255

template.json:

{
    "AWSTemplateFormatVersion": "2010-09-09",

    "Resources": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "FunctionName": {
                "Ref": "LambdaFunctionName"
            },
            "Handler": {
                "Ref": "LambdaHandler"
            },
            "Role": {
                "Ref": "LambdaExecutionRoleArn"
            },
            "Code": {
                "S3Bucket": {
                    "Ref": "LambdaSourceBucket"
                },
                "S3Key": {
                    "Ref": "LambdaSourceKey"
                }
            },
            "SubnetID": {
                "Ref": "LambdaSubnetID"
            },
            "Runtime": "nodejs4.3",
            "Timeout": "25",
            "MemorySize": "128",
            "VpcConfig": "vpc-2323454f",
            "securityGroupID": "sg-0sdfs17g"
        }
    }
}

params.json:

[
        {
            "ParameterKey": "LambdaFunctionName",
            "ParameterValue": "hello-world"
        },
        {
            "ParameterKey": "LambdaHandler",
            "ParameterValue": "index.handler"
        },
        {
            "ParameterKey": "LambdaExecutionRoleArn",
            "ParameterValue": "arn:aws:iam::312345678910:role/LambdaExecuteRole"
        },
        {
            "ParameterKey": "LambdaSourceBucket",
            "ParameterValue": "test-lambda-functions"
        },
        {
            "ParameterKey": "LambdaSourceKey",
            "ParameterValue": "helloworld.zip"
        },
        {
            "ParameterKey": "LambdaSubnetID",
            "ParameterValue": "subnet-1113121f,subnet-fer333ex"
        }
]

After updating the command to:

aws cloudformation create-stack --stack-name test-local-stack --template-body file://c:/cli/aws/template.json --parameters file://c:/cli/aws/params.json

I get error

An error occurred (ValidationError) when calling the CreateStack operation: Template format error: [/Resources/Type] resource definition is malformed

I am trying to use Ref function for referring to the parameters which are passed from parameters file during stack creation.

Can someone please let me know what I am doing wrong?

like image 695
Dwarrior Avatar asked Aug 18 '17 05:08

Dwarrior


2 Answers

On the first look, the problem is not related to the parameters. The error message is "Template format error: [/Resources/Type] resource definition is malformed", and I think this is wrong:

"Resources": {
  "Type": "AWS::Lambda::Function",
  ...
}

What you want is:

"Resources": {
  "YourResourceName": { 
    "Type": "AWS::Lambda::Function",
    ...
  }
}
like image 98
olpa Avatar answered Oct 18 '22 21:10

olpa


For anyone else looking at how to use external parameters file with CF template and call the values using Ref:

Main template will look like the below:

{
"AWSTemplateFormatVersion": "2010-09-09",
"Parameters": {
    "LambdaFunctionName": {
        "Description": "Lambda Function name",
        "Type": "String"
    }
...},
"Resources": {
    "LambdaFunction": {
        "Type": "AWS::Lambda::Function",
        "Properties": {
            "FunctionName": {
                "Ref": "LambdaFunctionName"
            }
        },
    ...}
}

}

and the parameter json file should look like this:

[
    {
        "ParameterKey":"LambdaFunctionName",
        "ParameterValue":"hello-world"
    },
    ....
]

Thanks @olpa for guiding me in the right direction.

like image 37
Dwarrior Avatar answered Oct 18 '22 21:10

Dwarrior