AWS Lambda How to Create A Function

Creating Lambda functions using the console:

In the following tutorial we will be learning how to create a function from our Lambda console. After that, we will get to invoke this Lambda function with some sample event data.

Your Lambda will be executing the function and returning its results. When this is done, you will have to verify those execution results and the created logs along with different CloudWatch metrics.

How to get a Lambda function created?

  1. Go straight to the AWS Lambda console.

    AWS Lambda How to Create a Function - AWS Lambda Console

    AWS Lambda How to Create a Function – AWS Lambda Console

  2. Click on Create a function.

    AWS Lambda How to Create a Function - AWS Lambda Create A Function

    AWS Lambda How to Create a Function – AWS Lambda Create A Function

  3. In Function name, enter the following name for your function:

my-function.

AWS Lambda How to Create a Function - AWS Lambda Enter Function Name

AWS Lambda How to Create a Function – AWS Lambda Enter Function Name

  1. Click on Create function.

    AWS Lambda How to Create a Function - AWS Lambda Click Create Function

    AWS Lambda How to Create a Function – AWS Lambda Click Create Function

What happens now?

Lambda will be creating a Node.js function as well as an execution role which will be giving your function the required permission for uploading logs. Also, Lambda will be assuming the execution role upon invoking the function, then creates with it the credentials for SDK and reading data from event sources.

How to Use the designer?

Your Designer will display for you the following:

– Function’s overview

– Function’s upstream resources
– Functions downstream resources

The Designer helps you in configuring the following: layers, destinations and triggers.

AWS Lambda How to Create a Function - AWS Lambda Designer

AWS Lambda How to Create a Function – AWS Lambda Designer

You can click on my-function which is found in the designer so that you get back to the configuration and code of your function. When using scripting languages, your Lambda will give you some sample code which is capable of returning a success response.

How to Invoke Lambda function?

Invoke your Lambda function using the sample event data provided in the console.

For invoking a function, follow the below listed steps:

  1. Select Test from the top right.

    AWS Lambda How to Create a Function - AWS Lambda Test Button

    AWS Lambda How to Create a Function – AWS Lambda Test Button

  2. From Configure test event page, select the option Create new test event then for the Event template, do not change the default option for Hello World. Type in a unique Event name then take into consideration the below shown sample event template:
  3. {
  4. “key3”: “value3”,
  5. “key2”: “value2”,
  6. “key1”: “value1”

}

It is possible to put different key and values in the sample JSON yet without changing any of the event’s structure. In case of choosing to change specific keys and values, it will be required for you to update your sample code with the newly entered parameters.

  1. Click on Create then select Test. Every single user is capable of creating up to ten test events for each function and they will not be available to different users.
  2. Your function will get executed by Lambda for you and the lambda function handler will receive then it will start processing the sample event.
  3. When the execution succeeds, go and check the results using the console.

– Execution result: displays succeeded execution status and the results for your function execution which get returned using the return statement.

– Summary: displays key data which is found in the section of Log output under the REPORT line shown in execution log.

Log output: displays log generated by Lambda upon every execution. Those generated logs are the ones Lambda Function writes to CloudWatch and they are displayed by the Lambda console to simplify things for you.

Keep in mind the fact that the Click here link will display logs using the CloudWatch console and then the function is going to add logs to CloudWatch inside the log group which is associated with the Lambda function.

  1. You will need to run your Lambda function a couple of times for the sake of getting a few metrics for viewing up next.
  2. Click on Monitoring where you will be able to check graphs for specific metrics sent by Lambda to CloudWatch.

    AWS Lambda How to Create a Function - AWS Lambda CloudWatch Metrics

    AWS Lambda How to Create a Function – AWS Lambda CloudWatch Metrics

How to perform the Clean up?

If you are done working with the example function, delete it. You can also delete the execution role that the console created, and the log group that stores the function’s logs.

Deleting Lambda function:

  1. Go to the Lambda console and open your Functions page.
  2. Select one of the functions.
  3. Click on Actions, then select the option Delete function.
  4. Click on Delete.

Deleting log group of the function:

  1. Go to the CloudWatch console and head to the Log groups page.
  2. Select the log group of the function which looks like this /aws/lambda/my-function.
  3. Click on Actions, then select the option Delete log group.
  4. Click on Yes, Delete.

Deleting execution role of the function:

  1. Go to the IAM console and open the Roles page.
  2. Select the role of the function which looks like this my-function-role-31exxmpl
  3. Click on Delete role.
  4. Select the option Yes, delete.

You can automate Cleanup and creation of functions, log groups and roles using CloudFormation and CLI.

 

How to perform Dependency management with layers?

It’s possible to locally install libraries and add them to the deployment package uploaded to Lambda, yet you need to know that doing so actually has its bad sides and some of which are:

– Bigger files will increase your deployment times

– It will not allow you to use the Lambda console for testing changes to your function code

How to solve this?

You will need to make sure that your deployment package as small as possible while avoiding the uploading of unchanged dependencies, and this way, the sample application will get to create a Lambda layer and get it associated with the selected function.

Example of a blank-nodejs/template.yml Dependency layer

Resources:

function:

Type: AWS::Serverless::Function

Properties:

Handler: index.handler

Runtime: nodejs12.x

CodeUri: function/.

Description: Call the AWS Lambda API

Timeout: 10

# Function’s execution role

Policies:

– AWSLambdaBasicExecutionRole

– AWSLambdaReadOnlyAccess

– AWSXrayWriteOnlyAccess

Tracing: Active

Layers:

        – !Ref libs

  libs:

    Type: AWS::Serverless::LayerVersion

    Properties:

      LayerName: blank-nodejs-lib

      Description: Dependencies for the blank sample app.

      ContentUri: lib/.

      CompatibleRuntimes:

        – nodejs12.x

2-build-layer.sh script: It will install the dependencies of your function using npm then add them to a folder having the necessary structure for Lambda runtime.

Example of 2-build-layer.sh How to prepare the layer?

#!/bin/bash

set -eo pipefail

mkdir -p lib/nodejs

rm -rf node_modules lib/nodejs/node_modules

npm install –production

mv node_modules lib/nodejs/

When the sample application gets deployed, the CLI will package the layer apart from the function code, then it goes ahead with deploying them.

When performing subsequent deployments, you will find that the layer archive will merely get uploaded in case the lib folder includes changed contents.

AWS lambda Synchro or Asynchro

 


AUTHOR