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?
- Go straight to the AWS Lambda console.
- Click on Create a function.
- In Function name, enter the following name for your function:
- Click on 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.
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:
- Select Test from the top right.
- 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:
- “key3”: “value3”,
- “key2”: “value2”,
- “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.
- 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.
- Your function will get executed by Lambda for you and the lambda function handler will receive then it will start processing the sample event.
- 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.
- You will need to run your Lambda function a couple of times for the sake of getting a few metrics for viewing up next.
- Click on Monitoring where you will be able to check graphs for specific metrics sent by Lambda to CloudWatch.
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:
- Go to the Lambda console and open your Functions page.
- Select one of the functions.
- Click on Actions, then select the option Delete function.
- Click on Delete.
Deleting log group of the function:
- Go to the CloudWatch console and head to the Log groups page.
- Select the log group of the function which looks like this /aws/lambda/my-function.
- Click on Actions, then select the option Delete log group.
- Click on Yes, Delete.
Deleting execution role of the function:
- Go to the IAM console and open the Roles page.
- Select the role of the function which looks like this my-function-role-31exxmpl
- Click on Delete role.
- 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
Description: Call the AWS Lambda API
# Function’s execution role
– !Ref libs
Description: Dependencies for the blank sample app.
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?
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.