Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AWS Cloudformation : Passing environmental variables as parameters to lambda functions

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, ... ]
  }
}
like image 654
Balaji V Avatar asked Jul 20 '18 09:07

Balaji V


People also ask

Can Lambda layer access environment variables?

You are right that there is no concept of environment variables for Lambda Layers.

Can CloudFormation interact with Lambda?

All in all, CloudFormation makes deploying AWS Lambda functions incredibly simple. Start by creating the template file that will define your resources. This will be your working folder for your code. Next, create your function in the appropriate file for your desired Lambda runtime.


2 Answers

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

like image 82
Aleksey Balenko Avatar answered Oct 21 '22 05:10

Aleksey Balenko


"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

like image 34
TheUnderdogr Avatar answered Oct 21 '22 06:10

TheUnderdogr