Sharing is caring ๐Ÿ™‚

AWS Lambda is a service that lets you 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. With Lambda, you can run code for virtually any application or backend service with zero administration. Just upload your code, and Lambda takes care of everything required to run and scale your code with high availability.

EC2 vs. Lambda

When you use an EC2 instance, there is a virtual server in the cloud, but we are restricted by the amount of memory and CPU power we give it. That instance is continuously running even though sometimes we don’t use it. To scale with EC2, we can use an autoscaling group, but we would need to add or remove servers over time. That process can be complicated and time-consuming.

AWS Lambda provides new ways to think about these challenges. With Lambda, we don’t have servers, we have virtual functions, and these functions are limited by time. This fits perfectly for code that runs for shorter durations but needs to run on demand. This means it will be run anytime you need to run a function. When you don’t need a function, it will not be run, and we will not be billed for it.

Lambda Scaling

In cases where scaling is needed, it’s already automated as part of the Lambda service, which is why Lambda is such a popular service from AWS. The benefits of using AWS Lambda are that the pricing is straightforward, and you’re only going to pay per request, and per compute time. As a bonus, when you are first starting with Lambda, AWS provides a free tier that is quite generous. The AWS Lambda free tier includes one million free requests per month and 400,000 GB seconds of compute time per month. This enables you to run some good services on Lambda for free, especially if you use your imagination!

Lambda Integration

AWS Lambda is integrated with the whole AWS suite of services. What that means is we have integration with almost anything you could need. The event-driven nature of Lambda means functions will only get invoked by AWS when something happens, when an event occurs, or when required.

Programming With Lambda

Lambda is fully integrated with many programming languages, and monitoring is effortless via CloudWatch. Lambda can run many different programming languages. The popular programming languages on Lambda include JavaScript, Node.js, Python, Java, C#, Golang, C# PowerShell, Ruby, and any language you want through the Custom Runtime API.

With Lambda, getting more resources per function is easy since you can get up to 10 gigabytes of RAM per function. Increasing the RAM will also improve the CPU and the network quality, which only helps your applications run even better.

Lambda Container Image

Lambda also has a runtime called the Lambda Container Image, allowing you to run actual Docker containers on top of Lambda. These container images must implement the Lambda runtime API. Not just any Docker image will be able to use this service, only specific Docker images that adhere to the Lambda runtime Application Programming Interface. The AWS Fargate service will be a better way of running standard Docker images.

Use Cases For AWS Lambda

The use cases for Lambda are vast. Here are just a few examples of what can be done with Lambda.

Serverless Websites

You can use Lambda and S3-hosted static websites for a Serverless Website. The web frontend can be hosted on S3, which can send requests to Lambda functions via API Gateway HTTPS endpoints. Lambda can handle the application logic and persist data to RDS or DynamoDB. A serverless application that runs in the AWS Cloud has the characteristics of the application has built-in fault tolerance and the application can scale based on demand. A benefit of using AWS serverless computing is that management of infrastructure is offloaded to AWS.

Predictive Page Rendering

AWS Lambda can offer predictive page rendering to retrieve documents and multimedia files, which may be used by the next page requested, and to perform the first steps of rendering them for display. If an external source is serving multimedia files, the Lambda application can check for their availability and attempt to use backup alternatives if they are not available.

Process S3 objects

S3 has object event notifications that you can use to process files by Lambda once they are in S3 buckets. Thumbnail creation with AWS Lambda is a typical example of this.

Log Analysis

Lambda can be used to build a function to check log files from Cloudtrail or Cloudwatch. This function could search in the logs looking for specific events as they happen and send out notifications via SNS or send notification hooks to Slack, Zendesk, or other applications by making an HTTP request to their API endpoint within Lambda.

Backup Automation

Lambda events can be scheduled and are suitable for performing repetitive tasks in AWS accounts. You can create backups, check idle resources, generate reports, and more. The Python boto3 library with Lambda can help with this,

Real-time Data Processing

