☰

Get Started With AWS Lambda

AWS Lambda: Get Started with Serverless Computing

Currently over 1.5 million customers utilize AWS Lambda. Each month, these customers cause tens of trillions of AWS Lambda function invocations. It is clear that many businesses are now adopting serverless. As a newcomer to AWS Lambda, it is one of the quickest ways to gain insight into how current cloud-based software is developed.

 

⚡ QUICK ANSWER

AWS Lambda is a serverless, pay-per-occurrence, compute service. Firstly, create an AWS account and log into the AWS Lambda console. From here, you can create and test an AWS Lambda function. You can then analyse the logs and metrics within Amazon CloudWatch.

Many people consider serverless to be an intangible concept before successfully deploying and observing your first AWS Lambda function. We will guide you through doing precisely this and show how to analyse logs and metrics. We will also show you how the AWS free tier and Amazon CloudWatch service can be utilised.

 

In this article, you will learn:

  1. What Is AWS Lambda and Why Serverless Computing Matters
  2. How to Get Started with AWS Lambda
  3. Run a Serverless “Hello, World!” or “Hello Prwatech” With AWS Lambda
  4. Monitor Your AWS Lambda Function With Amazon CloudWatch
  5. Clean Up Your AWS Lambda Function
  6. Real-World Examples of AWS Lambda in Action

 

What Is AWS Lambda and Why Serverless Computing Matters

AWS Lambda only executes code in response to certain events (for example, data being uploaded to Amazon S3 or an API call). You define the amount of memory and time-out period that should be allocated to your code. AWS then manages the underlying servers. Despite its name, servers are still employed. However, as a user you will never manage, configure or scale them. The AWS free tier includes 1 million function invocations and 400,000GB-seconds of processing power, so it is extremely cost effective to practise.

 

Contrasting traditional and AWS Lambda server management:

Aspect Traditional server (such as EC2) Lambda function
Setup
  • You launch and configure the instance          
  • You upload code and set memory and timeout
Scaling
  • You add capacity yourself
  • Runs once per trigger and scales with the workload
Billing
  • Charged while the instance runs
  • Charged per request and run time

 

How to Get Started with AWS Lambda

Getting Started with AWS Lambda, Are you searching for the ideal platform to gain knowledge on how to get started with AWS Lambda? Or are you eagerly awaiting to undertake the advanced Certification Course at India’s Leading AWS Training institute? In which case, you are on the Right Path.

 

Below is a Tutorial that will allow you to Understand in depth how to get started with AWS Lambda. Simply follow all the Tutorials provided by India’s Leading Best AWS Training institute and become a Pro AWS Developer.

 

First, create a Lambda function through the AWS Lambda console. Within a short period of time, you will then be able to both create and test a Lambda function. Further, you will be able to analyse logs, metrics and tracing data.

 

In order to utilise AWS Lambda and further AWS services, you will require an AWS account. If you do not currently possess one, please visit amazon.com and click ‘Create an AWS Account’.

 

As a best practice, further create an AWS Identity and Access Management (IAM) User with administrative privileges. Always use this user, rather than the root account, for all non-root related activity.

Run a Serverless “Hello, World!” or “Hello Prwatech” With AWS Lambda

Below we outline how to establish and execute a basic serverless function on AWS Lambda. We will also demonstrate how to create a ‘Hello World’ function via the AWS Lambda console.

AWS Lambda is free tier enabled.

 

Step 1: Access AWS Lambda Console

Open the AWS Management Console. Under the Compute category, click the AWS Lambda Console link.

Blueprints contain example code and can perform basic analysis. Typically a blueprint is used to analyse data from specific event sources. These may be AWS services such as Amazon S3 and Amazon DynamoDB, or even a self-developed application.

 

Step 2: Choose a Lambda Blueprint

  1. a) Within the AWS Lambda console, click “How a Lambda function runs”.

Note: This screen is only displayed if no functions have previously been manually created. Should you already have created functions, you will instead be shown the Lambda > Functions page. Click ‘Create a function’ to move to the Create function page and then select blueprints.

AWS Lambda dashboard showing how a Lambda function works, including event source, Lambda function execution, output, and the Create a Function option.

 

  1. b) Click Blueprints.
  2. c) Within the Filter box, enter ‘hello-world-python’ and click on the relevant blueprint of the same name.
  3. d) Click Configure.

AWS Lambda Create function page showing the Use a blueprint option and hello-world-python blueprint selection.

 

Step 3: Configure and create your AWS Lambda Function

Alongside your code and supporting libraries, AWS Lambda functions can be configured. You can define the amount of processing power (such as memory) to allocate to your function, as well as the time-out and an IAM role. AWS Lambda uses this IAM role to execute your function on your behalf.

  1. a) Now input Basic Information for your function.

 

Basic Information:

  • Function Name:You can name your Lambda function here. For this tutorial, enter hello-world-python.

 

