top of page
  • Cloud Life Team

Make Life Easier and Automate with AWS Lambda

Organizations that migrate to Cloud services gain benefits like managing and scaling complex IT infrastructures as well as handling variable workloads with ease. Automating services via the cloud extends these benefits further. One of the key tools AWS offers is Lambda, an on-demand service tackling a wide variety of tasks.

AWS Lambda

Amazon launched its serverless capability Lambda over 4 years ago. Lambda is an application, adhering to all the serverless principles:

  • No servers to provision or manage

  • Scales as per usage

  • Pay for value

  • High availability and fault tolerance

Serverless computing

AWS Lambda has several capabilities:

  • Load Balancing

  • Auto Scaling

  • Handling Failures

  • Security Isolation

  • OS Management

  • Managing Utilization

How does AWS Lambda functions work?

AWS Lambda is included within a serverless application, connecting several components. An event within a database or web service can trigger one or more Lambda functions.

AWS Lambda Serverless

There are around 40 different AWS services that can directly interact with Lambda. You can write your own API in a Lambda-supported language to communicate with the service, like requests to API gateways responding to an object being put in a S3 bucket.

AWS Lambda function
A basic representation of the workings of a lambda function

Lambda Events and Triggers

Lambda functions execute when events are triggered. There are different services that can create an event. First, create a Lambda function from the AWS console. A few different ways to trigger a Lambda function are:

API Gateway Event - In this method, when someone calls an API gateway it will trigger the Lambda function. These events are regarded as classic events. For Lambda to know about the event, you need to define it in the configuration, or serverless.yml file.

AWS Lambda function

S3 Events - Amazon S3 can publish events on a bucket of various types, such as PUT, POST, COPY and DELETE. Using the bucket notification feature, as shown in the diagram, you can configure an event source mapping that directs Amazon S3 to invoke a Lambda function when a particular type of event occurs.

AWS Lambda function

Automation with AWS Lambda

Repetitive processes such as a test and deployment pipeline can be automated by triggering them with events or running them according to a fixed schedule.

In the example below, we will understand how AWS Lambda can be used to automate clean-up of unused images from Amazon ECR.

The new image was created when Amazon ECR was used as part of the container build and deployment pipeline. The repositories are filled quickly with the new images whenever the code is changed. Manually identifying and deleting the images is a tedious job; however, Lambda helps to automate the process.

The process has two components - a Python Script and an AWS Lambda function. The Python script, which can be found at AWS Labs Repository, identifies the images in use by running tasks and deletes the stale images. The AWS Lambda function executes the script using Amazon CloudWatch Events.

The logic of the script is as follows:

  • Use the ListCluster API operation to get a list of all ECS clusters.

  • List running tasks for each cluster using ListTasks.

  • Call DescribeTasks to get the ARNs for each running task.

  • Use DescribeTaskDefinition to get container image for each running task

  • Filter out container images that contain “.dkr.ecr.” and “:” .

  • Use DescribeRepositories to get a list of all the repositories.

  • Use DescribeImages to get imagePushedAt value, tags, and SHA for every image.

  • Ignore the images with “latest” tag or which are currently running and delete the images that have tags as discovered earlier using BatchDeleteImage.

AWS Lambda function

Lambda discovers the container image tags used by running tasks based on the python script logic. It queries all ECR images and decides on the image tags to be cleaned based on coded logic. Finally, it deletes the images.

Final thoughts

AWS Lambda functions offer a multitude of features and possibilities for automation, with pricing only based on the number of instances when the code is run. The serverless application is a smart and efficient way for developers and businesses to better their performance and accomplish more with their time.

253 views0 comments
bottom of page