Real-time data is often bursty and random, making it a good option for AWS Lamda processing. Data can be streamed via mobile devices, IoT devices interacting with the physical world, or user input devices. Lamda is a good choice for these types of processing.

AWS Lambda is good for a company that wants to migrate a critical application that has a short runtime to AWS. The application is invoked by changes in data or by shifts in system state. AWS Lambda provides a compute solution that maximizes operational efficiency and minimizes the cost of running the application.

Your First Lambda Service

To get started with a new Lambda service, we first need to search for it in the AWS dashboard. You’ll notice the option to click that says “Lambda Run Code without Thinking about Servers.”

Lamda – Run Code without Thinking about Servers

Once you click into the Lambda service, you have the option to create a function.

Lambda > Functions

For this quick example, we’ll choose to use a blueprint to create a simple Python hollo world Lambda function.

Create A Function

When using the blueprint, you’ll need to configure some options, like the function name and execution role.

Configure Blueprint
Click Create function

After the configuration is complete, go ahead and click Create function. You should then see a message like Successfully created the function python_hello_world.

We can now test the Lambda function using a test event.

Running the Lambda test shows the output below.

Test Event Name


Function Logs
Loading function
START RequestId: b9f2c4b4-ac8a-40d0-bb88-84d5a44b870a Version: $LATEST
value1 = hello
value2 = from
value3 = Lambda
END RequestId: b9f2c4b4-ac8a-40d0-bb88-84d5a44b870a
REPORT RequestId: b9f2c4b4-ac8a-40d0-bb88-84d5a44b870a	Duration: 1.54 ms	Billed Duration: 2 ms	Memory Size: 128 MB	Max Memory Used: 36 MB	Init Duration: 108.26 ms

Request ID

This gives us a general idea of how to do things in AWS Lambda. The other thing to note is that this function can now be configured. We get some of the most important settings by going into the general configuration. The first one is around memory. So we can have the memory from anywhere between 128 megabytes up to 10,000, 240 megabytes. You will be billed more in terms of timeouts if you have more memory. We can go anywhere between three seconds or five seconds to 15 minutes. The maximum time out is 15 minutes, but you want to ensure that you only use the function for the time that you think will be used.

