AWS Lambda Python Demo Using AWS CLI

Convert Kg to Lb 100% via the Command Line!

AWS Lambda is a function as a service! Without going into the details of it, let us get into the demo mode, join me.

Create an IAM Role

Create a policy document json file policy.json

{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}

Create a role with the above assume role policy.

aws iam create-role --role-name sree-lambda --assume-role-policy-document file://policy.json
{
"Role": {
"Path": "/",
"RoleName": "sree-lambda",
"RoleId": "AROAI5CXJ3JBG2PMT6OPG",
"Arn": "arn:aws:iam::11111111:role/sree-lambda",
"CreateDate": "2018-11-18T12:35:24Z",
"AssumeRolePolicyDocument": {
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
}
}

Create the AWS Lambda Function

Since we are using a Python function, let us create a file called sree_fn.py like this.

print "Starting..\n"
def sree_handler(event, context):
kg=event['Kg']
lb=float(kg)*2.20462
ans = str(kg) + " Kgs is " + str(round(lb, 2)) + " lbs\n"
print ans
return ans

Zip the Function File

We need zip this before we can make the create fucntion Lambda call. The zipped archive is called sree-fn.zip.

zip sree-fn.zip sree_fn.py

Create Function Lambda Call

This how we can use the cli to upload and create a Lambda function. We are passing the function name, zip file name, handler name (see the function name in our Python script), and the role arn. We also need to tell AWS the run time to be used, in our case it is Python2.7.

aws lambda create-function --function-name sree-py1 --zip-file fileb://sree-fn.zip --handler sree_fn.sree_handler --runtime python2.7 --role arn:aws:iam::11111111:role/service-role/sree-lambda
{
"FunctionName": "sree-py1",
"FunctionArn": "arn:aws:lambda:us-west-2:11111111:function:sree-py1",
"Runtime": "python2.7",
"Role": "arn:aws:iam::11111111:role/service-role/sree-lambda",
"Handler": "sree_fn.sree_handler",
"CodeSize": 317,
"Description": "",
"Timeout": 3,
"MemorySize": 128,
"LastModified": "2018-11-18T08:55:06.299+0000",
"CodeSha256": "D/E++4ckm87zuJI5npHozZ0DLlpfv12Tm2AxqjJlwRk=",
"Version": "$LATEST",
"TracingConfig": {
"Mode": "PassThrough"
},
"RevisionId": "213fe80a-babf-431b-89aa-3f6274ca540f"
}

Invoke the Function

We can use the cli itself to pass parameters and invoke our function like this.

aws lambda invoke --function-name sree-py1 --payload '{"Kg":"100"}' myresults.txt
{
"StatusCode": 200,
"ExecutedVersion": "$LATEST"
}

The results of this execution is stored in the out file myresults.txt

cat myresults.txt"100 Kgs is 220.46 lbs"

Quick Look at the AWS Console

Here are some of the screenshots. We can do similar testing via the console as well like this. First we need to create an input argument JSON which is called a test event.

Then using the above test event we can run a test and see the results!

Cleaning Up

aws lambda delete-function --function-name sree-py1
aaws iam delete-role --role-name sree-lambda

That is it, thanks for your time. Do remember to follow for more such tiny demos!

Sreeprakash Neelakantan

Written by

AWS Certified DevOps Engineer & Solutions Architect Professional — Docker | Kubernetes | DevOps — Trainer | Running | Swimming | Cycling

Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade