In this tutorial, you will learn how to create, test, and update a Lambda function. You will also learn how to publish a new version and create an alias for a Lambda function. You can learn basics about Serverless here.
Here’s how you can create an AWS Lambda function through the AWS Lambda Console:
Once you have clicked the button, you will be presented with three different options to create an AWS Lambda function:
1: Authoring an AWS Lambda function from scratch.
2: Creating an AWS Lambda function from a list of preconfigured blueprints to kickstart your AWS Lambda functions.
3: Pick an already published serverless app from the Serverless Application Repository, which consists of apps published by developers and companies on AWS
Select the Blueprints option:
In the Blueprints filter box, add a filter called hello-world and select the blueprint called hello-world.
You should see A starter AWS Lambda function in Node.js. Click on the Configure button at the bottom:
Now, you will be presented with options to configure the AWS Lambda function that you are creating:
The other configuration setting that is very important is the name of the handler for your AWS Lambda function.
For the one that you created, set it as index.handler, which is the module-name.export value of your AWS Lambda function.
In this case, the AWS Lambda function will call exports.handler in the index.js file that was created as part of the blueprint:
If you examine the code that the blueprint created, it will look like the following:
console.log(‘Loading function’);
exports.handler = async (event, context) => {
// console.log(‘Received event:’, JSON.stringify(event, null, 2));
console.log(‘value1 =’, event.key1);
console.log(‘value2 =’, event.key2);
console.log(‘value3 =’, event.key3);
return event.key1; // Echo back the first key value
// throw new Error(‘Something went wrong’);
};
You can find this AWS Lambda function code here.
The preceding code prints the whole event that triggered the AWS Lambda function (which is commented out). Then it prints three different keys from the event, named key1, key2, and key3 and finally returns key1. When you test this AWS Lambda function through the console, you need to make sure that the test event has these three keys defined.
It’s time to take a look at how you can test the newly created AWS Lambda function using the AWS Lambda Console. At this point, as you don’t have any triggers defined for your AWS Lambda function, you will just have to use the AWS Lambda console to configure a test event and invoke your AWS Lambda function.
To test the newly created AWS Lambda function:
{“key3”: “value3”,“key2”: “value2”,“key1”: “value1”}
Here is a screenshot of Configure test event:
Now, ensure that the myHelloWorldEvent event is selected in the events list to test your AWS Lambda function in the Console, next to the Test button:
Now, when you click the Test button, you will see an execution result saying that it succeeded, providing a link to CloudWatch Logs, so you can view the log of your AWS Lambda function execution.
In the console, you will also see the value of key1 logged, which in this case is value1, since that is what was configured in the test event:
The console output also logs the Summary of this AWS Lambda function execution, including the following:
The summary section also includes a link to the CloudWatch log group:
If you navigate to the CloudWatch Log Group link that is available in the Summary, you can view the logs that are generated by your AWS Lambda function. In this case, the function outputs the three different values of the keys that are configured in the test event used to test the Lambda function:
Now that you are able to successfully test your AWS Lambda function through the console, it’s time to look at how you can make changes to the Lambda function through the console, deploy those changes through the console, and see if your changes are reflected by testing the AWS Lambda function as in the following steps:
console.log(‘Loading function’);
exports.handler = async (event, context) => {
console.log(‘Received event:’, JSON.stringify(event, null, 2));
console.log(‘value1 =’, event.key1);
console.log(‘value2 =’, event.key2);
console.log(‘value3 =’, event.key3);
return event.key1; // Echo back the first key value
// throw new Error(‘Something went wrong’);
};
You can find the preceding AWS Lambda function code here.
Once the changes have been made, click on the Save button in the top right-hand corner, which deploys the changes to the $LATEST version of your AWS Lambda function.
With the changes in place, click on the Test button with myHelloWorldEvent selected.
In the output of your AWS Lambda function execution, you should now see the whole event printed, along with the values of each key:
By default, when you update and save the AWS Lambda function, it gets saved to $LATEST version. However, if you want to create a snapshot of the AWS Lambda function’s $LATEST version for future reference, AWS provides a mechanism to do so by publishing the function to a named version. You can do this through the AWS console by performing the following steps:
In the dialog window, enter a value for the description of the version (for example, Snapshot created on June 1st 2018) and click on the Publish button:
Once the Publish button is pressed, a new version will be published for your AWS Lambda function, and the version number will be automatically set to 1 by the AWS Lambda platform (assuming this is the first time you are creating a version for your AWS Lambda function).
As you publish new versions, they will be auto-incremented by the AWS Lambda platform.
You can see the available versions for your AWS Lambda function by selecting the Version dropdown:
There will be cases where you will need to create slightly different versions of your AWS Lambda function for different environments (test and production), for different environment-specific settings. To handle this use case, the AWS Lambda platform provides you with the capability to create an alias, which points to one or more versions of your AWS Lambda function. Here’s how you can create an alias for your AWS Lambda function:
In the AWS console, from the Actions dropdown, select Create alias:
In the dialog window that opens up:
With the alias created, when you invoke your AWS Lambda function from an HTTP event trigger such as the Amazon API Gateway or an S3 event trigger, you can specify the alias of your AWS Lambda function that needs to be invoked.
We have covered step by step instructions to create, update the AWS lambda function and create their alias. You can checkout this book for more information on the same.
Originally published at codeforgeek.com on August 29, 2018.