Debug School

rakesh kumar
rakesh kumar

Posted on

Creating and deleting AWS lambda function in Aws Console

Creating Lambda function in AWS

Deleting Lambda function in AWS

Creating Lambda function in AWS

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.

Image description

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

Image description

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

Image description

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 −

Image description

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

Image description

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

Image description

Step 7
The code for index.js is as follows −

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

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 −

Image description

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 −

Image 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 −

Image description

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 −

Image description

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

Image description

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 −

Image description

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.

Image description

Observe the following screenshot which shows S3 trigger added to AWS Lambda −

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"
   }]
}
Enter fullscreen mode Exit fullscreen mode

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 
Enter fullscreen mode Exit fullscreen mode
//will display the name of the file
Enter fullscreen mode Exit fullscreen mode

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

event.Records[0].s3.bucket.name
Enter fullscreen mode Exit fullscreen mode
//will give the name of the bucket.
Enter fullscreen mode Exit fullscreen mode

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

event.Records[0].eventName 
Enter fullscreen mode Exit fullscreen mode
   // will display the event name
Enter fullscreen mode Exit fullscreen mode

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 );
};
Enter fullscreen mode Exit fullscreen mode

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

Image description

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

Image description

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");
         }
   });
};
Enter fullscreen mode Exit fullscreen mode

Deleting Lambda Function

Deleting AWS Lambda function will remove the AWS Lambda from the AWS console. There are 2 ways to delete AWS Lambda function.

Using AWS console.
Using AWS CLI command
This chapter discusses these two ways in detail.

Using AWS Console
For deleting a Lambda function using AWS console, follow the steps given below −

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 −

AWS Console Using
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.

Image description

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 −

Image description
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 −

Image description

Step 4
Select the Delete button to delete the AWS Lambda function. Once you click Delete, it displays the message as follows −

Image description

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.

Image description

Delete Role
Click the checkbox across the role you want to delete. You can also select multiple roles to delete at a time.

Image description

Delete Time
Step 7
You will see a confirmation message as shown below once you click Delete button −

Image description

Delete Button
Now, read the details mentioned carefully and later click Yes, delete button.

Using AWS CLI command

Let us first create a Lambda function using aws cli and delete the same using the same command. Follow the Steps given below for this purpose −

Step 1
The command with values for create-function is as follows −

aws lambda create-function 
--function-name "lambdatestcli" 
--runtime "nodejs8.10" 
--role "arn:aws:iam::625297745038:role/lambdaapipolicy" 
--handler "index.handler" 
--timeout 5 
--memory-size 256 
--zip-file "fileb://C:\demotest\index.zip"
Enter fullscreen mode Exit fullscreen mode

The corresponding output is shown here −

Image description

CLI command
Step 2
The AWS Lambda function created is lambdatestcli. We have used existing role arn to create the lambda function.

Then you can find this function displayed in AWS console as shown below −

Image description

Step 3
Now, let us invoke the function to test the output using the command shown −

Image description

aws lambda invoke --function-name "lambdatestcli" --log-type Tail 
C:\demotest\outputfile.txt
Enter fullscreen mode Exit fullscreen mode

This command will give you the output as shown −

Command Output
Step 4
You can observe logs from cloudwatch for lambda function lambdatestcli

Image description

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>]
Enter fullscreen mode Exit fullscreen mode

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.

-- 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.

--generate-cli-skeleton(string) − it prints json skeleton to standard output without sending the API request.

Command with values

aws lambda delete-function --function-name "lambdatestcli
Enter fullscreen mode Exit fullscreen mode

"
The corresponding output is shown below −

Lambda Delete Function
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

Image description

aws_lambda_creating_and_deploying_using_aws_console
aws_lambda_deleting_lambda_function

Top comments (0)