Skip to main content

Creating and Deploying using AWS Console

 We can create Lambda function and test the same in AWS console. This chapter discusses this in detail. For this purpose, you will have to follow the steps given here and observe the respective screenshots given −

Step 1

Login to AWS Console https://aws.amazon.com/console/. Now, you will be redirected to the screen where the AWS services are displayed.

AWS Services
Step 2

Now, click on Lambda service as highlighted above. This will redirect to create function as shown below −

Lambda Services
Step 3

Now, click Create function and enter the details of the function. Then you can see a screen as shown below −

My First Lambda
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 −

Code Entry
Step 5

Once done you need to save the changes for which the button is given at the top right corner as shown below −

Save Button Lambda
Step 6

Now, click Test button. This gives all details of the execution of the Lambda function as shown below −

Test Button
Step 7

The code for index.js is as follows −

exports.handler = (event, context, callback) => {
// TODO implement
callback
(null, 'Lambda test');
};

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

The Role details for Lambda function is a part of the configuration and is displayed as shown below −

Role Execution
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 −

Description
Step 10

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 −

Designer

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 −

Configure Trigger
Step 11

Now, select the bucket you want the trigger on. The event type has the following details −

Event Type
Step 12

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 −

Prefix
Step 13

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 −

Cloud Formation
Step 14

Now, let us use S3 sample event to test the Lambda function. The code for the same is shown here −

Amazon S3 Put Sample Event

{
"Records": [{
"eventVersion": "2.0",
"eventTime": "1970-01-01T00:00:00.000Z",
"requestParameters": {
"ExampleIPAddress": "127.0.0.1"
},
"s3": {
"configurationId": "testConfigRule",
"object": {
"eTag": "0123456789abcdef0123456789abcdef",
"sequencer": "0A1B2C3D4E5F678901",
"key": "HappyFace.jpg",
"size": 1024
},
"bucket": {
"arn": bucketarn,
"name": "Examplebucket",
"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 −

event.Records[0].s3.object.key     //will display the name of the file

You will have to use the following command to get the bucket name −

event.Records[0].s3.bucket.name    //will give the name of the bucket.

You will have to use the following command to get the EventName −

event.Records[0].eventName        // will display the event name


Step 15

Now, let us update AWS Lambda code to print the S3 details as shown below −

exports.lambdahandler = (event, context, callback) => {
callback
(null, "Bucket name: "+event.Records[0].s3.bucket.name+"
File name:"
+event.Records[0].s3.object.key );
};


Step 16

Save the changes. Click Test and enter the S3 sample event −

Configure Test


Step 17

Now click Test and you can see the output as shown −

Bucket Name
Step 18

To test the trigger on S3 using S3 AWS service, upload a file in S3 bucket: test bucket trigger. Update the role used with Lambda to take S3 and SES policy(to send mail) for permissions. This will update AWS Lambda code to send mail to see the trigger working −

The updated AWS Lambda code is as shown −

var aws = require('aws-sdk');
var ses = new aws.SES({
region
: 'us-east-1'
});
exports
.lambdahandler = function(event, context, callback) {
var eParams = {
Destination: {
ToAddresses: ["coxxxxxxx@gmail.com"]
},
Message: {
Body: {
Text: {
Data: "Bucket name: "+event.Records[0].s3.bucket.name+" File name:"+event.Records[0].s3.object.key
}
},
Subject: {
Data: "S3 and AWS Lambda"
}
},
Example: "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");
}
});
};

The corresponding screenshot is as shown here −

Target Path
Step 19

Now, upload the file and check the mail id provided in AWS Lambda code −

Mail Id

Comments

Popular posts from this blog

PERL Some good framework

1. Catalyst is the most popular agile Perl MVC web framework that encourages rapid development and clean design without getting in your way. Catalyst | Perl MVC web application framework 2. Mojolicious is a next generation web framework for the Perl programming language. Back in the early days of the web, many people learned Perl because of a wonderful Perl   ... Mojolicious - Perl real-time web framework 3. Documents for Perl  The Perl Archive Network, the gateway to all things Perl. The canonical location for Perl code and modules. The Comprehensive Perl Archive Network - www. cpan .org

C++ How to use Date and Time

The C++ standard library does not provide a proper date type. C++ inherits the structs and functions for date and time manipulation from C. To access date and time related functions and structures, you would need to include <ctime> header file in your C++ program. There are four time-related types: clock_t, time_t, size_t , and tm . The types clock_t, size_t and time_t are capable of representing the system time and date as some sort of integer. The structure type tm holds the date and time in the form of a C structure having the following elements: struct tm { int tm_sec ; // seconds of minutes from 0 to 61 int tm_min ; // minutes of hour from 0 to 59 int tm_hour ; // hours of day from 0 to 24 int tm_mday ; // day of month from 1 to 31 int tm_mon ; // month of year from 0 to 11 int tm_year ; // year since 1900 int tm_wday ; // days since sunday int tm_yday ; // days since January 1st int tm_isdst ; // hours of daylight savin...

Lambda Function with 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 − Create a table in DynamoDB with primary key Create a role which will have permission to work with DynamoDBand AWS Lambda. Create function in AWS Lambda AWS Lambda Trigger to send mail Add data in DynamoDB Let us discuss each of this step in detail. 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 ...