API, Automation, AWS, Cloud Computing, DevOps, Tutorials

8 Mins Read

AWS Lambda – the future of event driven automation

Lambda is a compute service in Amazon Web Services, which triggers actions based on the events. Lambda is efficient and event driven which responses to events from either objects added or removed from S3, updating to DynamoDB tables, SNS, data in Kinesis Stream, Cloudwatch logs, In-App activity etc. Once our code is uploaded to Lambda, this service handles all the capacity, scaling, patching and administration of the infrastructure needed to run our code and also provides us with greater visibility of performance by publishing us with logs and metrics to Amazon CloudWatch.Lambda is cost- effective as it helps in running our code without any provisioning and managing servers with high availability.  AWS has been adding a lot of new features. AWS Lambda supports only 4 AWS regions, which include US-East (North Virginia), US-West (Oregon), EU-West (Ireland) and Asia Pacific (Tokyo). AWS provided support for writing Lambda functions in Node.js language and added Java Programming Language recently this year. New features are added in Lambda to help developers and other community people to make life much simpler and easy.

Recently, AWS Lambda function has brought in new features;

Lambda is all about functions, so let’s take a look at each of these new features

Lambda functions support Python Programming Language:

As we know that AWS Lambda has support for writing functions in Node.js and Java, recently Python is added to that list which is very easy to learn and use. Python 2.7 comes with in-built access for AWS SDK for python. Python code must be zipped together with dependencies and uploaded to AWS Lambda using AWS CLI or using console.  

[showhide type=”qs” more_text=”Click here for the detailed Lab for Python.. ” less_text=”Hide Details” hidden=”yes”]

Lab for stopping the particular instances based on Tags with Role-Based in Python language.

Task 1

https://s3-ap-northeast-1.amazonaws.com/srihari-bucket/srihari-blog/ec2listinpython.zip

NOTE : There is no need to hard code by adding your access key and secret key because Lambda works on Role-Based. Task 2

Create a Lambda Function. In this task you are going to create the Lambda function.

1. Click on Lambda under Compute section.

  1. Select Tokyo Region.(Any region of your choice)
  2. Click on Create a Lambda function.

2

  1. Click on Skip option.

  

  Note: Do not select any Blueprint.  

  1. Configure the Lambda function.
    • Enter the Name for Lambda function.
    • Give the Description for Lambda function.(Optional)
    • Runtime should be Python 2.7 from the Drop Down list.

4

  1. Perform the following steps as shown

5

7.  Update the Handler as shown below(ec2listinpython.lambda_handler)

6

8. Creating a IAM Role for Lambda.

7

9. AWS Lambda uses a role. Create your role from the following steps.

12

13

 

 

14

10.  In Advanced settings make changes to Timeout.

9

11. Click on Create function.

10

12. Test the following Lambda function

. 13

13. Lambda function will stop the instances with the tag named TagName we have specified.

screenshot-eu-west-1.console.aws.amazon.com 2015-11-05 13-17-00

[/showhide]

Lambda supports Versioning and Aliases:

Lambda now supports versioning, which helps to maintain multiple versions of our Lambda function code and helps to achieve the execution in different environments like PROD, DEV, TEST etc. Versioning also allows us to publish one or more versions of our Lambda function code helping in rollback to previous version if there is any error in current version. Each Lambda function version will have there own ARN. After publishing the version of our Lambda function code, the code of the corresponding version will become immutable i.e. the code cannot be changed. AWS Lambda supports aliases for each of our Lambda function versions. Its alias is a pointer, which points to specific Lambda version. Each alias has an ARN, which cannot point to another alias.

NOTE: Versions after publishing will become immutable, whereas alias are mutable and can be updated accordingly.

Example of alias in Lambda: Suppose the event source for Lambda is S3 Bucket and whenever we choose S3 bucket as event source, we need to perform few notification changes in S3 bucket:

