Developing Solutions with AWS Lambda Flashcards
What is AWS Lambda
Key concept : Serverless
AWS Lambda is a compute service that enables you to run code without provisioning or managing servers. You pay only for the compute time you consume—there is no charge when your code is not running.
The code you run on AWS Lambda is uploaded as a “Lambda function”
The code must be written in a “stateless” style i.e. it should assume that there is no affinity to the underlying compute infrastructure
Anatomy of a Lambda function
- Handler : When a Lambda function is invoked, code execution begins at what is called the handler. The handler is a specific code method (Java, C#) or function (Node.js, Python) that you’ve created and included in your package.
- Event object : One of the parameters provided to your handler function is an event object. The contents of the event parameter include all of the data and metadata your Lambda function needs to drive its logic.
- Context object : The context object allows your function code to interact with the Lambda execution environment. Content and structure vary, at minimum it will contain :
- AWS RequestId
- Timeout -> maximum of 900 seconds
- Logging -> to stream log statements to Amazon CloudWatch Logs
How to package in Python, Java, C#
- Node.js, Ruby, and Python
To create a Lambda function, you first create a Lambda function deployment package, a .zip file consisting of your code and any dependencies. Use npm or pip to install libraries. All dependencies must be at root level.
- Java Your deployment package can be a .zip file or a standalone jar; it is your choice. Use Maven or IDE plugins. Compiled class and resource files at root level, required jars in /lib directory.
- C#
A .NET Core Lambda deployment package is a zip file of your function’s compiled assembly along with all of its assembly dependencies. Use Nuget or VisualStudio plugins. All assemblies (.dll) at root level.
The three ways to deploy an AWS Lambda function
- If your code does not require custom libraries (other than the AWS SDK), edit your code inline through the AWS Management Console. The console will zip up your code with the relevant configuration information into a deployment package that the Lambda service can run.
- In an advanced scenario where your code requires custom libraries, you can upload a .ZIP file of your code and all of its dependencies through the AWS Management Console or the AWS CLI.
- You can also create your Lambda deployment package (.ZIP) and upload it to an S3 bucket, and AWS Lambda loads your code directly from Amazon S3.
The three Lambda execution models
- Synchronous : direct call (ex: from Amazon API Gateway)
- Asynchronous : triggered by events -> Push event model
- Stream-based : triggered by changes (ex: from DynamoDB or Kinesis) -> Pull event model
How does the Push event model work
An event source directly invokes a Lambda function when it publishes an event
-> Sources : Amazon S3, Amazon SNS, Amazon Cognito, Amazon Echo, and user applications.
You do not use the AWS Lambda APIs to map your Lambda function to its event source. Instead, you use APIs from the event source to configure this mapping.
How does the Pull event model work
In the pull event model, AWS Lambda polls the event source and invokes your Lambda function when it detects an event.
-> Lambda is used for streaming event sources such as Amazon Kinesis and DynamoDB streams.
Lambda provides an API for you to create event source mappings that associate your Lambda function with a specific event source
How does the Direct invocation work
It causes AWS Lambda to run the function synchronously and returns the response immediately to the calling application letting you know whether the invocation happened.
It is available for custom applications.
Difference between Execution permissions and Invocation permissions
Execution permissions: The permissions that your Lambda function needs to access other AWS resources in your account. Execution roles determine what your function can do.
Invocation permissions: The permissions that the event source needs to communicate with your Lambda function. Invocation roles determine who can run your function
Difference between Push and Pull models for invocation permissions
- Push model : grant these event sources permissions to invoke the Lambda function by updating the access policy associated with the Lambda function
- Pull model : grant AWS Lambda permission to read from the stream
How does versioning work
Versioning allows you to publish one or more versions of your Lambda function. Each time you publish a version, AWS Lambda copies the $LATEST version (code and configuration) to create a new version.
What is an alias
Aliases can be created for Lambda function. An alias is a Lambda resource and has its own unique ARN. An alias is like a pointer to a specific Lambda function version. It allows you to access the Lambda function without having to know the specific version the alias is pointing to.
What is a layer
A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies. With layers, you can use libraries in your function without needing to include them in your deployment package.
A function can use up to 5 layers at a time.
You can create layers, or use layers published by AWS and other AWS customers.
Function code best practices
- Separate the Lambda handler (entry point) from your core logic
- Take advantage of execution context reuse to improve the performance of your function
- Use environment variables to pass operational parameters to your function
- Control the dependencies in your function’s deployment package
- Minimize your deployment package size to its runtime necessities
- Minimize the complexity of your dependencies
- Avoid using recursive code
- Share common dependencies with layers
Function configuration best practices
- Performance test your Lambda function for memory
- Load test your Lambda function for timeout
- Understand Lambda limits
- Delete Lambda functions that you are no longer using