Running and testing lambda-s in Linux console

 

Developing lambda-s thru AWS console is painfull to every cloud engineer. Many times I had my browser going crazy and removing my newly written code before I was able to click the magic Save & Test buttons was extremely frustrating and time consuming. After moving from AWS Console to linux console (and my favorite vim editor) I reached the point where I was able to develop lambda much faster and enjoy benefits of Linux script error handling.

Pre-requirements

Directory structure that I have decided to follow was:

/home/tameshi/lambda/

  • lambda/lambda_function.py
  • make.sh

 

Content of our lambda (lambda/lambda_function.py) is fairly simple:

 

For our purposes we have created a simple IAM role called lambda_basic_execution with just one policy attached:dd

 

Creating first version of Lambda

Initial version of my commands that I used to create lambda functions were very basic:

 

Above output confirms that lambda has been deployed, SHA256 of zip file is “b8vdj7/5xzhDwykUg/jlAZtS8afVafIHdRjIxH5EONM=” and ARN of newly created lambda is: “arn:aws:lambda:eu-west-1:11111111111111:function:lambda_function

Updating of lambda code

Now let’s go and change our lambda & re-deploy changed version:

 

Notice few things from above output. At first notice the “Version” got changed from 1 to 2. Secondly CodeSha256 changed due to lambda code change. If you would re-deploy the same code “Version” would together with CodeSha256 would remain the same.

Lambda SHA256 explained

In order to see what is the CodeSha256 of our zip file:

Use the following command:

Lambda Invocation

 

 

Notice that we add the –log-type Tail switch + –qualifier that informs that at first we want to see our log output and we strictly inform that we want this version of Lambda to be executed. If we add few more switches (–query) we can decode output from LogResult.

 

Now.. this looks much nicer. As you can see we have or famous Hello world in here. Output of function is stored at /tmp/out_file:

Summary

Now.. Let’s sum it up all together in a simple script that we might use in our lambda projects:

 

Now what problems did we solve?

  1. We are now able to quickly test our lambda code without disruption.
  2. We don’t need to go into cloudwatch logs to see function output.
  3. Linux developers can continue to work with their way of working.
  4. Beginning of nice Linux & Lambda framework that can be used in your CI/CD pipelines.

 

Script that is attached to this article – lambda-linux.tar.gz – can be freely modified under MIT license

In next chapter of lambda development tutorials we will show you how to link CloudFormation and lambda development.

 

That’s it! If you wish to know more please Contact us!