This will invoke the Lambda function whenever the object is created or removed for S3 bucket. But in this case, each time we publish our version we have to update in notification configuration of S3 bucket to invoke correct Lambda function version. Instead of specifying our Lambda function version ARN, we can specify our Lambda function alias ARN in notification configuration. (Example: PROD alias ARN) and we don’t need to update notification configuration in S3 events. If we promote new versions of our Lambda function, we need to add only PROD alias ARN of that corresponding version. Aliases which is pointing to one version can easily point to another updated version and helps us to easily roll back whenever any problems arises. We can refer to our Lambda functions using ARNs.

There will be two ARNs associated with initial version;

Qualified ARN: The function which is having version suffix is called as Qualified ARN

ex: arn:aws:lambda:aws-region:account-id:function:HelloWorld:$LATEST

UnQualified ARN: The function which is not having any version suffix is called UnQualified ARN

ex:  arn:aws:lambda:aws-region:account-id:function:HelloWorld

UnQualified ARN can be used in all the relevant operatoins but we will not able to create an alias and they will be having their own resource policies.

AWS Lambda now also supports Scheduled tasks (It will replace cron jobs):

AWS Lambda has come up with amazing feature, which will invoke our Lambda function code on regular and scheduled basis similar to cron jobs. For running cronjobs, we need to have one separate EC2 server running forever, which will cost that can be easily overcome by AWS Lambda scheduling feature . Our AWS Lambda function will be invoked only at particular scheduled time and we don’t need to take care of any EC2 server running, because Lambda will take care of provisioning and managing of servers. It is very  cost-effective as the pricing is applicable only for the amount of the time Lambda function code runs. We can specify a fixed data, which includes the number of minutes, hours or days or we may specify a cron expression (minutes: 0-59, hours: 0-23, days: 1-31, month: [1-12]or [JAN-DEC], days of week: [1-7]or[SUN-SAT], year:[1970-2199]).

 [showhide type=”we” more_text=”Demonstration of Scheduling a Lambda function which stops instances at particular scheduled time… ” less_text=”Hide Details” hidden=”yes”]

Task 1

https://s3-ap-northeast-1.amazonaws.com/srihari-bucket/srihari-blog/ec2listinpython.zip

NOTE : There is no need to hard code by adding your access key and secret key because Lambda works on Role-Based.

Task 2

Create a Lambda Function.

In this task you are going to create the Lambda function.

1. Click on Lambda under Compute section

. Capture

  1. Select Tokyo Region.(Any region of your choice)
  2. Click on Create a Lambda function.

2

  1. Please select the following BluePrint(lambda-canary).
  2. 2
  3. Configure the event sources as shown below and click on Next.3new
  4. Configure the Lambda function.
    • Enter the Name for Lambda function.
    • Give the Description for Lambda function.(Optional)
    • Runtime should be Python 2.7 from the Drop Down list.

4

  1. Perform the following steps as shown

5

8.  Update the Handler as shown below

9. Creating a IAM Role for Lambda.

10. AWS Lambda uses a role. Create your role from the following steps.

12 13

 

14

 

11.  In Advanced settings make changes to Timeout.

9

12. Click on Create function and select Enable later.

9

13. After creating a Lambda function, go to Event sources and Click on disabled to Enable the lambda function for performing Scheduled Task. 10 11

14. Test the following Lambda function.

13

15. Lambda function will stop the instances with the tag named TagName we have specified.

screenshot-eu-west-1.console.aws.amazon.com 2015-11-05 13-17-00

 [/showhide]

Execution time for Lambda functions has increased:

AWS Lambda function’s execution time has increased from 1 min(60 sec) to 5 min(300 sec). Lambda functions can scale up to process large volumes of data , in such scenarios the execution time can be increased up to 300 sec.We can specify the required timeout while creating our Lambda function as shown.

screenshot-eu-west-1.console.aws.amazon.com 2015-11-05 13-46-43

 

 

WRITTEN BY CloudThat

SHARE

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!