Execution Role: You will create an IAM role (referred to as the execution role) with the necessary permissions that AWS Lambda can assume to invoke your Lambda function on your behalf. Select Create a new role from template(s).

 

Role name:Type into the text box lambda_basic_execution.

AWS Lambda basic information settings showing function name, execution role selection and IAM role name configuration.

 

Lambda Function Code:

  • Within this area, you can examine the example Python code.

AWS Lambda Python function code showing JSON import, Lambda handler definition, event processing and return statement.

 

  1. b) Scroll to the bottom of the page and click ‘Create Function’.

AWS Lambda function creation page showing the sample Python code section and highlighted Create function button.

c)Runtime:At present, AWS Lambda allows you to programme functions in Java, Node.js, C#, Go and Python. For this tutorial we will continue to use Python 3.7.

d)Handler:You can identify which method/function within your code which should be executed by AWS Lambda. AWS Lambda provides event information to the handler for processing.

 

Within our example, AWS Lambda Function recognises this information and will pre-populate this as lambda_function.lambda_handler.

AWS Lambda function configuration showing Python 3.7 runtime and lambda_function.lambda_handler handler settings.

 

e)Further scroll down and then set memory, timeout and VPC parameters. For this tutorial, we will continue to use the default function settings.

AWS Lambda basic settings showing memory allocation, function timeout and VPC network configuration.

 

Step 4: Execute and analyse your Lambda function

Having accessed your hello-world-python function within the console, you can now test and analyse it.

  1. a) Click on the ‘Configure Test Event’ option from the ‘Select a test event…’ dropdown menu.

AWS Lambda Test tab showing the Configure test event option in the test event dropdown menu.

  1. b) A test-event editor appears so that you can enter test event information.

 

♦ Click on the ‘Hello World’ option from the list of sample test-event templates on the Input test event page.

Enter an event title such as

♦ Although you can alter values within the sample test-event JSON document, please do not modify its format. For this tutorial, amend value1 to say hello, world!.

Click Create.

AWS Lambda Configure Test Event window showing Hello World template, event name, JSON payload and Create button.

 

  1. c) Click Test.

Cloud function management console showing a saved HelloWorldEvent test event and the Test button for invoking a Python-based function.

  1. d) Upon successful function execution, analyse the results shown within the console:

♦ The ‘Execution results’ section demonstrates that the function was successfully executed.

Within the ‘Summary’ section, you can analyse key information from the Log output.

 

♦ The Log output section enables you to analyse the logs relating to the function execution.

Cloud function execution results showing the function summary, execution duration, memory usage and log output.

 

Monitor Your AWS Lambda Function With Amazon CloudWatch

 

Step 5: Analyse metrics

AWS Lambda automatically analyses and reports metrics for your functions using Amazon CloudWatch. AWS Lambda automatically analyses how many times your functions are invoked, as well as analysing request-latency and error-occurrences. Corresponding metrics are then published.

Cloud function test event showing successful function execution with multiple test runs and their respective execution times.

  1. a) Repeatedly click on the Test button to further execute your Lambda function. Subsequently, you will be able to analyse the ensuing metrics as explained in step 6.
  2. b) Click on the Monitoring tab to analyse results.
    Cloud function monitoring dashboard displaying performance metrics for function invocations, execution duration, error count, success rate and throttles.
  3. c) Scroll down to analyse the metrics in relation to your AWS Lambda function. As mentioned, AWS Lambda metrics are analysed using Amazon CloudWatch. These can furthermore be exploited to construct bespoke alerts.
  4. d) Within the Monitoring tab you can analyse the following 6 CloudWatch metrics: Number of invocations; Duration of invocations; Number of invocation errors; Number of throttled invocations; Age of iterator and; DLQ errors.
  5. e) AWS Lambda operates on a pay-for-what-you-use basis. Once you surpass the AWS Lambda free-tier limits, you are charged according to the number of function invocations and the length of time that your code is executed for. For further details, please refer to the AWS Lambda Pricing page.

Clean Up Your AWS Lambda Function

 

Step 6: Delete the Lambda Function

Although there is no charge for maintaining your AWS Lambda function, it can easily be deleted using the AWS Lambda console. a) Click on Actions and then click Delete Function.

Cloud function management console showing the Actions menu with the Delete function option highlighted in the Code tab.

Ahead of deleting your function, you can again edit the sample test event JSON. However, do not modify its format. For this tutorial, alter value1 to read Hello, Prwatech! .

For this, first click on select a test event and then click on configure event

Cloud function management console showing the Test tab with the Configure test event option for a Python-based function.

 

Then amend value1 to ‘Hello Prwatech’ as shown below so that your function can output the desired result.

Cloud function management console showing the Configure test event dialog with the Hello World event template, event name HelloWorldEvent, and a JSON test event containing the highlighted "key1": "hello, world!" value.

Configure a Hello World test event with a JSON payload containing the message "Hello, Prwatech!" in the Lambda console.