In the configuration panel for Lambda, we have these options:

  • General configuration – A starter AWS Lambda function
  • Triggers – Add a Trigger for the Lambda function. Choose from a wide variety of available triggers.
  • Permissions – View the resources and actions your function has permission to access.
  • Destinations – Add a destination to your function to retain records of function invocations or to retain discarded events. Destinations can be configured for asynchronous invocation or stream triggers. Asynchronous invocation is configured on all subsequent asynchronous requests, while stream invocation is configured on a single event source mapping that reads from a Kinesis or DynamoDB stream. Asynchronous invocation โ€“ The source can be your application or an AWS service like Amazon S3 that invokes the function asynchronously. The destination can be any of the following:
    • An Amazon SQS queue
    • An Amazon SNS topic
    • A Lambda function
    • An EventBridge event bus
  • When the condition for using the destination is met, Lambda sends a document with the event, response, and context to the destination resource. Stream invocation โ€“ The destination can be an Amazon SQS queue or an Amazon SNS topic. Configure a destination for discarded records because they are too old or fail all retries. On failure, Lambda sends a document with details about the discarded records to the destination resource.
  • Function URL – A function URL is a dedicated HTTP(S) endpoint for your function. When your function URL is configured, you can use it to invoke your function through a browser, curl, Postman, or any HTTP client. When you configure a function URL from the main function page, Lambda assigns the function URL to the $LATEST unpublished version of your function. You cannot set a function URL to any other function version, but you can assign a function URL to any function alias.
  • Environment variables – You can define environment variables as key-value pairs accessible from your function code. These are useful to store configuration settings without changing function code.
  • Tags – A tag is a label that you assign to an AWS resource. Each tag consists of a key and an optional value. You can use tags to search and filter your resources or track your AWS costs.
  • VPC – All Lambda functions run securely inside a default system-managed virtual private cloud (VPC). However, you can also configure your Lambda function to access resources in a custom VPC. A custom VPC defines a private network of resources, such as databases, cache instances, or internal services. If your AWS Identity and Access Management (IAM) permissions allow you only to create functions that connect to your VPC, you must configure the VPC details when you make the function. If your IAM permissions enable you to create functions that aren’t connected to your VPC, you can add the VPC configuration after you create the function.
  • Monitoring and operations tools – Lambda integrates with other AWS services to help you monitor, trace, and debug your Lambda functions: Amazon CloudWatch โ€“ Lambda reports CloudWatch metrics for your functions, including total requests, latency, error rates, and duration. Amazon CloudWatch Logs โ€“ Lambda creates a log group in CloudWatch Logs that you can use to create custom logging statements to validate your functions. AWS X-Ray โ€“ X-Ray allows you to detect, analyze, and optimize performance issues with your functions. CloudWatch Lambda Insights โ€“ Lambda Insights aggregates metrics, custom logging, enhanced diagnostics, and active tracing on your functions.
  • Concurrency – Use provisioned concurrency to enable your function to scale without fluctuations in latency. You can use Application Auto Scaling to adjust provisioned concurrency to automatically maintain a configured target utilization. Provisioned concurrency runs continually and has separate pricing for concurrency and execution duration.
  • Asynchronous invocation – AWS Lambda handles retries when you invoke your function asynchronously. Incoming events are placed in a queue before being sent to the function. If the function returns an error, Lambda retries up to two times. If the function is throttled or Lambda returns an error, the event is kept in the queue for up to six hours. To change the default retry behavior, adjust the number of retry attempts and the maximum amount of time an item can be kept in the queue. Lambda discards it when an event fails all attempts or stays in the asynchronous invocation queue for too long. Configure a dead-letter queue to send discarded events to an Amazon SQS queue or Amazon SNS topic. Your function’s execution role requires permission to write to the queue or topic. Amazon Simple Notification Service (Amazon SNS) is used to send both text and email messages from distributed applications.
  • Code signing – Create a configuration that you can attach to one or more functions to enable code signing. With code signing, you can ensure that the code has been signed by an approved source and has not been altered since signing and that the code signature has not expired or been revoked.
  • Database proxies – A database proxy manages a pool of database connections and relays queries from a function. This enables the function to reach high concurrency levels without exhausting database connections. A database proxy uses a Secrets Manager secret to get credentials for the database. Create a secret with username and password fields. Assign the proxy a role that gives Amazon RDS read permissions on the secret. The console only shows roles that Amazon RDS has permission to use. You can use the database password to connect your function to the proxy or the AWS SDK to generate a token with permissions from the function’s execution role. If you enable IAM authentication and authorization on the proxy, you can’t connect to it with the database password. To disconnect a database proxy from your function, open the function’s execution role and delete the IAM policy that allows connections to the DB instance (rds-db:connect). You must first link your function to the VPC where your database runs to connect to a database. See the sample applications for working examples.
  • File systems – An Amazon EFS file system provides distributed network storage for the instances of your function. When your function scales up, each function instance creates a connection to the file system and mounts it to a local path, which you can access from your code. To connect to a file system, you must first connect your function to the VPC where your file system runs. For a cross-account connection, configure VPC peering to the VPC of the file system.
  • State machines – The State machines pane lists the state machines with at least one workflow step that invokes the current Lambda function. Use the search box to find your state machines by name or type. To view a state machine’s details, including all its functions, choose its name. You can define a state machine to catch Lambda function errors and to retry requests if the function fails. Choose to Get started to create a state machine in the Step Functions console with a predefined catch-and-retry application pattern.

Under the AWS shared responsibility model, creating versions of Lambda functions is the customer’s responsibility when managing AWS Lambda functions.

Learn More About AWS Lambda

Sharing is caring ๐Ÿ™‚