Day 22–101 Days of DevOps — Introduction to AWS Lambda

Prashant Lakhera
6 min readJul 25, 2021

--

Welcome to Day 22 of 101 Days of DevOps. The topic for today is the AWS Lambda.

To view the complete course, please check the below url.

For more info, register via the below link

YouTube Channel link

What is AWS Lambda?

With AWS Lambda, you can run code without provisioning or managing servers. You pay only for the compute time that you consume — there’s no charge when your code isn’t running. You can run code for virtually any type of application or backend service — all with zero administration. Just upload your code, and Lambda takes care of everything required to run and scale your code with high availability. You can set up your code to automatically trigger other AWS services or call it directly from any web or mobile app.

  • To start with Lambda.

Go to https://us-west-2.console.aws.amazon.com/lambda → Create a function.

  • Create function you have four options
* Author from scratch: Which is self explanatory, i.e you are writing your own function
*
Use a blueprint: Build a lambda application from sample code and configuration preset for common use cases(Provided by AWS)
*
Browse serverless app repository: Deploy a sample lambda application from the AWS Serverless Application Repository(Published by other developers and AWS Patners)
*
Container Image: Lambda now supports container image which means you can package and deploy functions as a container image
  • Function name: HelloWorld
  • Runtime: Choose Python3.7 from the dropdown
  • Permission: For the time being, choose the default permission
  • Click Create Function

Invoking Lambda Function

  • When building applications on AWS Lambda, the core components are Lambda functions and event sources, an event source is the AWS service or custom application that publishes events, and a Lambda function is the custom code that processes the events.
* Amazon S3 Pushes Events
* AWS Lambda Pulls Events from a Kinesis Stream
* HTTP API requests through API Gateway
* CloudWatch Schedule Events
  • From the list, select EventBriddge(CloudWatch Events)
  • Rule: Create a new rule
  • Rule name: Everyday
  • Rule description: Give your Rule some description
  • Rule type: Choose Schedule expression and under its rate(1 day)(i.e., it's going to trigger it every day)
  • Click on Add and Save
  • Now go back to your Lambda Code(HelloWorld)
import jsondef lambda_handler(event, context):
# TODO implement
print(event) <--------
return {
'statusCode': 200,
'body': json.dumps('Hello from Lambda!')
}
  • Add this entry, which means we are trying to print the event
  • This time click on Deploy
  • Let’s try to set a simple test event. Click on Test.
  • Under Event template, search for Amazon CloudWatch.
  • Event Name: Give your event some name and test it
  • Go back to the main dashboard, and this time Click on Monitor. You will see one lambda Invocation and the duration for which this function is executed.
  • Click on the log stream, and you will see the same logs you see in the Lambda console.

Lambda Programming Model

  • AWS Lambda natively supports Java, Go, PowerShell, Node.js, C#, Python, and Ruby code and provides a Runtime API that allows you to use any additional programming languages to author your functions. Please read AWS documentation using Node.js, Python, Java, Ruby, C#, Go, and PowerShell.
  • You write code for your Lambda function in one of the languages AWS Lambda supports(provides a Runtime API that allows you to use any additional programming languages to author your functions). Regardless of the language you choose, there is a common pattern to writing code for a Lambda function that includes the following core concepts.
* Handler: Handler is the function AWS Lambda calls to start execution of your Lambda function, it act as an entry point.
  • As you can Handle, start with lambda_function, which is a Python Script Name, and then lambda_handler, which is a function and act as an entry point for event and context
  • Events: We already saw in the previous example where we passed the CloudWatch Event to our code. In simple terms, it’s the data sent during the lambda function invocation.
  • Context — AWS Lambda also passes a context object to the handler function as the second parameter. Via this context object, your code can interact with AWS Lambda. For example, your code can find the execution time remaining before AWS Lambda terminates your Lambda function. Other examples of context are request ID, log group, etc.
  • Logging — Your Lambda function can contain logging statements. AWS Lambda writes these logs to CloudWatch Logs.
  • Exceptions — Your Lambda function needs to communicate the result of the function execution to AWS Lambda. Depending on the language you author your Lambda function code, there are different ways to end a request or notify AWS Lambda successfully an error occurred during the execution.
  • One more thing I want to highlight is the timeout.
  • You can now set the timeout value for a function to any value up to 15 minutes. When the specified timeout is reached, AWS Lambda terminates the execution of your Lambda function. As a best practice, you should set the timeout value based on your expected execution time to prevent your function from running longer than intended.

Some new features

  • It now supports 1ms billing granularity(Reduced from 100ms to 1ms)
  • Larger lambda functions(max amount of memory now increased to 10GB and the corresponding vCPUs to 6)
  • Support for container image(package and deploy functions as a container image).
  • VPC endpoints support for AWS Lambda.

Lambda Layer: This is one feature I need badly in AWS Lambda and thanks AWS for introducing this feature and the feature is called the Lambda layer.

Lambda layer lets you share your code, you can upload the layer once, and then you can reference it any function. More about the Lambda layer in the below video.

If you like to dig deeper into the AWS concept, please feel free to check my book.

Looking forward to you guys joining this journey and spend a minimum of an hour every day for the next 101 days on DevOps work and post your progress using any of the below mediums.

--

--

Prashant Lakhera
Prashant Lakhera

Written by Prashant Lakhera

AWS Community Builder, Ex-Redhat, Author, Blogger, YouTuber, RHCA, RHCDS, RHCE, Docker Certified,4XAWS, CCNA, MCP, Certified Jenkins, Terraform Certified, 1XGCP

No responses yet