Learn AWS Lambda in 5 minutes

Jo-Yu Liao
5 min readFeb 27, 2020

--

Introduction to a famous serverless computing platform — AWS Lambda

Have you ever talked to Amazon Echo Dot, a voice-controlled smart speaker before? If you have, did you wonder how it works? In fact, when you speak to an AWS smart speaker, you speak to a AWS Lambda function, and get responses from it. In this article, you will know what is AWS Lambda and how to use it.

A Brief History of Cloud

A brief story of Cloud

Before 2006, if companies want to deploy applications, they need to buy some physical machines, hire more operations engineers, and spend from a few days to several months on deployment.

In 2006, Amazon launched AWS EC2 (Amazon Elastic Compute Cloud), an IaaS that provides secure, resizable compute capacity in the cloud. With EC2, companies can save time and money on the physical layer.

However, companies still pay for resources that go unused, and developers still need to maintain instances, manage scaling and infrastructure.

Fortunately, with serverless technology, those problems can be solved. Companies can only pay for the compute time they consume, and developers can just focus on coding and shift those responsibility to Cloud Computing Vendors. The most famous serverless product is Lambda.

What is AWS Lambda

AWS Lambda

AWS Lambda is a an event-driven, serverless computing platform provided by Amazon since November 2014. AWS Lambda lets you run code without provisioning or managing servers. It executes your code only when needed and scales automatically by scale-out. What you need to do is to upload your code to AWS or write code in Lambda’s code editor, set up events to trigger lambda, and create resources to store results, if at all.

image source: AWS

The core conception of AWS Lambda

The core conception of AWS Lambda

Functions

A Lambda function consists of code you provide, associated dependencies, and configuration. It’s an independent unit of deployment. A Lambda function will be trigger by an event, so the more events you have, the more Lambda functions will be invoked. Moreover, Lambda functions can trigger other lambda functions synchronously or asynchronously.

Triggers

Triggers are AWS services or resources that invoke your function. It can invoke lambda by synchronous, asynchronous, or poll-based events. For example, you can trigger Lambda asynchronously by uploading files to Amazon S3, invoke Lambda with an Amazon API Gateway REST request synchronously, or do it by data modification made in DynamoDB tables with polling services. In these cases, Amazon S3, Amazon API Gateway and DynamoDB are triggers.

Destinations

Destinations are AWS resources that receive a record of an invocation after it succeeds or fails. If you want to set up a destination, you should create a role and attach related permissions to authorize Lambda function to access AWS resources. For example, if you want to store the result of Lambda into Amazon S3 bucket, you need to attach the related permissions such as AmazonS3FullAccess to your role, otherwise you will get a permission denied error.

attach permissions for resources

How to Use It?

1. Create a function

Create function

You can create a Lambda function by authoring from scratch, using a blueprint or browsing serverless app repository. If you want to get started quickly, I recommend you to choose ‘use a blueprint’. Further, to manage dependencies, you can add a Lambda layer, a ZIP archive that contains libraries.

2. Add a Trigger / Destination

configuration of AWS Lambda

You can use the function designer to add triggers, and destinations to your function or adding them while creating Lambda function. In addition, you can use Amazon Web Services (AWS) SDK such as boto3 to create, configure, and manage triggers and destinations.

3. Create a Test

create a test

After adding a function and setting up triggers and destinations, you can try it by creating a test to define the event. After that, click test button to trigger AWS Lambda and you can get the results.

4. View Logs

Monitoring

According to the AWS documentation, a Lambda function comes with a CloudWatch Logs log group, with a log stream for each instance of a function. The runtime sends details about each invocation to the log stream, and relays logs and other output from your function’s code. You can click monitoring to see the CloudWatch metrics and click ‘Views logs in CloudWatch’ for more details.

About Pricing

You are charged based on the number of requests for your functions and the duration, the time it takes for your code to execute. The price for duration depends on the amount of memory you allocate to your function. To manage you cost, You can set the timeout and memory in the UI. For more information about pricing, you can check the documentation.

setting memory and timeout, source: Zoe

Pros and Cons

With AWS Lambda, you can focus on coding and shift the responsibility of maintaining instance to AWS, and you only pay for the compute time it consumes. Therefore, it cost you less human resources and money than the traditional cloud in some cases. However, the learning curve is steep and it is vendor lock-in. Moreover, it has startup latency, and it is not suitable for long-term tasks.

Consequently, if you want to use AWS Lambda, I recommend you to assess suitability before diving into it.

--

--