When ready to delete your function, click on Actions and then click Delete

Cloud function management console showing the Actions dropdown menu for the hello-world-python function, with the Delete function option highlighted in red. The Test tab displays a successful execution result for the HelloWorldEvent test event.

 

b) Confirm that you wish to delete your function by clicking on Delete function.

A confirmation dialog appears in the cloud function management console asking the user to confirm deletion of the hello-world-python function, with Cancel and Delete function buttons.

 

Congratulations!

 

You have successfully created your initial AWS Lambda function. This has marked your first foray into developing software without having to manage or maintain servers. AWS Lambda also effortlessly scales your software. It does this by executing your code in response to events and scaling accordingly to the extent of your workloads.

 

I trust that you have now learnt how to create AWS Lambda functions and recognise how AWS Lambda facilitates code execution, without requiring consideration of servers.

 

#Remember to seek assistance where required!

 

Real-World Examples of AWS Lambda in Action

Although the console-based tutorial is relatively basic, the same components are used within live environments. Two Coca-Cola examples are provided below.

Coca-Cola Freestyle.

The Coca-Cola team developed a mobile, hands-free dispensing function on their serverless architecture. They used AWS Lambda and Amazon API Gateway for the backend. According to the AWS case study, a prototype was developed within a week and the solution was deployed to 10,000 machines within just 150 days.

 

Coca-Cola Vending Pass.

After a customer swiped their phone at a vending machine, Amazon SNS topics and AWS Lambda functions were used to call backend code which added points to the customer’s record. Subsequently, AWS Step Functions was integrated so as to further optimise the design. Again, it is notable that often a single AWS Lambda function will evolve into a workflow comprising multiple functions.

 

Conclusion

You have now learnt about AWS Lambda and how to both develop and test functions within the AWS Lambda console. In addition, you have recognised where to analyse metrics for your functions (namely, within Amazon CloudWatch). Going forward, you should attach functions to genuine event sources, for example, S3 uploads and API calls. Then, observe the logs to ascertain what occurred. Incremental, iterative testing is highly valued within serverless environments.

 

Frequently Asked Questions

 

1. What is AWS Lambda used for?

AWS Lambda analyses and processes data in response to events. It is thus suitable for analysing data uploads, operating back-end APIs, and for stream and scheduled automation. You upload code and then define what events should activate it. AWS executes your code and charges you solely for the number of function invocations and for the period of time that your code is executed for.

2. Is AWS Lambda free to use?

AWS Lambda offers a free tier, allowing 1 million function invocations and 400,000GB-seconds of processing power to be utilised each month. Additional usage is charged for on a per-invocation and per-GB-second basis. Furthermore, there is a separate cost for sending logs to Amazon CloudWatch. Before undertaking extensive testing therefore, please consult the pricing page.

 

3. What is an AWS Lambda function?

An AWS Lambda function comprises both code and settings. Settings may include: memory size; timeout and an IAM role. AWS will consequently create an environment, supply event data to your function and run your code. Results can then be outputted and logging can occur. You are billed for the time that your function runs for.

 

4. How does serverless computing differ from using EC2?

With EC2 you can rent virtual servers. You can configure these and also control when they are updated. With serverless, AWS controls the servers which automatically adjust depending on the situation. EC2 is good for hosting services that are constant or require long term availability. Lambda is great for conducting short, irregular tasks, meaning that there is no waste of funds due to poor usage.

5. Which languages does Lambda support?

Lambda currently allows code to be run in Python, Node.js, Java, .NET and Ruby. Further languages can also be supported either by creating a custom runtime or using a custom container image. Unsupported languages are discontinued over time. Thus, it is advised that you check which runtimes AWS still support prior to beginning a new project.

6. How long can a Lambda function run?

Whilst 15 minutes is the maximum time limit, by default this will timeout after only 3 seconds. Set this value marginally above your requirement. Subsequent work can also either be broken down (such as in AWS Step Functions) or moved on to a container based service.

7. How do I monitor Lambda functions with Amazon CloudWatch?

Logs can also be sent to Amazon CloudWatch. It is then possible to view logs and analyse errors and throttling. You can then create alerting for monitoring. Logs can be analysed using the CloudWatch Logs Insights feature. A log group can be created and a log retention period set so that excessive logging is avoided and therefore unnecessary costs are not generated. 

8. What is a cold start in Lambda?

As well as launching and configuring the Lambda environment, there is an added delay for Lambda to fetch and configure your code. Warm requests do not have this issue. Smaller code and dependency files alongside accurately configured memory will also aid performance. Configure provisioned concurrency pre-launches environments so that they can be used immediately, providing fast performance.

Popular Tags:

AWS Lambda Tutorial for Beginners Building Your First AWS Lambda Function Create a Serverless Function with AWS Lambda Get Started with AWS Lambda Run a Serverless "Hello World!" with AWS Lambda