How to create AWS Lambda function
By: Ankita Agarwal
Prerequisites for creating an AWS lambda function
- An AWS account.
- Knowledge of anyone of the programming languages like C#, Go, Java, Node.js, Python.
- Ability to write small programs as the maximum execution timeout for AWS Lambda function is 15 minutes.
- Default deployment package size is 50 MB so, need to calculate the memory requirements.
Create lambda function with the console
To create AWS Lambda function
- Open the AWS Lambda console.
- Choose Create a function.
- For Function name, enter
- Choose Create function.
Lambda creates a Node.js function (or whichever language you have chosen) and an execution role that grants the function permission to upload logs. Lambda assumes the execution role when you invoke your function.
The Designer shows an overview of your function and its upstream and downstream resources. You can use it to configure triggers, layers, and destinations.
- Triggers – Triggers are services and resources that you have configured to invoke your function. Choose Add trigger to create a Lambda event source mapping or to configure a trigger in another service that the Lambda console integrates with.
- Layers – Choose the Layers node to add layers to your application. A layer is a ZIP archive that contains libraries, a custom runtime, or other dependencies.
- Destinations – Add a destination to your function to send details about invocation results to another service. You can send invocation records when your function is invoked asynchronously, or by an event source mapping that reads from a stream.
Choose lambda in the designer to return to the function’s code and configuration. For scripting languages, Lambda includes sample code that returns a success response.
- Code– The code and dependencies of your function. For scripting languages, you can edit your function code in the embedded editor.
- Runtime– The Lambda runtime that executes your function.
- Handler– The method that the runtime executes when your function is invoked, such as handler. The first value is the name of the file or module. The second value is the name of the method.
- Environment variables– Key-value pairs that Lambda sets in the execution environment. Use environment variables to extend your function’s configuration outside of code.
- Tags– Key-value pairs that Lambda attaches to your function resource. Use tags to organize Lambda functions into groups for cost reporting and filtering in the Lambda console.
- Tags apply to the entire function, including all versions and aliases.
- Execution role – The IAM role that AWS Lambda assumes when it executes your function.
- Description – A description of the function.
- Memory– The amount of memory available to the function during execution. Choose an amount between 128 MB and 3,008 MB in 64-MB increments.
- Timeout – The amount of time that Lambda allows a function to run before stopping it. The default is 3 seconds. The maximum allowed value is 900 seconds.
Invoke the Lambda Function
Invoke your Lambda function using the sample event data provided in the console.
To invoke a function
- In the upper right corner, choose Test.
- In the Configure test event page, choose Create new test event and in Event template, leave the default Hello World option. Enter an Event name.
- You can change key and values in the sample JSON, but don’t change the event structure. If you do change any keys and values, you must update the sample code accordingly.
- Choose Create and then choose Test. Each user can create up to 10 test events per function. Those test events are not available to other users.
- AWS Lambda executes your function on your behalf. The handler in your Lambda function receives and then processes the sample event.
- The Execution result section shows the execution status as succeeded and also shows the function execution results, returned by the return statement.
- The Summary section shows the key information reported in the Log output section (the REPORT line in the execution log).
- The Log output section shows the log AWS Lambda generates for each execution. These are the logs written to CloudWatch by the Lambda function. The AWS Lambda console shows these logs for your convenience.Upon successful execution, view results in the console.
Using Code Editor
The code editor in the AWS Lambda console enables you to write, test, and view the execution results of your Lambda function code.
The code editor includes the menu bar, windows, and the editor pane.
To create a new file, do one of the following:
- In the Environment window, right-click the folder where you want the new file to go, and then choose New File. Type the file’s name and extension, and then press Enter .
- Choose File, New File on the menu bar. When you’re ready to save the file, choose File, Save or File, Save As on the menu bar. Then use the Save As dialog box that displays to name the file and choose where to save it.
- In the tab buttons bar in the editor pane, choose the + button, and then choose New File. When you’re ready to save the file, choose File, Save or File, Save As on the menu bar. Then use the Save As dialog box that displays to name the file and choose where to save it.
You can change various code editor settings such as which coding hints and warnings are displayed, code folding behaviors, code auto completion behaviors, and much more.
To change code editor settings, choose the Preferences gear icon in the menu bar.
To know more about AWS Lambda function, visit https://nerdyelectronics.com/cloud/aws-lambda/
Other posts by Ankita Agarwal