Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass and retrieve constant json data to lambda function

enter image description here

I have lambda function defined sth like :

def lambda_handler(event, context):

   #get constant json argument passed from cloudwatch event rule

   ...

What is the way to get the values defined in Target/Configure Input /Constant(Json text).

like image 919
sakhunzai Avatar asked Nov 18 '16 11:11

sakhunzai


People also ask

How do you pass payload to lambda function?

To invoke a lambda function, passing it a file as a payload: Create a file on your local system that stores valid json in the form of the event your lambda function expects. Invoke the lambda function, passing it the file as the --payload parameter.

How do you auto trigger a lambda function?

Automated Triggering the Lambda Functions This can be done by setting up special rules in the CloudWatch console. Then Lambda functions will be triggered whenever the condition is met. Navigate to Rules under Events on the left pane in the CloudWatch portal and click Create Rule.


2 Answers

As I read in AWS documents, json passed to python as dict type. And then I simply call the value like this:

passed json:

{"type": "daily", "retention": 7}

Then in your handler:

def lambda_handler(event, context):
    type = event["type"]
    rententionDay = event["retention"]
    ...

Use this I was able to make an automation snapshot for all ebs volumes. Hope it help.

like image 134
Dominic Nguyen Avatar answered Oct 13 '22 07:10

Dominic Nguyen


This is based on NodeJS, but it should be the same for Python. The Constant under Input is a simple JSON encoded object, which can then be accessed using the event variable.

Input

{ "config": "uk" }

Lambda

console.log(event.config)

Found this entry as one of the top Google results, so hopefully this will help someone else.

like image 32
knrdk Avatar answered Oct 13 '22 07:10

knrdk