AWS Cloudformation : Passing environmental variables as parameters to lambda functions

22,025

Solution 1

For this purpose there is special section in cloudformation template - Parameters

"Parameters" : {
  "MyVariable" : {
    "Type" : "String",
    "Default" : "test",
    "AllowedValues" : ["test", "non-test"],
    "Description" : "My very important variable."
  }
}

And then use these parameters in Function declaration:

"Environment":{  
   "Variables":{  
      "SomeVariable":{  
         "Ref":"MyVariable"
      }
   }
}

And then pass values for this Parameters block when creating stack from cloudformation template:

aws cloudformation create-stack --stack-name S1 --template-body example template --parameters ParameterKey=MyVariable,ParameterValue=myValue

More information - here

Solution 2

"I want to pass the key value pair object as parameter"
You cannot pass key-value pairs as parameters in AWS CF templates. For the accepted parameter types please see this

Share:
22,025
Balaji V
Author by

Balaji V

Updated on November 30, 2020

Comments

  • Balaji V
    Balaji V over 3 years

    I am creating a cloud formation for lambda . I want to have a generic lambda script that created lambda . I am having problem injecting "Environment" parameter from outside .

    I want to pass the key value pair object as parameter . Can some one tell me how to do it . I have highlighted it below

    {
      "Variables" : **{ String:String, ... }**
    }
    
    {
      "Type" : "AWS::Lambda::Function",
      "Properties" : {
        "Code" : Code,
        "DeadLetterConfig" : DeadLetterConfig,
        "Description" : String,
        "Environment" : Environment,
        "FunctionName" : String,
        "Handler" : String,
        "KmsKeyArn" : String,
        "MemorySize" : Integer,
        "ReservedConcurrentExecutions" : Integer,
        "Role" : String,
        "Runtime" : String,
        "Timeout" : Integer,
        "TracingConfig" : TracingConfig,
        "VpcConfig" : VPCConfig,
        "Tags" : [ Resource Tag, ... ]
      }
    }