Aws Lambda Tutorial
Aws Lambda Tutorial
Aws Lambda Tutorial
i
AWS Lambda
Audience
This tutorial is designed for software programmers who want to learn the basics of AWS
Lambda and its programming concepts in simple and easy way. This tutorial will give you
enough understanding on various functionalities of AWS Services to be used with AWS
Lambda with illustrative examples.
Prerequisites
To work with AWS Lambda, you need a login in AWS. The details on how to get free login is
discussed in tutorial. AWS Lambda supports languages like NodeJS, Java, Python, C# and Go.
If you are novice to any of these technologies, we suggest you to go through tutorials related
to these before proceeding with this tutorial.
Copyright &Disclaimer
© Copyright 2018 by Tutorials Point (I) Pvt. Ltd.
All the content and graphics published in this e-book are the property of Tutorials Point (I)
Pvt. Ltd. The user of this e-book is prohibited to reuse, retain, copy, distribute or republish
any contents or a part of contents of this e-book in any manner without written consent of
the publisher.
We strive to update the contents of our website and tutorials as timely and as precisely as
possible, however, the contents may contain inaccuracies or errors. Tutorials Point (I) Pvt.
Ltd. provides no guarantee regarding the accuracy, timeliness or completeness of our website
or its contents including this tutorial. If you discover any errors on our website or in this
tutorial, please notify us at contact@tutorialspoint.com
i
AWS Lambda
Table of Contents
About the Tutorial ...................................................................................................................................... i
Audience..................................................................................................................................................... i
Prerequisites ............................................................................................................................................... i
Table of Contents....................................................................................................................................... ii
ii
AWS Lambda
Configuration ........................................................................................................................................... 42
Monitoring............................................................................................................................................... 50
iii
AWS Lambda
Installing Go ............................................................................................................................................. 90
11. AWS LAMBDA — CREATING AND DEPLOYING USING AWS CONSOLE ................................ 128
iv
AWS Lambda
12. AWS LAMBDA — CREATING AND DEPLOYING USING AWS CLI........................................... 140
13. AWS LAMBDA — CREATING AND DEPLOYING USING SERVERLESS FRAMEWORK .............. 163
Using API Gateway and AWS Lambda with Serverless Framework ........................................................ 181
14. AWS LAMBDA — EXECUTING AND INVOKING LAMBDA FUNCTION ................................... 190
v
AWS Lambda
16. AWS LAMBDA — WORKING WITH AMAZON API GATEWAY ............................................... 217
17. AWS LAMBDA — USING LAMBDA FUNCTION WITH AMAZON S3 ...................................... 244
Steps for Using AWS Lambda Function with Amazon S3 ........................................................................ 244
18. AWS LAMBDA — USING LAMBDA FUNCTION WITH AMAZON DYNAMODB ....................... 263
Creating Role with Permissions to Work with DynamoDB and AWS Lambda ......................................... 269
vi
AWS Lambda
19. AWS LAMBDA — USING LAMBDA FUNCTION WITH SCHEDULED EVENTS.......................... 280
Create Role to use AWS SES, Cloudwatch and AWS Lambda .................................................................. 283
20. AWS LAMBDA — USING LAMBDA FUNCTION WITH AMAZON SNS .................................... 289
Add Code in AWS Lambda to Send Message to your Phone ................................................................... 297
21. AWS LAMBDA — USING LAMBDA FUNCTION WITH CLOUDTRAIL ...................................... 301
Create a Trail in Cloudtrail and Assign the S3 bucket and SNS service .................................................... 303
22. AWS LAMBDA — USING LAMBDA FUNCTION WITH AMAZON KINESIS ............................. 309
vii
AWS Lambda
23. AWS LAMBDA — USING LAMBDA FUNCTION WITH CUSTOM USER APPLICATIONS ........... 318
24. AWS LAMBDA — USING AWS LAMBDA@EDGE WITH CLOUDFRONT ................................. 324
25. AWS LAMBDA — MONITORING AND TROUBLESHOOTING USING CLOUDWATCH ............. 342
Create AWS Lambda and API Gateway to Send OTP Message to Phone using SNS service .................... 354
Create AWS Lambda and API Gateway to POST Form Data and Insert in DynamoDB Table ................... 357
viii
AWS Lambda
Create AWS Lambda and API Gateway to Read Data from DynamodDB Table ...................................... 366
ix
1. AWS Lambda — Overview AWS Lambda
AWS Lambda is a service which performs serverless computing, which involves computing
without any server. The code is executed based on the response of events in AWS services
such as adding/removing files in S3 bucket, updating Amazon dynamo dB tables, HTTP
request from Amazon API gateway etc.
To get working with AWS Lambda, we just have to push the code in AWS Lambda service.
All other tasks and resources such as infrastructure, operating system, maintenance of server,
code monitoring, logs and security is taken care by AWS.
AWS Lambda supports languages such as Java, NodeJS, Python, C# and Go. Note that AWS
Lambda will work only with AWS services.
AWS Lambda is a compute service that lets you run code without provisioning or managing
servers. AWS Lambda executes your code only when needed and scales automatically, from
a few requests per day to thousands per second. You pay only for the compute time you
consume - there is no charge when your code is not running.
1
AWS Lambda
Step 1: Upload AWS lambda code in any of languages AWS lambda supports, that is NodeJS,
Java, Python , C# and Go.
Step 2: These are few AWS services on which AWS lambda can be triggered.
Step 3: AWS Lambda which has the upload code and the event details on which the trigger
has occurred. For example, event from Amazon S3, Amazon API Gateway, Dynamo dB,
Amazon SNS, Amazon Kinesis, CloudFront, Amazon SES, CloudTrail , mobile app etc.
Step 4: Executes AWS Lambda Code only when triggered by AWS services under the
scenarios such as:
Step 5 : Remember that AWS charges only when the AWS lambda code executes, and not
otherwise.
AWS Lambda can also execute parallel requests as per the event triggers.
Log Provision
AWS Lambda gives the details of number of times a code was executed and time taken for
execution, the memory consumed etc. AWS CloudWatch collects all the logs, which helps in
understanding the execution flow and in the debugging of the code.
2
AWS Lambda
Other features
You can use AWS Lambda for free by getting a login to AWS free tier. It gives you service for
free for 1 year. Take a look at the free services offered by AWS free tier.
You need to carefully analyze your code and decide the memory and timeout. Incase
if your function needs more time than what is allocated, it will get terminated as per
the timeout specified on it and the code will not be fully executed.
Since AWS Lambda relies completely on AWS for the infrastructure, you cannot install
anything additional software if your code demands it.
3
AWS Lambda
4
AWS Lambda
5
2. AWS Lambda — Environment Setup AWS Lambda
Before you start working with AWS Lambda, you need to have a login with Amazon console.
AWS Lambda supports two IDEs: Visual studio and Eclipse. In this chapter, we will discuss
about the installation of AWS Lambda stepwise in detail.
Step 1
Go to https://aws.amazon.com/free/ and click on create free account. You can see the
screenshot as given below:
6
AWS Lambda
Step 2
Click on Create a Free Account button and you will be redirected to the screen as shown
below:
Now, fill in the details of email address, password and AWS account name as per your choice
in this form shown above and click Continue.
7
AWS Lambda
Step 3
Now, you can find the screen as shown below:
Note that there are minimum charges to be paid based on country selected. The same is
refunded once the details entered are validated. You need credit or debit card details to create
the free account. For Indian users Rs 2/- is deducted and for US $1 is charged.The same is
refunded to the respective card user once the user is validated.
Please note the account is free and there is limit to the usage of the services. If the usage
exceeds the limit, the user will be charged for it.
Once the details are entered in the form shown above click Create Account and Continue.
8
AWS Lambda
Step 4
You need to enter the payment details, that is either credit card or debit card, along with its
expiry date and the card holder's name as shown below:
9
AWS Lambda
Step 5
Once all the details are entered, click Secure Submit and it will validate the card with the
bank and will give you the OTP on your mobile which is linked with the card. You can find a
window as shown below:
Now, enter the OTP details and click Make Payment.You are charged based on the country
selected.
10
AWS Lambda
Step 6
Once the payment is done the next step is phone verification.You need to enter your mobile
number as shown below:
Once details are filled click Call Me Now. AWS will call immediately using automated system.
When prompted on call, enter the 4-digit number that will appear on your AWS site to your
phone using your phone keypad. This will verify your number and you will get the mail
activation in the mail id specified at the start while creating login.
11
AWS Lambda
Step 7
Click the mail link and enter the account name or email id and the password and login to you
to the AWS services as shown below:
The account name is displayed at top right corner as shown above. You can now start using
the AWS Lambda service. For AWS Lambda service the languages supported are NodeJS,
Python, Java, C# and Go.
12
AWS Lambda
Download the community version ie Visual Studio Community 2017 as its a free now for
practice. Once installed, it will run you through the installation steps where you need to select
packages to be used later. You can select nodejs, python, c# package for us to work later.
13
AWS Lambda
Step 1
Go to https://aws.amazon.com/visualstudio/ and download the AWS toolkit for Visual Studio.
The display is as shown below:
Note that the package downloaded for Visual Studio 2017 is vsix package. If your visual
studio version is between 2013-2015, it will install a msi installer. Click the Download button
as shown below.
14
AWS Lambda
Step 2
Now, double click the vsix package downloaded and it will run you through installation steps
as shown below:
Once Visual Studio is successfully installed, you can see a window, as shown below:
15
AWS Lambda
Step 3
Now, open Visual Studio 2017 and you should see a welcome page from AWS as shown below:
Note that you need to add the access key, secret key, account number to get started and use
the AWS services from visual studio.s
16
AWS Lambda
Step 1
You can download Visual studio code for free from the official
website:https://www.visualstudio.com/downloads/. The home page of Visual Studio
downloads looks like this:
17
AWS Lambda
Step 2
Now, open Visual Studio code as shown below:
18
AWS Lambda
Step 3
To install support
for AWS, support
for nodejs
option is
available inside
extensions. You can search for AWS and it will display the option as follows:
Step 4
Now, install the boilerplate for AWS Lambda in nodejs as shown:
19
AWS Lambda
Step 5
Click the repository and clone it in Visual Studio to start writing the Lambda function in Visual
Studio. It redirects you to this repository which we can clone in Visual Studio :
https://github.com/loganarnett/vscode-lambda-snippets. Now, open command palette from
View option in Visual Studio.
Step 6
Click on it and choose git clone as shown below:
20
AWS Lambda
Step 7
Enter the repository url and save it as per your choice locally. Create index.js file as shown
below to work with lambda function:
21
AWS Lambda
22
AWS Lambda
23
AWS Lambda
Step 1
Go to help from the menu and click Install New Software.
Step 2
Enter https://aws.amazon.com/eclipse in the text box labeled Work with at the top of the
dialog.
Step 3
Now, select the required AWS Core Management Tools and other optional items from the
list shown below.
Step 4
Now, click Next. Eclipse will guide you through the remaining installation steps as given in
the further steps given below.
24
AWS Lambda
Step 5
The AWS core modules are displayed in the grid below as shown in the screenshot given
below:
25
AWS Lambda
Step 6
Once installed the AWS tool will be available in Eclipse as shown below:
26
AWS Lambda
27
AWS Lambda
Step 7
You can see the following screen when you click on the Amazon service.
Now, click on AWS Explorer to see the services available. We shall discuss how to work with
the installed IDE in upcoming chapters.
28
3. AWS Lambda — Introduction AWS Lambda
AWS Lambda is a service which takes care of computing your code without any server. It is
said to be serverless compute. The code is executed based on the response of events in AWS
services like adding /removing files in S3 bucket, updating Amazon DynamoDB tables, HTTP
request from Amazon Api gateway etc.
AWS Lambda code can be written in NodeJS, Java, C#, Python and Go. This chapter will talk
in detail about creating AWS Lambda function in AWS console.
AWS Console
Login to AWS Console at the link https://aws.amazon.com/console.Once you login into
it, it will redirect you to the screen where AWS services are displayed.
29
AWS Lambda
Click Create function button and the screen displays following details:
Note that, by default, the option is Author from scratch. This option lets you to write the
Lambda code from scratch.It will just have a simple function with helloworld message.
30
AWS Lambda
It gives details of code already written for some of the aws services in languages available
with AWS Lambda. In case you need to write AWS Lambda code for any services you can
check in blueprints and get started.
The third option Serverless Application Repository has the setup of serverless application
which will help to deploy the AWS Lambda code.
In the discussion futher, we will work on the first option where we create the AWS lambda
function using Author from scratch.
Before we create Lambda function, will need a role i.e, permission for working with AWS
services and aws lambda. Later the Role has to be assigned to aws lambda function.
31
AWS Lambda
Now, if you click IAM, you will the screen as shown below:
32
AWS Lambda
If you select Roles, you can see the following buttons on the screen:
Now, click Create role.It will ask you to choose the service where you need to use the role
created.
Since we need to use this role with AWS Lambda , select Lambda and click
Next:Permissions button as shown above. The next screen displays the policy name which
is available as per AWS services.You can select the policy from here:
For example, if you want permission for AWS Lambda to work with S3 and DynamoDB, you
need to select the policy. In the searchbox, enter the AWS service and click on the checkbox.
You can select multiple policies and later click on Next:Review.
33
AWS Lambda
It is also possible to create policy of your own.For example, there is dynamodb table and you
need to give permission only to that table, under such cases you can create policy.
Click on Create policy button as shown in the screen above.Following are the details
displayed on screen.
34
AWS Lambda
Choose a Service for which you are creating the policy. Later it will display data for Actions,
Resources and Request conditions.
35
AWS Lambda
Now, we should choose the service. Let us select AWS Dynamodb from search. Actions has
following details:
Now, enter the Access level you want to give to DynamoDB. Then, Resources will display
the following details:
36
AWS Lambda
Now, select the table resource type. You can see the following output:
For permission on table, you need to Add ARN. ARN is the details which is unique to the table
created in AWS DynamoDB. You will get the details when the table is created in dynamodb.
37
AWS Lambda
38
AWS Lambda
Now, if you enter the ARN and the Region, Account and Table name will get populated.
You should click Add button to add the policy. Similarly, you can create policies for other
services.
39
AWS Lambda
You can follow the steps discussed earlier to create the policies using ARN.
Step 1
Click Create role button to create the role. All the roles created are displayed as shown:
Step 2
Note that you can select the role you require incase you need any modification for the role
created. If we select Author from scratch option, you have to enter Name, Runtime and
Role.
40
AWS Lambda
Step 3
Step 4
You can select the runtime of your choice and proceed as shown.
41
AWS Lambda
Choose an existing role: This will display all the roles created in the IAM roles.
Create new role from template(s): This will allow you to create role and will display
permission to be selected for that role. Observe the scrrenshot for a better
understanding.
Create a custom role: This allows the user to create policies as we discussed earlier.
Step 5
Select the runtime, role and add the function. Click on Create function button to create
the lambda function. The next screen displayed is as follows:
Configuration
The following functionalities are included in the Configuration.
Add Triggers
The triggers that are needed to added to AWS Lambda function are displayed as follows:
42
AWS Lambda
Note that when we select a trigger, we need to add the configuration details for that
trigger.For example for S3 trigger, we need to select the bucket name; for Dynamodb trigger
we need to select the table name .
Example
Let us see an example of configuration details for a S3 trigger:
Here you need to select the bucket name, event type on which you want to trigger Lambda,
prefix and filter pattern if any and Add the trigger.
43
AWS Lambda
The inline code editor where you can write you code is as follows:
You can write your code by choosing the language of your choice. You are allowed to choose
the runtime again here.
44
AWS Lambda
The code has to be written in index.js.Handler. Details will differ based on runtime. For
nodejs, it is filename.exportfunction which is right now index.lambdahandler.
You can first write the code, zip it and upload the zip file by selecting Upload a .ZIP file.
You can upload the file in S3 bucket and choose the option Upload a file from Amazon S3.
Note that for .ZIP and S3 it will not be possible to change the runtime.
Environment variables
They take in key value pairs and share them with AWS Lambda code.We can use environment
variables in AWS Lambda for storing the database connection details, file details as to store
the output , log file details etc.
45
AWS Lambda
Tags
They are key-value pairs added to AWS Lambda for better organizing the function when used
across different regions. For a simple use case, it is not required. When there are lot of Lambda
functions created, the tagging helps in filtering and managing the Lambda functions.
Execution role
You can change the role again here if not done properly at the start of creating Lambda
function. You can update or create new role here. It provides same options which were
displayed at the start of creating Lambda function.
46
AWS Lambda
Basic Settings
Here you need to enter the short description of what your Lambda function is doing. Select
the memory and timeout which are required for the Lambda function.
Network
This allows you to select the VPC which will allow you to access the Lambda function from the
VPC. By default, no VPC is selected.
47
AWS Lambda
Concurrency
This allows you to allocate a specific limit of concurrent executions allowed for this function.
48
AWS Lambda
Once done you need to save the changes using the Save button as shown here:
Now, if you click Test button, it will ask for a test event.You can pass a sample test event as
follows:
Now, save the test event and click the test button to see the execution of AWS Lambda
function:
49
AWS Lambda
Note that callback function is called when there is error or success. If success, you can see
Lambda tes will get displayed.
Monitoring
Select the monitoring tab to view the execution details of Lambda function. The graphs show
the details of the execution time, errors occured etc.
50
AWS Lambda
You can also view the logs in Cloudwatch. For this, go to AWS services and select cloudwatch
as shown:
Now, select logs from left side and enter your function name in the filter:
51
4. AWS Lambda — Building the Lambda function AWS Lambda
AWS Lambda function executes a code when it is invoked. This chapter discusses all these
steps involved in the life cycle of AWS Lambda function in detail.
Authoring
Deploying
Monitoring
Troubleshooting
NodeJS
Java,
Python
C#
Go.
We can write code for AWS Lambda using the AWS console, AWS CLI, from Eclipse IDE, from
Visual Studio IDE, serverless framework etc.
52
AWS Lambda
The following table shows a list of languages and the different tools and IDE that can be used
to write the Lambda function:
.NET core
However, remember that proper permission has to given to be given to the zip file .
53
AWS Lambda
To start writing Lambda function, there is pattern to be followed. The following are the main
core concepts to be followed for writing a Lambda function:
Handler
Handler is a name of the AWS lambda function from where the execution starts. It appears in
AWS console as shown below:
Notice that here we have changed the default handler to another name and updated the same
in the Handler:
Note that the way a handler is called differs from the languages selected as runtime.
54
AWS Lambda
Event parameter has all the details for the trigger used.
Context parameter basically takes care of runtime details for the Lambda function to execute.
We can interact with the Lambda function using the context param. It has the details like
the time left before AWS Lambda terminates a function i.e, timeout specified while creating
Lambda function, name of the Lambda function, cloudwatch group name, arn details etc.
55
AWS Lambda
Example
Let us understand the details obtained from AWS Lambda context object with the help of an
example:
When you execute the Lambda function shown above, you can see the following output:
Output
{"callbackWaitsForEmptyEventLoop":true,"logGroupName":"/aws/lambda/myfirstlambdafu
nction","logStreamName":"2018/05/20/[$LATEST]04f17ee4ff7048d5bb1fedffaa807c71","fu
nctionName":"myfirstlambdafunction","memoryLimitInMB":"128","functionVersion":"$LA
TEST","invokeid":"c931e21c-5bf3-11e8-acfe-47fdbb39eee9","awsRequestId":"c931e21c-
5bf3-11e8-acfe-47fdbb39eee9","invokedFunctionArn":"arn:aws:lambda:us-east-
1:625297745038:function:myfirstlambdafunction"}
Logging
56
AWS Lambda
The logs added inside the Lambda function are displayed in AWS CloudWatch when the AWS
function executes. The logs syntax will vary from the langauge selected. For example in
nodejs, it is console.log.
Error Handling
AWS Lambda function provides a callback function which is used to notify to the Lambda
function that an error or success has happened. Note that here we have used nodejs as the
runtime. The error handling will differ as per the language selected.
57
AWS Lambda
Output
When you test the Lambda code, you can find the output as shown below:
58
5. AWS Lambda — Function in NODEJS AWS Lambda
Nodejs is one of the languages that AWS Lambda function supports. The version supported
with nodejs are v6.10 and v8.10. In this chapter, we will learn about various functionalities
of AWS Lambda function in NODEJS in detail.
Handler in NodeJS
To write AWS Lambda function in nodejs, we should first declare a handler first. The handler
in nodejs is name of the file and the name of the export function. For example, the name of
the file is index.js and the export function name is lambdahandler, so its corresponding
handler is index.lambdahandler
Params to Handler
Handler is the main core for building Lambda function. The handler takes three params:
event, context and callback.
Event Parameter
It has all the details of the event triggered. For example, if we are using Lambda function to
be triggered on S3, the event will have details of the S3 object.
Context Parameter
It has the details of the context such as the properties and configuration details of the Lambda
function.
Callback Function
It helps in giving details back to the caller. The structure of callback looks as follows:
callback(error, result);
Error: This will have details if any error has occurred during the execution of Lambda function.
If the Lambda function succeeds, null can be passed as the first param for callback function.
Result: This will give the details of the successful execution of the lambda function. If an
error occurrs, the result param is ignored.
Note: It is not mandatory to use the callback function in AWS Lambda. In case if there is no
callback function, the handler will return it as null.
59
AWS Lambda
Whenever AWS Lambda gets executed the callback details such as error or success, are logged
in AWS CloudWatch along with console messages, if any.
60
AWS Lambda
You can observe the following output after testing this code in AWS console:
Note that the output from the above code is a promise object. It does not give the count, as
the count is incremented inside a setTimeout and the function call does not wait for the
execution inside setTimeout and returns the promise object.
If we had async/await on the handler function will get exact output of from the lambda
function.
61
AWS Lambda
We have added async and await in above code. When we use await beside the function
call, the execution pauses till the promise inside the function gets resolved. Note that await
is valid only for async functions.
You can observe the following output after testing this code in AWS console:
The following tables shows the list of methods and attributes available with context object:
62
AWS Lambda
identity.cognito_identity_id
identity.cognito_identity_pool_id
client_context.client.installation_id
client_context.client.app_title
client_context.client.app_version_name
client_context.client.app_version_code
client_context.client.app_package_name
Look at the following example to get a better idea about context object:
63
AWS Lambda
You can observe the following output after testing this code in AWS console:
You can observe the following log output after testing this code in AWS console:
Logging in NodeJS
We can use console.log for logging in NodeJS.The log details can be fetched from CloudWatch
service against the Lambda function.
64
AWS Lambda
You can observe the following output after testing this code in AWS console:
65
AWS Lambda
66
AWS Lambda
{
"errorMessage": "something is wrong",
"errorType": "Error",
"stackTrace": [ "exports.handler (/var/task/index.js:2:17)" ] }
67
6. AWS Lambda Function in Java AWS Lambda
In this chapter, let us understand in detail how to create a simple AWS Lambda function in
Java in detail.
Once you are done with installation, follow the steps given here:
Step 1
Open Eclipse IDE and create a new project with AWS Lambda Java Project. Observe the
screenshot given below for better understanding:
68
AWS Lambda
Step 2
Once you select Next, it will redirect you the screen shown below:
69
AWS Lambda
Step 3
Now, a default code is created for Input Type Custom. Once you click Finish button the
project gets created as shown below:
Step 4
Now, right click your project and export it.Select Java / JAR file from the Export wizard
and click Next.
70
AWS Lambda
Step 5
Now, if you click Next, you will be prompted save the file in the destination folder which will
be asked when you click on next.
Once the file is saved, go back to AWS Console and create the AWS Lambda function for Java.
Step 6
Now, upload the .jar file that we created using the Upload button as shown in the screenshot
given below:
71
AWS Lambda
Example
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LambdaFunctionHandler implements RequestHandler<Object, String> {
@Override
public String handleRequest(Object input, Context context) {
context.getLogger().log("Input: " + input);
72
AWS Lambda
getClientContext() this will give details about the app and device
when used with aws mobile sdk .It will give
details like version name and code, client id,
title , app package name.It can be null.
73
AWS Lambda
Now, let us update the code given above and observe the output for some of the methods
listed above. Observe the example code given below for a better understanding:
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
@Override
public String handleRequest(Object input, Context context) {
context.getLogger().log("Input: " + input);
System.out.println("AWS Lambda function name: " +
context.getFunctionName());
System.out.println("Memory Allocated: " + context.getMemoryLimitInMB());
System.out.println("Time remaining in milliseconds: " +
context.getRemainingTimeInMillis());
System.out.println("Cloudwatch group name " + context.getLogGroupName());
System.out.println("AWS Lambda Request Id " + context.getAwsRequestId());
// TODO: implement your handler
return "Hello from Lambda!";
}
}
Once you run the code given above, you can find the output as given below:
74
AWS Lambda
You can observe the following output when you are viewing your log output:
The memory allocated for the Lambda function is 512MB.The time allocated is 25 seconds.
The time remaining as displayed above is 24961, which is in milliseconds. So 25000 - 24961
which equals to 39 milliseconds is used for the execution of the Lambda function. Note that
Cloudwatch group name and request id are also displayed as shown above.
Note that we have used the following command to print logs in Java:
The same is available in CloudWatch. For this ,go to AWS services, select CloudWatch
services and click Logs.
75
AWS Lambda
Now, if you select the Lambda function, it will display the logs date wise as shown below:
Logging in Java
You can also use Lambdalogger in Java to log the data. Observe the following example that
shows the same:
Example
package com.amazonaws.lambda.demo;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
import com.amazonaws.services.lambda.runtime.LambdaLogger;
public class LambdaFunctionHandler implements RequestHandler<Object, String> {
@Override
76
AWS Lambda
The code shown above will give you the following output:
77
AWS Lambda
package com.amazonaws.lambda.errorhandling;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
public class LambdaFunctionHandler implements RequestHandler<Object, String> {
@Override
public String handleRequest(Object input, Context context) {
throw new RuntimeException("Error from aws lambda");
} }
Note that the error details are displayed in json format with errorMessage Error from AWS
Lambda. Also, the ErrorType and stackTrace gives more details about the error.
78
AWS Lambda
The output and the corresponding log output of the code given above will be as shownin the
following screenshots given below:
79
7. AWS Lambda — Function in Python AWS Lambda
In this chapter, we will create a simple AWS Lambda function in Python and understand its
working concepts following detail.
Before proceeding to work on creating a Lambda function in AWS, we need AWS toolkit
support for Python. For this purpose, follow the steps given below and observe the
corresponding screesnshots attached:
Step 1
Login to AWS console and create Lambda function and select the language as Python.
Step 2
80
AWS Lambda
Now, click Create function button and enter the details for creating a simple AWS Lambda
in Python. This code returns the message Hello from Lambda using Python and looks as
shown here:
Step 3
Now, save the changes and the test the code to see the output. You should see the following
output and logs when you test it in AWS console using the test button from the UI.
Step 4
81
AWS Lambda
Now, you can write code inside any editor or an IDE for Python. Here, we are using visual
studio code for writing the code. You should later zip the file and upload in AWS console.
Step 5
82
AWS Lambda
Once the upload is done and changes are saved, it actually shows the details of the zip file in
the online editor in AWS Lambda console. Now, let us test the code to see the output and
logs.
Now, let us understand the details of the Lambda function using the following sample code:
In the above code, the function name my_handler is having 2 params, event and context.
The methods and attributes available on context object are shown in the tables given below:
83
AWS Lambda
identity.cognito_identity_id
identity.cognito_identity_pool_id
client_context.client.installation_id
client_context.client.app_title
client_context.client.app_version_name
client_context.client.app_version_code
client_context.client.app_package_name
Let us see a working example in Python which outputs the context details. Observe the code
given below:
84
AWS Lambda
85
AWS Lambda
86
AWS Lambda
\ Observe the following example to understand about using logger to print logs to
CloudWatch:
import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)
def my_handler(event, context):
logger.info('Using logger to print messages to cloudwatch logs')
return "aws lambda in python using zip file"
87
AWS Lambda
The output for this will be as shown in the screenshot given below:
88
AWS Lambda
89
8. AWS Lambda — Function in Go AWS Lambda
Go Language support is a recent addition to AWS . To work with Go, you need to select the
language from AWS console while creating the AWS Lambda function. In this chapter, let us
learn in detail about AWS Lambda function in Go language.
Installing Go
To get started we need Go Language support. In this section, we will go through following
details to start working with AWS Lambda in Go. This is the official site for Go download:
https://golang.org/dl/
Now, download the package as per the operating system. Follow the procedure given here to
install Go on the respective operating system.
Installation on Windows
Observe that for Windows, there is 32-bit and 64-bit download available. Download the zip
file and extract the contents and store it in a directory of your choice.
90
AWS Lambda
Add the environment variables available at ControlPanel ---> System ---> Advanced
system settings.
91
AWS Lambda
Now, click Environment Variables button and add the directory path as shown here:
92
AWS Lambda
Once these steps are done, you should be able to start working with Go. Open command
prompt and check the Go command for version. Observe the following screenshot for the
same.
Unpack the packages and store it at the location /usr/local/go. Now, add
/usr/local/go/bin to the PATH environment variable. It can be done using /etc/profile or
$HOME/.profile.
export PATH=$PATH:/usr/local/go/bin
To add AWS support to for Windows, Linux and mac, use the following in your git command
line:
93
AWS Lambda
Main.go
// main.go
package main
import (
"github.com/aws/aws-lambda-go/lambda"
)
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(hello)
}
Note that the execution of the Go program starts from main where lambda.start is called
with the handler function. Observe the code shown below:
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(hello)
}
Now, let us execute the above file using Go command and then zip the executable file.
94
AWS Lambda
With go build, it creates an executable file called main.exe. To zip the file and upload it in
AWS Lambda, you can use the following procedure:
95
AWS Lambda
Then, login into AWS console and create Lambda function using Go as runtime :
Once the function is created, upload the executable zip file created above.
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(hello)
}
96
AWS Lambda
Now, save the function and test it. You can see the execution result as shown here.
97
AWS Lambda
AwsRequestID
This is AWS request id which you get when AWS Lambda function is invoked.
ClientContext
This contains details about the client application and device when invoked through the AWS
Mobile SDK. It can be null. Client context provides details like client ID, application title,
version name, version code, and the application package name.
InvokedFunctionArn
The ARN of the function invoked. An unqualified ARN executes the $LATEST version and
aliases execute the function version it is pointing to.
Identity
It gives details about the Amazon Cognito identity provider when used with AWS mobile SDK.
98
AWS Lambda
// main.go
package main
import (
"context"
"log"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-lambda-go/lambdacontext"
)
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(hello)
}
99
AWS Lambda
We need to import the log and lambdacontext to use it with Go. The context details are as
follows:
You can observe the following output on testing the above code:
100
AWS Lambda
Logging data
With Go you can log data using the log or fmt module as shown below:
// main.go
package main
import (
"log"
"fmt"
"github.com/aws/aws-lambda-go/lambda"
)
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(hello)
}
101
AWS Lambda
102
AWS Lambda
Function Errors
You can create custom error handling in AWS Lambda using the errors module as shown in
the code below:
// main.go
package main
import (
"errors"
"github.com/aws/aws-lambda-go/lambda"
)
func main() {
// Make the handler available for Remote Procedure Call by AWS Lambda
lambda.Start(hello)
}
103
AWS Lambda
104
9. AWS Lambda — Function in C# AWS Lambda
This chapter will explain you how to work with AWS Lambda function in C# in detail. Here,
we are going to use visual studio to write and deploy the code to AWS Lambda. For any
information and help regarding installation of Visual studio and adding AWS toolkit to Visual
Studio, please refer to the Introduction chapter in this tutorial. Once you are done with
installation of Visual Studio, please follow the steps given below.Refer to the respective
screenshots for a better understanding:
Step 1
Open your Visual Studio and follow the steps to create new project. Click on File -> New ->
Project.
105
AWS Lambda
Step 2
Now, the following screen is displayed wherein you select AWS Lambda for Visual C#.
Select AWS Lambda Project (.NET Core).
You can change the name if required, will keep here the default name. Click OK to continue.
106
AWS Lambda
Select Empty function for this example and click Finish. It will create a new project
structure as shown below:
Now, select Function.cs which is the main file where the handler with event and context is
created for AWS Lambda.
107
AWS Lambda
You can use the command given below to serialize the input and output parameters to AWS
Lambda function.
[assembly:
LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
108
AWS Lambda
String input: The parameters to the handler string input has all the event data such as S3
object, API gateway details etc.
The Lambda handler can be invoked in sync and async way. If invoked in a sync way as shown
above you can have the return type. If async than the return type has to be void.
Now, let us deploy the AWS Lambda C# and test the same. Right click the project and click
Publish to AWS Lambda as shown below:
109
AWS Lambda
Fill up the Function Name and click on Next. The next screen displayed is the Advanced
Function Details as shown:
110
AWS Lambda
Enter the Role Name, Memory and Timeout details. Note that here we have selected the
existing role created and used memory as 128MB and timeout as 10 seconds. Once done click
Upload to publish to AWS Lambda console.
You can see the following screen once AWS Lambda function is uploaded. Click Invoke to
execute the AWS Lambda function created. At present, it shows error as it needs some input
as per the code written.
111
AWS Lambda
Now, let us enter some sample input and Invoke it again. Note that here we have entered
some text in the input box and the same on clicking invoke is displayed in uppercase in the
response section.The log output is displayed below:
112
AWS Lambda
Now, let us also check AWS console to see if the function is created as we have deployed the
function from Visual Studio.
The Lambda function created above is awslambdausingcsharp and the same is displayed
in AWS console as shown in the screenshots given below:
113
AWS Lambda
114
AWS Lambda
Handler Signature
Handler is start point for AWS to execute. The name of the handler should be defined as:
ASSEMBLY::TYPE::METHOD
ASSEMBLY:This is the name of the .NET assembly for the application created. It is basically
the name of the folder from where the project is created.
115
AWS Lambda
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
namespace AWSLambda3
{
public class Function
{
/// <summary>
/// A simple function that takes a string and does a ToUpper
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public string FunctionHandler(string input, ILambdaContext context)
{
return input?.ToUpper();
}
}}
116
AWS Lambda
Context object in C#
Context Object gives useful information about the runtime in AWS environment. The
properties available in the context object are as shown in the following table:
Properties Description
117
AWS Lambda
Example
In this section, let us test some of the above properties in AWS Lambda in C#. Observe the
sample code given below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Amazon.Lambda.Core;
// Assembly attribute to enable the Lambda function's JSON input to be converted
into a .NET class.
[assembly:
LambdaSerializer(typeof(Amazon.Lambda.Serialization.Json.JsonSerializer))]
namespace AWSLambda6
{
public class Function
{
/// <summary>
/// </summary>
/// <param name="input"></param>
/// <param name="context"></param>
/// <returns></returns>
public void FunctionHandler(ILambdaContext context)
{
118
AWS Lambda
The related output that you can observe when you invoke the above code in C# is as shown
below:
The related output that you can observe when you invoke the above code in AWS Console
is as shown below:
119
AWS Lambda
Logging using C#
For logging, you can use two functions:
context.Logger.Log
LambdaLogger.Log
Observe the following example shown here:
120
AWS Lambda
Example
namespace Example {
public class AccountAlreadyExistsException : Exception {
public AccountAlreadyExistsException(String message) :
base(message) {
}
}
}
namespace Example {
public class Handler {
121
AWS Lambda
The corresponding output for the code given above is as given below:
"errorType": "LambdaException",
"errorMessage": "Error in AWS Lambda!"
}
122
10. AWS Lambda — Configuring Lambda Function AWS Lambda
In the previous chapters, we have learnt how to create AWS Lambda function in AWS console.
However, there are other parameters for creating a Lambda function. These include memory
allocation, timeout etc.
In this chapter, let us understand in detail about the following configuration properties for
AWS Lambda.
Memory Allocation
Login to AWS console and create or select the existing lambda function. Click the
Configuration tab to get the details of the memory allocated. Look at the screenshot shown
below:
Note that by default the memory allocated is 128MB. If you want to increase the memory
you can click the slider.
123
AWS Lambda
The memory will get incremented to 64MB as you move the slider. Observe that the
maximum memory available is 3008MB. Look at the screenshot shown below:
You can also use aws cli from command prompt to increase the memory limit. You will have
to give the memory in increments of 64MB.
Now, let us increase the memory limit of AWS Lambda with name :myfirstlambdafunction.
The memory details of the function are shown in the screenshot given below:
124
AWS Lambda
The command used to change the memory using aws cli is as follows:
125
AWS Lambda
IAM Role
When creating AWS Lambda function, the role or the permission needs to be assigned. Incase
you need AWS Lambda for S3 or dynamoDB, permission with regard to the services of lambda
needs to be assigned. Based on the role assigned, AWS Lambda will decide the steps to be
taken. For example if you give full access of dynamodb, you can add, update and delete the
rows from the dynamodb table .
Handler Name
This is the start of execution of the AWS Lambda function. Handler function has the details of
the event triggered, context object and the callback which has to send back on success or
error of AWS Lambda.
Step 1
Step 2
126
AWS Lambda
Step 3
Step 4
To get the details from environment variables we need to use process.env as shown. Note
that this syntax is for NodeJS runtime.
Step 5
127
11. AWS Lambda — Creating and Deploying using AWS Lambda
AWS Console
We can create Lambda function and test the same in AWS console. This chapter discusses
this in detail. Forthis purpose, you will have to follow the steps given here and observe the
respective screenshots given:
Step 1
128
AWS Lambda
Step 2
Now, click on Lambda service as highlighted above. This will redirect to create function as
shown below:
Step 3Now, click Create function and enter the details of the function. Then you can see a
Step 4
You can write your code by choosing the language of your choice. The code has to be written
in editor if the option selected is edit code inline. The other options available are as follows:
Step 5
Once done you need to save the changes for which the button is given at the top right corner
as shown below:
129
AWS Lambda
Step 6
Now, click Test button. This gives all details of the execution of the Lambda function as shown
below:
Step 7
This will call the Callback function and the result can be error or success. On success you
will see a Lambda test message; if error it will pass null.
Step 8
130
AWS Lambda
The Role details for Lambda function is a part of the configuration and is displayed as shown
below:
Step 9
Now, you can update the role if required and save the Lambda function. Then, the memory
and timeout details for lambda function are displayed as shown below:
Step 10
131
AWS Lambda
Now, we need to add trigger to the Lambda function so that it executes when the event
occurs. The trigger details are displayed at the start of the AWS Lambda function screen as
shown below:
From this, you can select the trigger you want your Lambda function to get triggered. When
you select the trigger, the config details for the trigger has to be added.
For example, for trigger on S3 the config details to be added are as follows:
Step 11
132
AWS Lambda
Now, select the bucket you want the trigger on. The event type has the following details:
Step 11
For the trigger, you can also mention the prefix type files or file pattern, the Lambda has to
be trigger. The details are as shown:
Step 12
133
AWS Lambda
Now, fill up the required details for the trigger and click Add button .Save the Lambda function
for the trigger to get added. Saving the function deploys the details, and from now onwards
anytime files are added to the S3 bucket, the Lambda will get triggered.
Observe the following screenshot which shows S3 trigger added to AWS Lambda:
Step 13
Now, let us use S3 sample event to test the Lambda function. The code for the same is shown
here:
{
"Records": [
{
"eventVersion": "2.0",
"eventTime": "1970-01-01T00:00:00.000Z",
"requestParameters": {
"sourceIPAddress": "127.0.0.1"
},
"s3": {
"configurationId": "testConfigRule",
"object": {
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901",
"key": "HappyFace.jpg",
"size": 1024
},
"bucket": {
"arn": bucketarn,
134
AWS Lambda
"name": "sourcebucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
}
},
"s3SchemaVersion": "1.0"
},
"responseElements": {
"x-amz-id-2":
"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
"x-amz-request-id": "EXAMPLE123456789"
},
"awsRegion": "us-east-1",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"eventSource": "aws:s3"
}
]
}
You will have to use the following command to get the details of file uploaded from the S3
put event:
You will have to use the following command to get the bucket name :
You will have to use the following command to get the EventName:
Step 14
135
AWS Lambda
Now, let us update AWS Lambda code to print the S3 details as shown below:
Step 15
Save the changes. Click Test and enter the S3 sample event:
Step 16
136
AWS Lambda
Now click Test and you can see the output as shown:
Step 17
137
AWS Lambda
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data) {
if (err) console.log(err);
else {
console.log("===EMAIL SENT===");
console.log("EMAIL CODE END");
console.log('EMAIL: ', email);
context.succeed(event);
callback(null, "email is send");
}
});
};
Step 18
138
AWS Lambda
Now, upload the file and check the mail id provided in AWS Lambda code:
139
12. AWS Lambda — Creating and Deploying using AWS Lambda
AWS CLI
AWS CLI is a command line tool which helps to work with AWS services.We can use it to
create, update, delete, invoke aws lambda function. In this chapter, you will discuss about
installation and usage of AWS CLI in detail.
For Windows
Check your Windows configuration and choose one of the following links for installing AWS
CLI MSI:
For Windows 64 bit: AWS CLI MSI installl for windows (64bit)
For Windows 32 bit: AWS CLI MSI installl for windows (32bit)
Once you choose corresponding link and click it, you can find a Window as shown here:
140
AWS Lambda
Next, set the Environment path in windows as shown in the screenshots below:
Once done, you can use the following command on the command prompt, to see if aws cli is
installed:
aws --version
141
AWS Lambda
Now, we need to configure AWS settings. You can use the following command for this purpose:
aws configure
You can obtain these details from your aws console. Go to you Account name at top right
corner as shown:
142
AWS Lambda
Now, click My Security Credentials and select users from left side.Add user with details as
asked.
Add the user and to get the access key and secret key. To see the new access key, choose
Show. Your credentials will look like as shown below:
143
AWS Lambda
144
AWS Lambda
create-function
This api will create a new lambda function. The code needs to be given in zip format. If the
function to be created already exists, the api will fail. Note that the function name is case-
sensitive.
Commands Included
The list of commands that you can use with create-function is given here:
create-function
--function-name <value>
--runtime <value>
--role <value>
--handler <value>
[--code <value>]
[--description <value>]
[--timeout <value>]
[--memory-size <value>]
[--environment <value>]
[--kms-key-arn <value>]
[--tags <value>]
[--zip-file <value>]
[--cli-input-json <value>]
145
AWS Lambda
Options Included
Various options that you can use with the functions above are as follows:
--function-name (string): This takes the name of the function.The name can be 64-bit
characters.
--runtime(string): Here you need to specify the runtime environment ie the language
selection. The details of the runtime are as given below:
Java java8
C# 1 dotnetcore1.0
C# 2 dotnetcore2.0
Go go1.x
--role(string): This will be the name of the lambda policy ie the role to be given to the
lambda function for accessing other services.It will have the permission as per the role
specified.
--handler (string): This is the name of the handler where the lambda code execution will
start.
--timeout (integer): timeout will have the time at which the lambda function has to
terminate execution.The default is 3s.
146
AWS Lambda
--memory-size (integer): This is the memory given to the aws lambda function.AWS will
allocate the amount of CPU and memory allocation based on the memory given.
--environment (structure): its a object with environment details required in the aws
lambda function.
e.g : Variables={Name1=string,Name2=string}
--kms-key-arn (string): this is amazon resource name (ARN) used to encrypt the
environment variables.If not provided it will take the default settings to encrypt.
--zip-file (blob): path of the zip file which has the details of the code.
--cli-input-json (string) : Performs service operation based on the JSON string provided.
The JSON string follows the format provided by --generate-cli-skeleton. If other arguments
are provided on the command line, the CLI values will override the JSON-provided values.
Now, let us create a simple AWS Lambda function using runtime as nodejs and add some
console.logs to be printed.
Getting ARN
For the role, let us use the arn from the existing role we have created. To get the ARN, you
will have to follow the steps as shown here. Observe the respective screenshots wherever
attached:
147
AWS Lambda
Step 1
Go to IAM and select the role you want from Roles. The ARN details for the role are displayed
as shown below. Use Role ARN with create-function in aws cli.
148
AWS Lambda
Now, if you run the command in aws cli, you can find an output as shown below:
149
AWS Lambda
You can test the function and check the output as shown:
150
AWS Lambda
list-functions
This api gives the list of functions created so far in AWS Lambda.
Commands Included
The following are the commands asscociated with this API:
list-functions
[--master-region <value>]
[--function-version <value>]
[--max-items <value>]
[--cli-input-json <value>]
--max-items(integer): optional.This will give the items as the per the value specified.
151
AWS Lambda
get-function
This api will give details of the functions and also a url link which has zip file uploaded using
create-function. The url with zip details will be valid only for 10 mins.
Commands Included
The following are the commands associated with this api:
get-function
--function-name <value>
[--qualifier <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
152
AWS Lambda
Options Included
--function-name: Name of the AWS Lambda function. You can also specify Amazon Resource
Name of the function.
--qualifier(string): Optional. Function version can be used to get the details of the function.
It gives the url which has the zip code uploaded. In the above case the url is:
https://prod-04-2014-
tasks.s3.amazonaws.com/snapshots/625297745038/awslambdausingcli-97048f8d-4a08-
4ed9-99d9-acb00d2063d2?versionId=d04HKvPu9S2zz8pzjbW6Rmf5o5fxnc_r&X-Amz-Security-
Token=FQoDYXdzEKT%2F%2F%2F%2F%2F%2F%2F%2F%2F%2FwEaDCpTmGvtwKToPBiWcyK3A96UcJEnwvYD
hMbbxu%2Bg2gffK2ocfnlEeiCHak8QqqE1RFpbKrdks9NzxP9gNbagL4M9RValxJ1a9PUY%2FOdAekscRH
OiX00MVAxUlI82pKryhdOwLJWSj0uRzqvOxCcBwJenHrSNPeG6lMa2ZDo0qZFEUDONSaTg4nuSnJK1f6t3
pMAKu4vF9wPvf92G%2BU60rUxwleggigISmD9l1IlZse3%2BVF1JlNuN%2F5d85v0y2Q%2F%2BO515Cybc
Zpn91sHPYG8JMJ00LsrkQ2Ww4VU9Zz5c5QYH4JYPj0CyEgSz9b%2FMceMPpOoPUAMjctb%2FEwQqcShZeq
Ar9%2Fcd2ZI%2BXl2%2Bs4ri0ucgPvQQvseGIIiZbX3GqdwR2jb1nylrAEIfiuFMoSWfcFYoYtuL0MZnjG
R9jy2GNkp6MB%2BlHHr7%2BnuFRUzU26rgDYmdE1wRb3%2B21Jm49WGDa9opRLvUxFaux57Or70haib2Fu
KzN6Gf3Vzzk5KPdWsYUpaLyf%2B1ovEytOZhB1JEXuCs%2FGIlOXS88yxT%2BpOKmyxweiezpGgI%2FAkS
AQTbSRsYQKIOFyIJNHzplwrJKhy28vy60numIBIo9Zqq2AU%3D&X-Amz-Algorithm=AWS4-HMAC-
SHA256&X-Amz-Date=20180527T112426Z&X-Amz-SignedHeaders=host&X-Amz-Expires=600&X-
Amz-Credential=ASIAICSQHLSBWFP37U4Q%2F20180527%2Fus-east-1%2Fs3%2Faws4_request&X-
Amz-Signature=8b97e7d6d7da13313068e027894d2c875be5e50a0c5a62550f55307985bdc1aa
153
AWS Lambda
get-function-configuration
This will give the configuration details of the AWS Lambda function.
The following are the commands used along with this api:
get-function-configuration
--function-name <value>
[--qualifier <value>]
--function-name (string): name of the aws lambda function.You can also specify Amazon
Resource Name of the function.
--qualifier(string): Optional. Function version can be used to get the details of the function.
get-account-settings
This api gives the accounts settings.
Commands Involved
The command that you can use with this api are:
get-account-settings
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
154
AWS Lambda
Options Involved
You can use the following options with this api:
You can see the following output when you execute the command given above:
update-function-configuration
This api helps to update the configuration details for AWS Lambda function created. You can
change the memory, timeout, handler, role, runtime, description etc.
Commands Involved
The following are the commands involved in the update-function-configuration api:
update-function-configuration
--function-name <value>
[--role <value>]
[--handler <value>]
[--description <value>]
[--timeout <value>]
[--memory-size <value>]
[--environment <value>]
[--runtime <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
155
AWS Lambda
Options Involved
The following are the options involved in update-function-configuration api:
e.g: Variables={Name1=string,Name2=string}
--runtime(string): Here you need to specify the runtime environment ie the language
selection.
The details of the runtime are shown in the table given below:
Java java8
C# 1 dotnetcore1.0
C# 2 dotnetcore2.0
Go go1.x
--cli-input-json (string): optional.This will perform the operation on the api as specified in
the json string provided.
--generate-cli-skeleton (string): optional. This will output the JSON skeleton of all details
156
AWS Lambda
without executing the api. The output can be used as a input to --cli-input-json.
Now, let us chage the memory and timeout of AWS Lambda function that we have created
earlier. Follow the steps given below and observe the corresponding screenshots attached for
this purpose:
Step 1
Step 2
Step 3
157
AWS Lambda
Step 4
Update-function-code
This api will update the code for an existing AWS Lambda function.
Commands Involved
update-function-code
--function-name <value>
[--zip-file <value>]
[--s3-bucket <value>]
[--s3-key <value>]
[--s3-object-version <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
Options Involved
The following are the options involved with the update-function-code api:
158
AWS Lambda
--zip-file (blob): optional . Path of the zip file which has the code to be updated.
--s3-bucket(string): optional.S3 bucket name which has the zip file with code uploaded.
--cli-input-json (string): optional.This will perform the operation on the api as specified in
the json string provided.
--generate-cli-skeleton (string): optional. This will output the JSON skeleton of all details
without executing the api. The output can be used as a input to --cli-input-json.
You can use the following command with values for this purpose:
159
AWS Lambda
delete-function
The delete aws cli api will delete the function given.
Commands Included
The command details for the same are given here:
delete-function
--function-name <value>
[--qualifier <value>]
[--cli-input-json <value>]
160
AWS Lambda
[--generate-cli-skeleton <value>]
Options Included
The options included in this api are as given below:
--function-name(string): this will take the lambda function name or the arn of the aws
lambda function.
--qualifier (string): This is optional.Here you can specify the version of aws lambda that
needs to be deleted.
You can use the following command with values for this purpose:
161
AWS Lambda
Now, observe that the function will not be seen in AWS Lambda function list:
162
13. AWS Lambda — Creating and Deploying using AWS Lambda
Serverless Framework
AWS Lambda can be created and deployed using serverless framework. It allows you to create
AWS Lambda triggers and also deploy the same by creating the required roles. Serverless
framework allows to handle big projects in an easier way.The events and resources required
are written in one place and just a few commands helps in deploying the full functionality on
AWS console.
In this chapter, you will learn in detail how to get started with AWS serverless framework.
You will have to use the following command to install serverless using npm package:
163
AWS Lambda
Once npm is done, execute serverless command which shows the list of command to be used
to create and deploy AWS Lambda function. Observe the screenshots given below:
164
AWS Lambda
You can also use sls instead of servesless. sls is the shorthand command for serverless.
In case you need help on the command sls, you can use the following command:
165
AWS Lambda
For creating a serverless framework, you have to follow the steps given below:
Step 1
To start using serverless framework, we need to add the credentials. By this, you can the
user first in AWS console as follows:
Step 2
Click on Next:Permissions button to add permissions. You will have to attach the existing
policies or Administrator Access to this user.
166
AWS Lambda
Step 3
Click Create User to add the user. It will display the access key and secret key which we
need to configure the serverless framework:
Note that the details of credentials entered, that is the access key and secret key are stored
in the file /aws/credentials.
167
AWS Lambda
First, create a folder where you want your project files to be stored.
Step 1
168
AWS Lambda
Step 2
Step 3
We shall make use of aws-nodejs template to create our first project using serverless
framework. The command for the same purpose is as shown here:
Step 4
Now, open the folder created in an IDE. Here we are using Visual Studio code and the folder
structure is as follows:
169
AWS Lambda
Step 5
The AWS Lambda basic function details are shown in handler.js as follows:
'use strict';
callback(null, response);
// Use this code if you don't use the http event with the LAMBDA-PROXY
integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed
successfully!', event });
};
Thie file Serverless.yml has the configuration details of the serverless framework as shown
below:
# Welcome to Serverless!
#
# This file is the main config file for your service.
# It's very minimal at this point and uses default values.
# You can always add more config options for more control.
# We've included some commented out config examples here.
# Just uncomment any of them to get that config option.
#
# For full config options, check the docs:
# docs.serverless.com
170
AWS Lambda
#
# Happy Coding!
# You can pin your service to only deploy with a specific Serverless version
# Check out our docs for more details
# frameworkVersion: "=X.X.X"
provider:
name: aws
runtime: nodejs6.10
# you can add statements to the Lambda function's IAM Role here
# iamRoleStatements:
# - Effect: "Allow"
# Action:
# - "s3:ListBucket"
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" :
"ServerlessDeploymentBucket" } ] ] }
# - Effect: "Allow"
# Action:
# - "s3:PutObject"
# Resource:
# Fn::Join:
# - ""
# - - "arn:aws:s3:::"
# - "Ref" : "ServerlessDeploymentBucket"
# - "/*"
171
AWS Lambda
# environment:
# variable1: value1
functions:
hello:
handler: handler.hello
172
AWS Lambda
# - "aws.ec2"
# detail-type:
# - "EC2 Instance State-change Notification"
# detail:
# state:
# - pending
# - cloudwatchLog: '/aws/lambda/hello'
# - cognitoUserPool:
# pool: MyUserPool
# trigger: PreSignUp
173
AWS Lambda
Now, we need to add changes in serverless.yml file as per our requirements. You can use the
commands as given below:
Now, change the service here and add the name given to our folder as shown:
provider:
name: aws
runtime: nodejs6.10
The provider is aws and runtime is nodejs6.10. We need to add the region in which we will
be working and the stage, that is dev or prod environment for the project. So here are the
updated details of provider:provider:
name: aws
runtime: nodejs6.10
# you can overwrite defaults here
stage: prod
region: us-east-1
IAM Role
The iam role, that is, the code for permission to work with Lambda is shown here in the .yml
file:
# iamRoleStatements:
# - Effect: "Allow"
# Action:
# - "s3:ListBucket"
# Resource: { "Fn::Join" : ["", ["arn:aws:s3:::", { "Ref" :
"ServerlessDeploymentBucket" } ] ] }
# - Effect: "Allow"
# Action:
# - "s3:PutObject"
# Resource:
# Fn::Join:
174
AWS Lambda
# - ""
# - - "arn:aws:s3:::"
# - "Ref" : "ServerlessDeploymentBucket"
# - "/*"
Note that we need to give the details of the role, that is the permission required with other
AWS services, in the above section.
functions:
hello:
handler: handler.hello
The resource details about the s3 service added as shown below here:
175
AWS Lambda
Step 1
sls deploy
Step 2
Now, you should see the function in AWS console now as shown. The details of serverless
AWS are logged in AWS cloudformation. For this purpose, go to AWS service and select
CloudFormation.The details of the AWS Lambda are displayed as follows:
176
AWS Lambda
Observe that the name given is project name followed by the stage used.
Step 3
It creates the iam role for AWS Lambda and log group for AWS cloudwatch. S3 bucket is
created which has the code details stored and the configuration details.
This is created by the command sls deploy. You need not specify the iam role, instead it is
created by default during the deploy stage.
177
AWS Lambda
Step 4
178
AWS Lambda
When you test the Lambda function, you can find the following output:
We can also test the AWS Lambda function using the serverless command as shown below:
179
AWS Lambda
This invoke command triggers the AWS Lambda function and displays the output in the
command prompt as shown below:
180
AWS Lambda
You can also test the Lambda function before deploying and the command for same using the
following command :
Please note that it is not always possible to test locally as the resources like S3 and DynanoDB
cannot be simulated on the local environment. Only the basic function calls can be tested
locally.
181
AWS Lambda
Now, open aws-api project in visual code. You can see that the handler.js and
serverless.yml files created. Let us do the changes in that for addition of api gateway.
182
AWS Lambda
Now, the events details added for api gateway activation with AWS Lambda:
There is a new thing added here called events. We have specified the event as http, along
with its path and method.
The path is the end-point which we will use when the api gateway path is created and method
used is GET.
Observe that the handler is handler.hello, and hello is the export name from handler.js.
183
AWS Lambda
Note that you do not have to deploy the api gateway here, as the serverless framework will
perform it.
Now, we will run the sls deploy command to create AWS Lambda function with trigger as
api gateway.
sls deploy
184
AWS Lambda
Observe that the deploy details are listed above. It gives the Get url with the end-point as
the path details. The stage is prod so same is used in the url. The name of the function is
aws-api-prod-hello.
Let us hit the url and see the output. You can see the followings the response we get from
the api-gateway get url:
185
AWS Lambda
The event details are also available in the output when you hit the url. The httpMethod is GET
and the queryStringParameters are null as there is nothing passed in the query string. The
event details are given to input which we have specified in the AWS Lambda handler:
The output we get from api gateway are only the body details such as message and input.
The response is totally controlled by the api gateway and how to display it as output.
Now, let us pass inputs to the GET url in query string and see the display:
186
AWS Lambda
Let us change the AWS Lambda function to just display the querystring details as shown
below:
'use strict';
module.exports.hello = (event, context, callback) => {
const response = {
statusCode: 200,
body: JSON.stringify({
message:(event.queryStringParameters &&
event.queryStringParameters.displaymessage!="") ?
event.queryStringParameters.displaymessage : 'Go Serverless v1.0! Your function
executed successfully!'
}),
187
AWS Lambda
};
callback(null, response);
// Use this code if you don't use the http event with the LAMBDA-PROXY
integration
// callback(null, { message: 'Go Serverless v1.0! Your function executed
successfully!', event });
};
Observe that we have changed the message based on the querystring displaymessage. This
will deploy the the function again and check the output.It displays the details present in query
string variable displaymessage as shown below.
Let us now add post method to the events created as shown below:
188
AWS Lambda
Now, deploy the changes made and you can see the following output from the deploy
command:
Note that testing post url in browser directly will not give the details. You should test the post
url in postman.
189
14. AWS Lambda — Executing and Invoking AWS Lambda
Lambda Function
This chapter will explain in detail about process of executing and invoking Lambda function
and the steps involved in it.
With the help of the configuration details, AWS Lambda creates an execution context.
Execution context is a temporary runtime environment which is made ready with any external
dependencies such as database connection, http endpoints, third party libraries etc., if any.
When AWS Lambda function is invoked for the very first time or if the lambda function is
updated, there is little latency added because of the execution context setup. However, the
subsequent calls are faster in comparison to the first one. AWS Lambda tries to reuse the
execution context again if the Lambda function is invoked taking lesser time.
● If there is any database connection done for the execution of Lambda, the connection
is maintained for reuse. So the Lambda code has to be such that the connection has
to be checked first- if exists and reused; otherwise we shall have to make fresh new
connection.
● Execution context maintains a disk space of 500MB in /tmp directory. The data
required is cached in this directory. You can have additional check in the code to see
if the data exists.
● If the callbacks or some background processes if the are not complete when the
Lambda function was invoked, the execution will start when the lambda function is
invoked again. Incase you do not need such thing to happen make sure your processes
are all ended properly, when the function execution is complete.
You should use of the execution context and the data stored in tmp directory. You will have
to add necessary checks in the code to see if the required data exists before creating fresh
new ones.This will save the time during execution and make it more faster.
190
AWS Lambda
Commands
create-function
--function-name <value>
--runtime <value>
--role <value>
--handler <value>
[--code <value>]
[--description <value>]
[--timeout <value>]
[--memory-size <value>]
[--environment <value>]
[--kms-key-arn <value>]
[--tags <value>]
[--zip-file <value>]
[--cli-input-json <value>]
191
AWS Lambda
192
AWS Lambda
Now, you can invoke the function using the command: invoke
--function-name <value>
[--invocation-type <value>]
[--log-type <value>]
[--client-context <value>]
[--payload <value>]
[--qualifier <value>]
outfile <value>
Options
DryRun is to be used when you want to verify the Lambda function without need of
executing it.
--log-type: It will be Tail if the invocation type is RequestResponse.It gives the last 4KB
base64-encoded log data. Possible values are Tail and None.
--client-context: You can pass client specific details to the Lambda function.The
clientcontext has to be in json format and base64-encoded. Maximum file size is 3583 bytes.
--qualifier: You can specify Lambda function version or alias name.If you pass the function
version than the api will use qualified function arn to invoke the Lambda function.If you specify
alias name, the api uses alias ARN to invoke Lambda function.
193
AWS Lambda
You can use payload option to send dummy event to the lambda function in json format as
shown below.
Observe that in the code, we have console event.name and event.addr. Now, let us use
payload option in aws cli to send the event with name and address as follows:
194
AWS Lambda
Sample Events
You can test AWS Lambda function by passing a sample event. This section gives some sample
events for AWS Services. You can use the invoke command to test the output when triggered
with any of the services. Observe the codes given for corresponding sample events below:
195
AWS Lambda
"key": "HappyFace.jpg",
"size": 1024
},
"bucket": {
"arn": bucketarn,
"name": "sourcebucket",
"ownerIdentity": {
"principalId": "EXAMPLE"
}
},
"s3SchemaVersion": "1.0"
},
"responseElements": {
"x-amz-id-2":
"EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH",
"x-amz-request-id": "EXAMPLE123456789"
},
"awsRegion": "us-east-1",
"eventName": "ObjectCreated:Put",
"userIdentity": {
"principalId": "EXAMPLE"
},
"eventSource": "aws:s3"
}
]
}
To get the details of the file from the s3 put event, you can use the following command:
To get the bucket name, you can use the following command:
196
AWS Lambda
197
AWS Lambda
"principalId": "EXAMPLE"
},
"eventSource": "aws:s3"
}
]
}
Amazon DynamoDB
Amazon DynamoDB can be an event on AWS Lambda when changes are made on DynamoDB
table. We can perform operation like add entry, update and delete records from the
DynamodDB table.
A sample event for DynamoDB add, insert and delete event is shown here:
{
"Records": [
{
"eventID": "1",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"NewImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES",
"SequenceNumber": "111",
"SizeBytes": 26
198
AWS Lambda
},
"awsRegion": "us-west-2",
"eventName": "INSERT",
"eventSourceARN": eventsourcearn,
"eventSource": "aws:dynamodb"
},
{
"eventID": "2",
"eventVersion": "1.0",
"dynamodb": {
"OldImage": {
"Message": {
"S": "New item!"
},
"Id": {
"N": "101"
}
},
"SequenceNumber": "222",
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 59,
"NewImage": {
"Message": {
"S": "This item has changed"
},
"Id": { "N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
},
199
AWS Lambda
"awsRegion": "us-west-2",
"eventName": "MODIFY",
"eventSourceARN": sourcearn,
"eventSource": "aws:dynamodb"
},
{ "eventID": "3",
"eventVersion": "1.0",
"dynamodb": {
"Keys": {
"Id": {
"N": "101"
}
},
"SizeBytes": 38,
"SequenceNumber": "333",
"OldImage": {
"Message": {
"S": "This item has changed"
},
"Id": {
"N": "101"
}
},
"StreamViewType": "NEW_AND_OLD_IMAGES"
}, "awsRegion": "us-west-2",
"eventName": "REMOVE",
"eventSourceARN": sourcearn,
"eventSource": "aws:dynamodb" }
]
}
200
AWS Lambda
triggered with a SNS event, which has details of the messages. This messages can be
processed inside Lambda function and can be sent further to other services as per the
requirement.
Once the message is entered, SNS will trigger the Lambda function. If any error tries to invoke
the Lambda function, SNS will retry to call the lambda function upto three times.
{
"Records": [
{
"EventVersion": "1.0",
"EventSubscriptionArn": eventsubscriptionarn,
"EventSource": "aws:sns",
"Sns": {
"SignatureVersion": "1",
"Timestamp": "1970-01-01T00:00:00.000Z",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
},
"Type": "Notification",
"UnsubscribeUrl": "EXAMPLE",
"TopicArn": topicarn,
"Subject": "TestInvoke"
201
AWS Lambda
}
}
]
}
{
"Records": [
{
"eventVersion": "1.0",
"ses": {
"mail": {
"commonHeaders": {
"from": [
"Jane Doe <janedoe@example.com>"
],
"to": [
"johndoe@example.com"
],
"returnPath": "janedoe@example.com",
"messageId": "<0123456789example.com>",
"date": "Wed, 7 Oct 2015 12:34:56 -0700",
"subject": "Test Subject"
},
"source": "janedoe@example.com",
"timestamp": "1970-01-01T00:00:00.000Z",
"destination": [
"johndoe@example.com"
],
202
AWS Lambda
"headers": [
{
"name": "Return-Path",
"value": "<janedoe@example.com>"
},
{
"name": "Received",
"value": "from mailer.example.com (mailer.example.com [203.0.113.1])
by inbound-smtp.us-west-2.amazonaws.com with SMTP id o3vrnil0e2ic for
johndoe@example.com; Wed, 07 Oct 2015 12:34:56 +0000 (UTC)"
},
{
"name": "DKIM-Signature",
"value": "v=1; a=rsa-sha256; c=relaxed/relaxed; d=example.com;
s=example; h=mime-version:from:date:message-id:subject:to:content-type;
bh=jX3F0bCAI7sIbkHyy3mLYO28ieDQz2R0P8HwQkklFj4=;
b=sQwJ+LMe9RjkesGu+vqU56asvMhrLRRYrWCbV"
},
{
"name": "MIME-Version",
"value": "1.0"
},
{
"name": "From",
"value": "Jane Doe <janedoe@example.com>"
},
{
"name": "Date",
"value": "Wed, 7 Oct 2015 12:34:56 -0700"
},
{
"name": "Message-ID",
"value": "<0123456789example.com>"
},
{
"name": "Subject",
203
AWS Lambda
204
AWS Lambda
"virusVerdict": {
"status": "PASS"
}
}
},
"eventSource": "aws:ses"
}
]
}
205
AWS Lambda
"Accept":
"text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
"Accept-Encoding": "gzip, deflate, lzma, sdch, br",
"Accept-Language": "en-US,en;q=0.8",
"CloudFront-Forwarded-Proto": "https",
"CloudFront-Is-Desktop-Viewer": "true",
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36
OPR/39.0.2256.48",
"Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
"X-Forwarded-For": "192.168.100.1, 192.168.1.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"pathParameters": {
"proxy": "hello"
},
"requestContext": {
"accountId": "123456789012",
"resourceId": "us4z18",
"stage": "test",
"requestId": "41b45ea3-70b5-11e6-b7bd-69b5aaebc7d9",
"identity": {
"cognitoIdentityPoolId": "",
"accountId": "",
"cognitoIdentityId": "",
"caller": "",
"apiKey": "",
206
AWS Lambda
"sourceIp": "192.168.100.1",
"cognitoAuthenticationType": "",
"cognitoAuthenticationProvider": "",
"userArn": "",
"userAgent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36
OPR/39.0.2256.48",
"user": ""
},
"resourcePath": "/{proxy+}",
"httpMethod": "GET",
"apiId": "wt6mne2s9k"
},
"resource": "/{proxy+}",
"httpMethod": "GET",
"queryStringParameters": {
"name": "me"
},
"stageVariables": {
"stageVarName": "stageVarValue"
}
}
207
AWS Lambda
"CloudFront-Is-Mobile-Viewer": "false",
"CloudFront-Is-SmartTV-Viewer": "false",
"CloudFront-Is-Tablet-Viewer": "false",
"CloudFront-Viewer-Country": "US",
"Host": "wt6mne2s9k.execute-api.us-west-2.amazonaws.com",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6)
AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36
OPR/39.0.2256.48",
"Via": "1.1 fb7cca60f0ecd82ce07790c9c5eef16c.cloudfront.net (CloudFront)",
"X-Amz-Cf-Id": "nBsWBOrSHMgnaROZJK1wGCZ9PcRcSpq_oSXZNQwQ10OTZL4cimZo3g==",
"X-Forwarded-For": "192.168.100.1, 192.168.1.1",
"X-Forwarded-Port": "443",
"X-Forwarded-Proto": "https"
},
"body": "Hello World"
}
208
15. AWS Lambda — Deleting Lambda Function AWS Lambda
Deleting AWS Lambda function will remove the AWS Lambda from the AWS console. There
are 2 ways to delete AWS Lambda function.
Step 1
Login to AWS console and go to AWS Lambda service. You can find that AWS lambda functions
created so far are listed in AWS console as shown below:
209
AWS Lambda
The list shows that there are 23 AWS Lambda functions created so far. You can view them
using the pagination provided on the top or search the AWS Lambda by using the search box.
Step 2
Observe that there is a radio button across each of the AWS Lambda function. Select the
function you want to delete. Observe the screenshot shown below:
Step 3
Once you select the AWS Lambda function, the Action dropdown which was earlier grayed
out is highlighted now. Now, open the combo box and it will display options as shown:
210
AWS Lambda
Step 4Select the Delete button to delete the AWS Lambda function. Once you click Delete,
it displays the message as follows:
Step 5
Read the message carefully and later click Delete button to remove the AWS lambda function
permanently.
Note: Deleting aws lambda will not delete the role linked. To remove the role, you need to
go to IAM and remove the role.
Step 6
The list of roles created so far is shown below. Observe that there is a Create role button
and Delete role button.
Click the checkbox across the role you want to delete. You can also select multiple roles to
delete at a time.
211
AWS Lambda
Step 7
You will see a confirmation message as shown below once you click Delete button:
Now, read the details mentioned carefully and later click Yes, delete button.
212
AWS Lambda
Step 1
Step 2
The AWS Lambda function created is lambdatestcli. We have used existing role arn to create
the lambda function.
213
AWS Lambda
Then you can find this function displayed in AWS console as shown below:
Step 3
Now, let us invoke the function to test the output using the command shown:
214
AWS Lambda
Step 4
You can observe logs from cloudwatch for lambda function lambdatestcli
Step 5
Now, let us come to the actual part of deleting the AWS function. Delete aws cli api will
delete the function given. The details of command used for this purpose is given below:
Command
delete-function
--function-name <value>
[--qualifier <value>]
[--cli-input-json <value>]
[--generate-cli-skeleton <value>]
Options
--function-name(string): This will take the Lambda function name or the arn of the AWS
Lambda function.
--qualifier (string): This is optional. Here you can specify the version of AWS Lambda that
needs to be deleted.
215
AWS Lambda
Step 6
If you check now, you can observe that the function will not be seen in AWS Lambda function
list as shown in the screenshot given below:
216
16. AWS Lambda — Working with Amazon API AWS Lambda
Gateway
AWS Lambda function can be invoked on HTTPS url. It can be done on GET, POST, PUT.
When the HTTPS url is invoked, the AWS Lambda function can also triggered and the data
passed to HTTPS using get/post can be made available inside AWS Lambda to be used to
insert in DynamoDB or to send mail etc.
This chapter discusses in detail about various processes involved in workin with AWS lambda
and API Gateway.
Processes involved
The following are the processes involved in working with AWS lambda and API Gateway:
A basic diagram that explains the working of API gateway and AWS Lambda is given here:
These processes are explained in detail further in this chapter with relevant screenshots.
217
AWS Lambda
Go to IAM and select Roles from left side section as shown below:
218
AWS Lambda
219
AWS Lambda
Select Lambda and click Permissions at the bottom. Select the permission required for the
API Gateway and Lambda.
220
AWS Lambda
Search for API gateway in the search and it will list you all the related permissions. Here we
have chosen full access to API gateway as shown below:
221
AWS Lambda
Now, search for API gateway and it will list you all the related permissions. Here we have
chosen full access to API gateway as shown below:
222
AWS Lambda
223
AWS Lambda
Once you are done choosing the necessary policies, click Review for the next step. Enter the
name of the role as per your choice as shown below:
It displays the policies attached to the role. Click Create role and we are done with the role
creation and can proceed with the lambda function.
224
AWS Lambda
The UI screen for Lambda function is shown below. Click Create function button to proceed
with creation of Lambda function.
225
AWS Lambda
Enter the name of the function and choose the existing role which we have created above.
226
AWS Lambda
It flashes a message that the function with the name lambdawithapigateway is created
successfully.
Note that here we will use nodejs runtime to write the code.The AWS code with helloworld
message is as shown below:
AWS Lambda code is present in index.js file. The function called handler has the params
namely events, context and callback.
Callback function basically has the error and the success message. Note that here we do not
have any error related code, so null is passed and the success message is HelloWorld from
lambda.
Lastly, save the changes added and let us proceed to add the Lambda function to the API
gateway.
227
AWS Lambda
Click API Gateway and it will lead you to the screen where new API gateway can be created.
228
AWS Lambda
Click the Create API button on right side of the screen. This will display the newly created
API on to left side of the screen.
229
AWS Lambda
Click the Actions dropdown to create a new resource for the API.
230
AWS Lambda
231
AWS Lambda
Enter the Resource Name as shown below.You will see the name of the resource entered in
the url created at the end.Click Create Resource and you will see it on the screen as follows:
232
AWS Lambda
Add GET/POST methods to the resource created as shown below. Select the method from
Actions dropdown.
233
AWS Lambda
Next step is the integration which will integrate it with Lambda function. Now add the Lambda
function to it as shown below:
234
AWS Lambda
235
AWS Lambda
Save the changes and you can see a dialog box asking for permission as shown below:
Click OK for the permission. This is the execution details between the API gateway HTTP
request and the Lambda function:
236
AWS Lambda
Now, let us deploy the API gateway changes. For this purpose, we need to select the Deploy
API from Actions dropdown as shown below:
237
AWS Lambda
Select Deploy API. It will ask for the deployment state. Select New Stage from Deployment
stage dropdown and add the stage name as Production.
Click Deploy button and it will redirect you to the url as shown below:
Select the GET method from left side to get the url. Open the url in a new tab to see the
message from Lambda function.
This is a basic example of working with AWS Lambda and AWS API Gateway.In the above
example, we have hardcoded the message in Lambda function.
238
AWS Lambda
Now, let us take the message details from the API Gateway. Incase if the HTTPS call has to
be called from a different domain, for example AJAX call to the API, we need to enable CORS
for the API gateway created.
Select the resource created for the API and click Actions dropdown:
239
AWS Lambda
You can also specify the domain name you want to work with the API. Click Enable CORS
and replace existing CORS headers button and it will display confirmation message as
shown below:
Click Yes, replace existing values button to enable it. The Enable CORS screen looks as
shown below:
240
AWS Lambda
241
AWS Lambda
Choose Body Mapping Templates and add the Content-Type for this example as
application/json. Click on the content type added add the details as follows:
Observe that we have taken the message as the parameter to get data from API Gateway
and share it with AWS Lambda.The syntax to get the details is as shown above.
Now, deploy the API to make the changes available on the API Gateway URL. For this, we
need to change Lambda function to display the data based on the API Gateway URL. The code
for Lambda function is givn below. Note that we are taking the message from the event and
passing to callback.
242
AWS Lambda
Now, save the changes in Lambda and hit the URL to see the changes. Observe the screenshot
given below:
https://rw2ek1xung.execute-api.us-east-
1.amazonaws.com/prod/hello?message=hello%20from%20api%20gateway
Observe that here we are passing message as query string to the GET url. Then you can
observe the output as shown below:
It reads the details sent to message from the URL and displays the same in the browser.
243
17. AWS Lambda — Using Lambda Function with AWS Lambda
Amazon S3
Amazon S3 service is used for file storage, where you can upload or remove files. We can
trigger AWS Lambda on S3 when there are any file uploads in S3 buckets. AWS Lambda has
a handler function which acts as a start point for AWS Lambda function.The handler has the
details of the events. In this chapter, let us see how to use AWS S3 to trigger AWS Lambda
function when we upload files in S3 bucket.
● Create S3 Bucket
● Create role which has permission to work with s3 and lambda
● Create lambda function and add s3 as the trigger.
Example
Let us see these steps with the help of an example which shows the basic interaction between
Amazon S3 and AWS Lambda.
The block diagram that explains the flow of the example is shown here:
244
AWS Lambda
Creating S3 Bucket
Let us start first by creating a s3 bucket in AWS console using the steps given below:
Step 1
Go to Amazon services and click S3 in storage section as highlighted in the image given
below:
245
AWS Lambda
Step 2
Click S3 storage and Create bucket which will store the files uploaded.
Step 3
Once you click Create bucket button, you can see a screen as follows:
246
AWS Lambda
Step 4
Enter the details Bucket name, Select the Region and click Create button at the bottom
left side. Thus, we have created bucket with name : workingwithlambdaands3.
Step 5
Now, click the bucket name and it will ask you to upload files as shown below:
247
AWS Lambda
Step 1
248
AWS Lambda
Step 2
Step 3
Now, click Create role and choose the services that will use this role. Select Lambda and
click Permission button.
249
AWS Lambda
Step 4
Step 5
250
AWS Lambda
Step 6
Now, enter the Role name, Role description and click Create Role button at the bottom.
251
AWS Lambda
Step 1
Step 2
Click Lambda and follow the process for adding Name. Choose the Runtime, Role etc. and
create the function. The Lambda function that we have created is shown in the screenshot
below:
252
AWS Lambda
Step 3
253
AWS Lambda
Step 4
Choose the trigger from above and add the details as shown below:
254
AWS Lambda
Step 5
Select the bucket created from bucket dropdown. The event type has following details:
Select Object Created (All), as we need AWS Lambda trigger when file is uploaded, removed
etc.
255
AWS Lambda
Step 7
You can add Prefix and File pattern which are used to filter the files added. For example, to
trigger lambda only for .jpg images. Let us keep it blank for now as we need to trigger Lambda
for all files uploaded. Click Add button to add the trigger.
256
AWS Lambda
Step 8
You can find the the trigger display for the Lambda function as shown below:
Let’s add the details for the aws lambda function. Here, we will use the online editor to add
our code and use nodejs as the runtime environment.
Step 9
To trigger S3 with AWS Lambda, we will have to use S3 event in the code as shown below:
257
AWS Lambda
};
Note that the event param has the details of the S3 event. We have consoled the bucket
name and the file name which will get logged when you upload image in S3 bucket.
Step 10
Now, let us save the changes and test the lambda function with S3 upload. The following are
the code details added in AWS Lambda:
Step 11
Step 12
Now, save the Lambda function. Open S3 from Amazon services and open the bucket we
created earlier namely workingwithlambdaands3.
258
AWS Lambda
Step 13
259
AWS Lambda
Step 14
Click Add files to add files. You can also drag and drop the files. Now, click Upload button.
Step 15
To see the trigger details, go to AWS service and select CloudWatch. Open the logs for the
Lambda function and use the following code:
260
AWS Lambda
AWS Lambda function gets triggered when file is uploaded in S3 bucket and the details are
logged in Cloudwatch as shown below:
261
AWS Lambda
262
18. AWS Lambda — Using Lambda Function with AWS Lambda
Amazon DynamoDB
DynamoDB can trigger AWS Lambda when the data in added to the tables, updated or deleted.
In this chapter, we will work on a simple example that will add items to the DynamoDB table
and AWS Lambda which will read the data and send mail with the data added.
Requisites
To use Amazon DB and AWS Lambda, we need to follow the steps as shown below:
Example
We are going to work out on following example which shows the basic interaction between
DynamoDB and AWS Lambda. This example will help you to understand the following
operations:
● Creating a table called customer in Dynamodb table and how to enter data in that
table.
● Triggering AWS Lambda function once the data is entered and sending mail using
Amazon SES service.
The basic block diagram that explains the flow of the example is as shown below:
263
AWS Lambda
264
AWS Lambda
Now, click Create table to create the table as shown. We have named the table as customer
with primary key for that table as cust_id. Click on Create button to add the table to
dynamodb.
265
AWS Lambda
266
AWS Lambda
267
AWS Lambda
268
AWS Lambda
269
AWS Lambda
Let us create a policy to be used only for the DynamoDB table created earlier:
Now, choose a Service. Observe that the service we have selected is DynamoDB. For
Actions we have taken all Dynamodb actions ie access to list, read and write. For
Resources, we will select the table resource type actions. When you click it, you can see a
screen as follows:
270
AWS Lambda
Now, select table and Add ARN to it as shown. We will get ARN details from customer
table created as shown below:
271
AWS Lambda
272
AWS Lambda
Click Add button to save the changes.Once done Click on Review policy. Enter the name
of the policy, description etc as shown below:
Click on create policy to save it. Add the policy to the role to be created. Select Role from
left side and enter the details.
273
AWS Lambda
Now, let us add DynamodDB trigger to the AWS Lambda created. The runtime we shall use is
Node.js.
274
AWS Lambda
You can find the following details in Dynamodb trigger that are to be configured for AWS
Lambda:
Sending an email
To send email, you need to follow the steps given below:
Step 1
Go to AWS service and select SES (simple email service). Validate the email to which we need
to send an email as shown:
275
AWS Lambda
Step 2
Click the button Verify a New Email Addres to add the email address.
Step 3
Enter an email address to verify it. The email address will receive and activation mail from
Amazon which needs to be clicked. Once the activation is done, the email id is verified and
can be used with AWS services.
Step 4
The AWS Lambda code which reads data from the event and sends email is given below:
var eParams = {
Destination: {
ToAddresses: ["xxxxx@gmail.com"]
},
Message: {
276
AWS Lambda
Body: {
Text: {
Data: "The data added is as follows:\n
CustomerId:"+customerid+"\n Name:"+name+"\nAddress:"+address
}
},
Subject: {
Data: "Data Inserted in Dynamodb table customer"
}
},
Source: "xxxxx@gmail.com"
};
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data) {
if (err) console.log(err);
else {
console.log("===EMAIL SENT===");
console.log("EMAIL CODE END");
console.log('EMAIL: ', email);
context.succeed(event);
callback(null, "email is send");
}
});
}
277
AWS Lambda
Step 1
Step 2
278
AWS Lambda
Step 3
Click Save button and check the email id provided in AWS Lambda to see if the mail has been
sent by AWS Lambda.
279
19. AWS Lambda — Using Lambda Function with AWS Lambda
Scheduled Events
Scheduled events are suppose to happen at regular intervals based on a rule set. Scheduled
events are used to execute Lambda function after an interval which is defined in cloudwatch
services. They are best used for working on cron jobs along with AWS Lambda. This chapter
will explain with simple example how to send mail after every 5 minutes using scheduled
events and AWS Lambda.
Requisites
The requirements for using Lambda function with Scheduled events are as follows:
Example
The example that we are going to consider will add CloudWatch event to the AWS Lambda
function. Cloudwatch will trigger AWS Lambda based on the time pattern attached to it. For
example, in the example below we have used 5 minutes as the trigger. It means for every 5
minutes, AWS Lambda will be triggered and AWS Lambda will send mail whenever triggered.
280
AWS Lambda
281
AWS Lambda
Enter Email Address you want to verify. Click Verify This Email Address button. You will
receive mail from AWS on that email id with email subject: Amazon Web Services – Email
Address Verification Request in region US East (N. Virginia)
Click the link given in the mail to verify email address. Once verified, it will display the email
id as follows:
282
AWS Lambda
283
AWS Lambda
284
AWS Lambda
Note that the event will be triggered after every 5 minutes as per the rule trigger created.
285
AWS Lambda
},
Message: {
Body: {
Text: {
Data: "this mail comes from aws lambda event scheduling"
}
},
Subject: {
Data: "Event scheduling from aws lambda"
}
},
Source: "coxxxxxx@gmail.com"
};
console.log('===SENDING EMAIL===');
var email = ses.sendEmail(eParams, function(err, data) {
if (err) console.log(err);
else {
console.log("===EMAIL SENT===");
console.log("EMAIL CODE END");
console.log('EMAIL: ', email);
context.succeed(event);
callback(null, "email is send");
}
});
};
Now, we need the AWS SES service. You can add this using the code shown as follows:
286
AWS Lambda
To send mail from nodejs, we have created eParams object which has details like the
source mail, to mail id and the body with message as follows:
var eParams = {
Destination: {
ToAddresses: ["xxxxxxxx12@gmail.com"]
},
Message: {
Body: {
Text: {
Data: "this mail comes from aws lambda event scheduling"
}
},
Subject: {
Data: "Event scheduling from aws lambda"
}
},
Source: "coxxxxxx@gmail.com"
};
287
AWS Lambda
Now, let us save this Lambda function and check the email id for mails. The screenshot shown
below shows that the mail is sent from AWS Lambda after every 5 minutes.
288
20. AWS Lambda — Using Lambda Function with AWS Lambda
Amazon SNS
Amazon SNS is a service used for push notification. In this chapter, we will explain working
of AWS Lambda and Amazon SNS with the help of an example where will perform the following
actions:
● Create Topic in SNS Service and use AWS Lambda Add Topics to CloudWatch
● Send SNS text message on phone number given.
Requisites
To create Topic in SNS Service and use AWS Lambda Add Topics to CloudWatch, we need ot
follow the steps given below:
Example
In this example, we will create a topic in SNS. When details are entered in the topic to publish,
AWS Lambda is triggered. The topic details are logged in CloudWatch and a message is sent
on phone by AWS Lambda.
289
AWS Lambda
Step 1
290
AWS Lambda
Step 2
Step 3
Step 4
Enter the Topic name and Display name and click on Create topic. You should see the
topic name in the display as follows:
291
AWS Lambda
Observe that we have added policies for SNS, Lambda and CloudWatch. Add rolename and
click Create role button to complete the process of role creation.
292
AWS Lambda
For this purpose, login to AWS console and choose AWS Lambda from AWS services. Add the
function name, role details etc and create the AWS Lambda function as shown.
293
AWS Lambda
Then , select SNS topic and Add the trigger to AWS Lambda function as shown:
294
AWS Lambda
Publish to Topic
First Select name of the topic you want to publish. Click on Publish to topic button:
295
AWS Lambda
You can also select JSON message format to send in JSON style. Click Publish the message
button at the end of the screen.
296
AWS Lambda
297
AWS Lambda
};
We have added AWS SDK and the SNS service to use to send message. The message from
the event coming from SNS is send as text message on the phone numbe given.
sns.publish({
Message: snsmessage,
PhoneNumber: '+911212121212'
}, function (err, data) {
if (err) {
console.log(err);
callback(err, null);
} else {
console.log(data);
callback(null, data);
}
});
298
AWS Lambda
Enter the topic now to see the message in cloudwatch and the phone number given above.
299
AWS Lambda
Click Publish message to publish the message. You see a message on the phone number
given as follows:
300
21. AWS Lambda — Using Lambda Function with AWS Lambda
CloudTrail
AWS CloudTrail is a service available with Amazon, which helps to logs all the activities done
inside AWS console. It logs all the API calls and stores the history, which can be used later
for debugging purpose. Note that we cannot trigger Lambda from CloudTrail. Instead,
CloudTrail stores all the history in the form of logs in S3 bucket and we can trigger AWS
Lambda from S3. Once any logs are to be processed, AWS Lambda will get triggered whenever
any logs are added to S3 bucket.
Requisites
Before you start to work with AWS CloudTrail, S3 and AWS Lambda, you need to perform the
following:
Example
Let s consider an example which shows the working of AWS CloudTrail, S3 and AWS
Lambda. Here, we will create a bucket in S3 which will store all the logs for any interaction
done in AWS console. Let us create SNS topic and publish it. For this action, the logs will be
entered as a file in S3. AWS lambda will get triggered which will send mail using Amazon
SES service.
301
AWS Lambda
Observe that here we have created a S3 bucket cloudtraillogsaws for storing the logs.
302
AWS Lambda
We have created topic called displaytrail to publish a topic. Its details will get stored in S3
bucket that is created above.
Create a Trail in Cloudtrail and Assign the S3 bucket and SNS service
Go to AWS console and click CloudTrail service from Management tools as shown:
Click Create Trail button. Enter the Trail name, Apply trail to all regions and choose Yes.
Then So the logs will be applied for all the region.
303
AWS Lambda
For Read/Write events, choose All. Add the S3 bucket and SNS topic details as shown
below. You can create a new one here or add an existing one.
304
AWS Lambda
Note that there are options available to encrypt log files, enable log file validation , send
sns notification for every log file delivery etc. I have used the default values here.You
can allow file encryption and it will ask for encryption key. Click on Create Trail button once
the details are added.
305
AWS Lambda
Add the S3 bucket details to add the trigger and add the following AWS Lambda code:
306
AWS Lambda
Text: {
Data:s3message
}
},
Subject: {
Data: "cloudtrail logs"
}
},
Source: "coxxxxxx@gmail.com"
};
Note that we are taking the S3 bucket and log details from the event and sending mail using
SES service as shown above.
307
AWS Lambda
Whenever any activity takes place in AWS console, the logs will be sent to S3 bucket and at
the same time, AWS lambda will get triggered and the mail will be send to the email id
mentioned in the code.
Note that you can process the logs as per your needs in AWS Lambda.
308
22. AWS Lambda — Using Lambda Function with AWS Lambda
Amazon Kinesis
AWS Kinesis service is used to capture/store real time tracking data coming from website
clicks, logs, social media feeds. We can trigger AWS Lambda to perform additional processing
on this logs.
Requisites
The basic requirements to get started with Kinesis and AWS Lambda are as shown:
Example
Let us work on an example wherein we will trigger AWS Lambda for processing the data
stream from Kinesis and send mail with the data received.
309
AWS Lambda
310
AWS Lambda
There are 4 options as shown. We will work on Create data stream in this example.
Click Create data stream. Enter the name in Kinesis stream name given below.
311
AWS Lambda
Enter the name and click the Create Kinesis stream button at the bottom.
312
AWS Lambda
Click Create function button at the end of the screen. Add Kinesis as the trigger to AWS
Lambda.
313
AWS Lambda
314
AWS Lambda
The event param has the data entered in kinesis data stream. The above aws lambda code
will get activated once data is entered in kinesis data stream.
315
AWS Lambda
316
AWS Lambda
317
23. AWS Lambda — Using Lambda Function with AWS Lambda
We can use AWS lambda function to process using generated events by user application in
the following two ways:
Note that in the above code, we are printing name and address using event.
318
AWS Lambda
The details to the event will be given using the test event created as follows:
319
AWS Lambda
The event details are given to payload and the output is stored at
C:\clioutput\outputfile.txt. as follows:
input.txt
On invoking the Lambda using AWS CLI, you can see the output is as follows:
Similarly, in case you want to test AWS Lambda for any other AWS service, you can do so
using the test event in AWS console and AWS CLI. A sample event for SNS service is shown
below:
{
"Records": [
{
"EventVersion": "1.0",
"EventSubscriptionArn": "arnid",
"EventSource": "aws:sns",
"Sns": {
"SignatureVersion": "1",
320
AWS Lambda
"Timestamp": "1970-01-01T00:00:00.000Z",
"Signature": "EXAMPLE",
"SigningCertUrl": "EXAMPLE",
"MessageId": "95df01b4-ee98-5cb9-9903-4c221d41eb5e",
"Message": "Hello from SNS!",
"MessageAttributes": {
"Test": {
"Type": "String",
"Value": "TestString"
},
"TestBinary": {
"Type": "Binary",
"Value": "TestBinary"
}
},
"Type": "Notification",
"UnsubscribeUrl": "EXAMPLE",
"TopicArn": "topicarn",
"Subject": "TestInvoke"
}
}
]
}
321
AWS Lambda
Let us add the sample event shown above and test it as shown:
In AWS Lambda, code will print the SNS message as shown in the example given below:
322
AWS Lambda
callback(null, event.Records[0].Sns.Message);};
Let us invoke the same using AWS CLI. Let us save the event in a file and use that for payload
using the command shown:
323
24. AWS Lambda — Using AWS Lambda@Edge AWS Lambda
with CloudFront
Lambda@Edge is an addition to the AWS Lambda compute service which is used to customize
the content that cloudfront delivers.
The block diagram which shows the working of AWS Lambda with cloudfront from AWS is
shown below:
Viewer Request: End user makes the request called Viewer Request to CloudFront
Origin Request: CloudFront forwards the request to the origin
Origin Response: CloudFront receives the response from the origin
Viewer Response: CloudFront send the response to the viewer
● Add cookies details to the headers. Carry out AB testing based on the request and
response.
● We can fetch the user-agent from the headers and find out the details of the
browser, OS, etc.
324
AWS Lambda
Requisites
To start with working on CloudFront and Lambda@Edge, we need the following:
We will work on an example with CloudFront and Lambda@Egde, wherein we will host the
page and change the response when detected as desktop and devices.
325
AWS Lambda
Now, click Create bucket button and add the details of the bucket as shown below:
326
AWS Lambda
Create Role
Go to AWS console and click IAM.
327
AWS Lambda
Choose the permission for S3, Lambda and Cloudfront. It is a good practice to create the
policy giving permission to only the required function, storage by using the ARN details.
328
AWS Lambda
In the example discussed below, we are showing the Full Access permission. Policies for the
All the policy required for lambda@edge and cloudfront are as shown above. There is a
additional step to be done here since incase of cloudfront the url will be available across region
and it needs a trust relationship between the services we are using.
329
AWS Lambda
Now, for the role created, click on Trust relationships tab as shown:
330
AWS Lambda
It displays a policy document. We need to add the other services in the Principal -> Service
which we are planning to use. The final trust relationship policy document is as shown below:
331
AWS Lambda
Origin Settings
Origin Domain Name: This is the name of the S3 bucket where we have stored the html
files. We can also store images, if any, in the S3 bucket by creating folders of our choice.
Origin Path: Here you need to enter the name of the folder where the files are stored. At
present, we do not have this folder, so we will keep it blank for now.
Origin ID: It gets populated when the origin domain name is selected. You can change the
id as per your choice.
Restrict Bucket Access: In this, we will choose the option yes. Here we need security for
the S3 bucket so that no one has the access to the S3 bucket. For this option there are some
more options populated like Origin Access Identity, Comment and Grant Read
Permission on Bucket.
Origin Access Identity: We have used create a new identity option.You can also choose the
existing identity. This creates a new identity which is used by CloudFront to read the details
from S3 bucket.
Grand Read Permission on Bucket: For this, choose the option Yes.
Origin Custom Headers: We will keep the headers blank here, as we do not need the details
right now.
332
AWS Lambda
Next, let us discuss and fill up the Behaviour Settings for Cloudront distribution:
333
AWS Lambda
Now, select the protocol – https or http, and the caching option. Note that the default caching
is 86400 or 24 hrs. You can change this value as per the requirement.
Click Object Caching (customize option) to change the caching. You can use smooth
streaming in case if there any videos on your page. Here, we are keeping the default option
available. Once the lambda function is created, its details will be added.
Price class: It has details like the origin of users traffic. Note that here we have selected the
default one - Use All Edge Locations.
AWS WAF Web ACL: This is for web application firewall selection. Here, it has option as
None. First, we need to create the firewall in AWS. It provides security to the site.
Alternate Domain Names: Here you can specify the domain name if you have.
SSL Certificate: This has all the details to be selected for SSL certificate. We will keep the
default ones.
Default Root Object: Here we will specify the filename which we have uploaded in S3. For
this, we need the content from the .html to be displayed by default.
334
AWS Lambda
Note that the distribution will take some time to show the status as deployed.
In AWS Lambda code, we will take the request headers and check the user-agent. If the user-
agent is from desktop, we will change the response to display message as “DESKTOP :
Welcome to AWS Lambda with Cloudfront!” and if device the message will be “MOBILE
DEVICES : Hello from Lambda@Edge!”
let content = `
<\!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple Lambda@Edge Static Content Response</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>MOBILE DEVICES : Hello from Lambda@Edge!</h1>
</body>
335
AWS Lambda
</html>
`;
let content1 = `
<\!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Simple Lambda@Edge Static Content Response</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<h1>DESKTOP : Welcome to AWS Lambda with Cloudfront!</h1>
</body>
</html>
`;
exports.handler = (event, context, callback) => {
let request = event.Records[0].cf.request;
let finalrequest = JSON.stringify(request);
let headers = request.headers;
let useragent = JSON.stringify(headers["user-agent"][0].value);
let str = "";
if(/Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera
Mini|Mobile|mobile|CriOS/i.test(useragent)) {
str = content;
} else {
str = content1;
}
const response = {
status: '200',
statusDescription: 'OK',
body: str+useragent,
};
callback(null, response);
};
336
AWS Lambda
Now, save the Lambda function. Note that we need to publish the Lambda function so that it
can be used with all regions. To publish, we need to do the following:
Now, enter the Version description and click Publish.The ARN will display the version of the
AWS Lambda function created as shown below:
337
AWS Lambda
Now, add the configuration details for CloudFront .The CloudFront event has option for
Viewer request, Origin request, Origin response, and Viewer response.
Next, choose the CloudFront distribution created earlier. From events, we will select Viewer
request. Based on the viewer request, the desktop/device from user-agent will be decided
and the response will be changed. Next, add the trigger details.
338
AWS Lambda
Once the trigger is added, we need to wait for the distribution from CloudFront to be deployed.
Once the status is changed to Deployed, we can test the CloudFront url and check the domain
name in browser.
The display in desktop browser is as shown below. Here we have printed the user-agent from
the viewer-request event.
339
AWS Lambda
Thus, in the above example, we have used Lambda@Edge to change response on desktop
and mobile device.
340
AWS Lambda
341
25. AWS Lambda — Monitoring and AWS Lambda
Functions created in AWS Lambda are monitored by Amazon CloudWatch. It helps in logging
all the requests made to the Lambda function when it is triggered.
Consider that the following code is uploaded in AWS Lambda with function name as
lambdaandcloudwatch.
When the function is tested or triggered, you should see an entry in Cloudwatch. For this
purpose, go to AWS services and click CloudWatch.
342
AWS Lambda
343
AWS Lambda
When you click Logs, it has the Log Groups of AWS Lambda function created in your account.
Select any AWS Lambda function and check the details. Here, we are referring to Lambda
function with name:lambdaandcloudwatch. The logs added to the Lambda function are
displayed here as shown below:
Now, let us add S3 trigger to the Lambda function and see the logs details in CloudWatch as
shown below:
344
AWS Lambda
Let us update AWS Lambda code to display the file uploaded and bucket name as shown in
the code given below:
345
AWS Lambda
When the file is uploaded, AWS Lambda functions will get triggered and the console log
messages from Lambda code are displayed in CloudWatch as shown below:
If there is any error, CloudWatch gives the error details as shown below:
346
AWS Lambda
Note that we have referred to the bucket name wrongly in AWS Lambda code as shown:
The bucket name reference from the event is wrong. Thus, we should see an error displayed
in CloudWatch as shown below:
347
AWS Lambda
CloudWatch Metrics
The details of the Lambda function execution can be seen in the metrics. Click Metrics
displayed in the left side.
348
AWS Lambda
The graph details for the lambda function lambdaandcloudwatch are as shown below:
It gives details such as the duration for which the Lambda function is executed, number of
times it is invoked and the errors from the Lambda function.
349
26. AWS Lambda —Additional Example AWS Lambda
Till now, we have seen working of AWS Lambda with AWS services. Based on that knowledge,
let us create a simple user registration form and post the data using API gateway to AWS
Lambda. AWS Lambda will get the data from the event or the API gateway trigger and will
add those details to DynamoDB table.
Example
Let us consider an example and perform the following functionalities on it:
You can use the following details to create DynamodDB table in AWS console. First, go to
AWS Service and click DynamoDB. Click Table to create the table as shown below:
350
AWS Lambda
You can use the ARN to create policy for the DynamoDB to be used with AWS Lambda.
Go to IAM and select Policies. Click Create policy, choose service as DynamodDB as shown
below:
351
AWS Lambda
Click All DynamoDB actions as shown above. Choose resource and enter the ARN for table
as shown below:
352
AWS Lambda
If you click Review policy button at the end of the screen, you can see the following window:
Enter name of the policy and click Create policy button at the end of the page. Now, we
need to create role to be used with Lambda. We need permissions for DynamoDB, API
Gateway and Lambda.
Go to AWS services and select IAM. Select Roles from left side and add the required roles.
Enter the role name and click Create role. The role created is roleforlambdaexample.
353
AWS Lambda
Create AWS Lambda and API Gateway to Send OTP Message to Phone
using SNS service
If you see the user registration form, there is a button validate phone. User is suppose to
enter phone number and click on validate phone button to validate the phone number.
When a user clicks this button, the API gateway post method which contains the phone details
is called and internally AWS Lambda is triggered.
Then, AWS Lambda sends OTP to the phone number entered using AWS SNS service.
The user receives the OTP and has to enter this OTP number.
The textbox to enter OTP will appear when the phone number is entered and validate phone
button is clicked.
The OTP received from AWS Lambda and the OTP entered by the user has to match,to allow
the user to submit the user registration form.
354
AWS Lambda
A simple block diagram that explains the working of phone validation is shown here:
355
AWS Lambda
Note that we are using SNS service to send the OTP code. This code is used to validate the
mobile number entered by the user in the user registration form. The API gateway created
for above phone validation is as follows:
356
AWS Lambda
The Lambda function given is phonevalidationexample. We are taking the mobile phone
details here to be used inside AWS Lambda. Then, AWS Lambda will send the OTP code to
the given mobile number.
Create AWS Lambda and API Gateway to POST Form Data and Insert in
DynamoDB Table
For user registration form, all the fields are mandatory. There is an AJAX call made wherein
the data entered in the form is posted to the API Gateway URL.
A simple block diagram which explains the working of the submit button is shown here:
Once the form is filled, the submit button will call the API gateway which will trigger AWS
Lambda. AWS Lambda will get the details of the form from event or the API Gateway and the
data will be inserted in the DynamodDB table.
First, go to AWS services and click Lambda. The Lambda function created is as shown here:
Now, to create an API gateway, go to AWS service and select API Gateway. Click on Create
API button shown below.
357
AWS Lambda
Enter the API name and click on Create API button to add the API.
Now, an API is created called as registeruser. Select the API and click Actions dropdown to
create Resource.
358
AWS Lambda
Click Create Resource. Now, let us add the POST method. For this, click on Resources
created on left side and from Actions dropdown select create method. This will display
dropdown as shown below:
Select the POST method and add the Lambda function that we created above.
359
AWS Lambda
Click Save button to add the method. To send the form details to Lambda function
lambdaexample we need to add the Integration Request as shown below:
360
AWS Lambda
To post the form details, you will have to click Integration Request. It will display below
details.
361
AWS Lambda
Next, click Add mapping template and enter the content type. Here, we have added
application/json as the content type. Click it and here you need to enter the field in json
format as shown below:
362
AWS Lambda
Now, click the Save button and deploy the API as shown below:
Here is the API created for POST which will use inside
our .html file.Please note we need to Enable CORS for
the resource created.Will use the api gateway url to
make ajax call so the CORS has to enabled.
Select the Methods on which you want to enable the CORS.Click on Enable CORS and
replace existing CORS headers.
363
AWS Lambda
The AWS Lambda code for POST API Gateway is as shown here:
364
AWS Lambda
first_name:event.fname,
last_name:event.lname,
emailid:event.emailid,
mobile_no : event.mphone,
otp:event.otp,
username:event.uname,
password:event.passwd,
confirm_password:event.cpasswd
}
}
docClient.put(data,function(err, value){
if (err) {
console.log("Error");
callback(err, null);
} else {
console.log("data added successfully");
callback(null, value);
}
});
}
The event parameter in AWS Lambda handler will have all the details which are added earlier
in POST integration request. The details from event are added to the DynamodDB table as
shown in the code.
Now, we need to get the service details from AWS-SDK as shown below:
365
AWS Lambda
mobile_no : event.mphone,
otp:event.otp,
username:event.uname,
password:event.passwd,
confirm_password:event.cpasswd
}
}
docClient.put(data,function(err, value){
if (err) {
console.log("Error");
callback(err, null);
} else {
console.log("data added successfully");
callback(null, value);
}
});
Create AWS Lambda and API Gateway to Read Data from DynamodDB
Table
Now, we will create AWS Lambda function to read data from DynamoDB table. We will trigger
API Gateway to the AWS Lambda function which will send data to the html form.
366
AWS Lambda
Here the data is read from the DynamoDB table and given to the callback. Now, we will create
API Gateway and add AWS Lambda function as the trigger.
367
AWS Lambda
Lambda function added is lambdareaddataexample. Click Save to save the method and
deploy the api.
368
AWS Lambda
Now, enter the details as shown above. Note that the submit button is disabled. It will be
enabled only when all the details are entered as shown:
369
AWS Lambda
Now, enter the mobile number and click validate phone button. It will display the alert
message saying “OTP is send to the mobile, please enter the OTP to continue”. OTP
sent to the mobile number is as follows:
370
AWS Lambda
Enter the OTP and remaining details and submit the form.
371
AWS Lambda
example1.html
<html>
<head>
<script
src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script type="text/javascript" src="formdet.js"></script>
<style>
input[type=text],input[type=password],button {
width: 100%;
padding: 5px 5px;
margin: 5px 0;
box-sizing: border-box;
}
#maincontainer {
width: 80%;
margin: auto;
padding: 10px;
}
div#userregistration {
width: 60%;
float: left;
}
div#userdisplay {
margin-left: 60%;
}
</style>
</head>
<body>
<div id="maincontainer">
<div id="userregistration">
<h1>User Registration Form</h1>
<table border="0">
372
AWS Lambda
<tr>
<td><b>First Name<span style="color:red;">*</span> : </b></td><td><input
type="text" value="" name="fname" id="fname" /></td>
<td id="tdfname" style="display:none;"><span style="color:red;">Enter First
Name</span></td>
</tr>
<tr>
<td><b>Last Name<span style="color:red;">*</span> : </b></td><td><input
type="text" value="" name="lname" id="lname" /> </td>
<td id="tdlname" style="display:none;"><span style="color:red;">Enter Last
Name</span></td>
</tr>
<tr>
<td><b>Email Id<span style="color:red;">*</span> : </b></td><td><input type="text"
value="" name="emailid" id="emailid" /> </td>
<td id="tdemailid" style="display:none;"><span style="color:red;">Enter
Email</span></td>
</tr>
<tr>
<td><b>Mobile No<span style="color:red;">*</span> : </b></td><td><input
type="text" name="mphone" id="mphone"/></td>
<td id="tdmphone" style="display:none;"><span style="color:red;">Enter Mobile
Number</span></td>
</tr>
<tr>
<td></td>
<td><button id="validatephone">validate phone</button></td>
<td></td>
</tr>
<tr id="otpdiv" style="display:none;">
<td><b>Enter OTP<span style="color:red;">*</span>:</b> </td><td><input
type="text" value="" name="otp" id="otp" /></td>
<td id="tdotp" style="display:none;"><span style="color:red;">Enter
OTP</span></td>
</tr>
<tr>
<td><b>Username<span style="color:red;">*</span>: </b></td><td><input type="text"
value="" name="uname" id="uname"/></td>
373
AWS Lambda
374
AWS Lambda
</html>
formdet.js
function validateform() {
var sError="";
if ($("#fname").val() === "") {
$("#tdfname").css("display","");
sError++;
}
if ($("#lname").val() === "") {
$("#tdlname").css("display","");
sError++;
}
if ($("#emailid").val() === "") {
$("#tdemailid").css("display","");
sError++;
}
if ($("#mphone").val() === "") {
$("#tdmphone").css("display","");
sError++;
}
if ($("#otp").val() === "") {
$("#tdotp").css("display","");
sError++;
}
if ($("#uname").val() === "") {
$("#tduname").css("display","");
sError++;
}
if ($("#passwd").val() === "") {
$("#tdpasswd").css("display","");
sError++;
}
if ($("#cpasswd").val() === "") {
375
AWS Lambda
$("#tdcpasswd").css("display","");
sError++;
}
if (sError === "") {
return true;
} else {
return false;
}
}
$("#fname").change(function(){
if ($("#fname").val() !== "") {
$("#tdfname").css("display","none");
} else {
$("#tdfname").css("display","");
}
});
$("#lname").change(function(){
if ($("#lname").val() !== "") {
$("#tdlname").css("display","none");
} else {
$("#tdlname").css("display","");
}
});
$("#emailid").change(function(){
if ($("#emailid").val() !== "") {
$("#tdemailid").css("display","none");
} else {
$("#tdemailid").css("display","");
}
});
$("#mphone").change(function(){
if ($("#mphone").val() !== "") {
$("#tdmphone").css("display","none");
376
AWS Lambda
} else {
$("#tdmphone").css("display","");
}
});
$("#otp").change(function(){
if ($("#otp").val() !== "") {
$("#tdotp").css("display","none");
} else {
$("#tdotp").css("display","");
}
});
$("#uname").change(function(){
if ($("#uname").val() !== "") {
$("#tduname").css("display","none");
} else {
$("#tduname").css("display","");
}
});
$("#passwd").change(function(){
if ($("#passwd").val() !== "") {
$("#tdpasswd").css("display","none");
} else {
$("#tdpasswd").css("display","");
}
});
$("#cpasswd").change(function(){
if ($("#cpasswd").val() !== "") {
$("#tdcpasswd").css("display","none");
} else {
$("#tdcpasswd").css("display","");
377
AWS Lambda
}
});
$(document).ready(function(){
378
AWS Lambda
$("#otp").on("change", function(){
var otpentered = $("#otp").val();
if (otpsend == otpentered) {
document.getElementById("submit").disabled = false;
} else {
alert("OTP is not valid.Please enter the valid one or validate phone
again to continue!");
document.getElementById("submit").disabled = true;
}
});
$("#validatephone").on("click",function(){
$.ajax({
type:'POST',
url:phonevalidationurl,
data:JSON.stringify({
"mphone":$("#mphone").val()
}),
success: function(data){
$("#otpdiv").css("display", "");
alert("OTP is send to the mobile, please enter to continue");
console.log(data);
otpsend = data;
},
error : function(err) {
$("#otpdiv").css("display", "none");
alert("Invalid mobile no.");
}
});
});
$("#submit").on("click",function(){
if (validateform()){
$.ajax({
type:'POST',
url:posturl,
379
AWS Lambda
data:JSON.stringify({
"fname": $("#fname").val(),
"lname": $("#lname").val(),
"emailid":$("#emailid").val(),
"mphone":$("#mphone").val(),
"otp":$("#otp").val(),
"uname":$("#uname").val(),
"passwd":$("#passwd").val(),
"cpasswd":$("#cpasswd").val()
}),
success: function(data){
alert("Data added successfully");
console.log(data);
getdata();
}
});
}
});
getdata();
});
Till now, we have done AJAX call to the API created and posted the data as shown above.
380
AWS Lambda
$("#submit").on("click",function(){
if (validateform()){
$.ajax({
type:'POST',
url:posturl,
data:JSON.stringify({
"fname": $("#fname").val(),
"lname": $("#lname").val(),
"emailid":$("#emailid").val(),
"mphone":$("#mphone").val(),
"otp":$("#otp").val(),
"uname":$("#uname").val(),
"passwd":$("#passwd").val(),
"cpasswd":$("#cpasswd").val()
}),
success: function(data){
alert("Data added successfully");
console.log(data);
getdata();
}
});
}
});
});
Note that to read the data, a function is called, whose code is given below:
function getdata() {
var a =0;
381
AWS Lambda
$.ajax({
type:'GET',
url:posturl,
success: function(data){
$("#displaydetails").html('');
$("#displaydetails").css("display", "");
console.log(data);
$("#displaydetails").append('<tr
style="padding:5px;margin:5px;background-color:gray;"><td>Name</td><td>Mobile
No</td><td>EmailID</td></tr>');
data.Items.forEach(function(registeruser){
var clr = (a%2 === 0) ? "#eee": "white";
a++;
$("#displaydetails").append('<tr
style="padding:5px;margin:5px;background-
color:'+clr+'"><td>'+registeruser.first_name+'-
'+registeruser.last_name+'</td><td>'+registeruser.mobile_no+'</td><td>'+registerus
er.emailid+'</td></tr>');
});
},
error: function(err) {
console.log(err);
}
});
}
When you click mobile number validate button, the following code is called and sends the
mobile number:
382
AWS Lambda
383