Creating a Lambda function

We can easily create a new Lambda function from the management console by navigating to the AWS Lambda section and execute the following steps:

  1. Click on Create function to begin the process:

  1. The Create function dialog will give us the option to select the following:
    • Author from scratch: Write our own function that will be completely custom
    • Blueprints: Use a template that AWS has provided for us to help with authoring the function
    • AWS Serverless Application Repository: A repository for predefined Lambda functions
  2. We will select the Author from scratch option, to write our own function in this scenario:

  1. In the Author from scratch section, we need to name our function, select a runtime, and create or select an existing role if we have one, so that the Lambda function will have the necessary permissions to execute the code:

  1. After the function is created, a window with the function configuration details appears. Here, we have the ability to use the Designer tool. This is an invaluable resource for building Lambda functions, as it can help us complete common tasks by giving us the ability to quickly integrate our Lambda function with triggers that will invoke a function and downstream resources that will receive its output:

  1. Further down, we can scroll to the Function code section. Here, we can author our code straight from the browser and save it into our function package. In our example, we will be using a simple Node.js function that replies with Hello, World! when invoked:
exports.handler = function(event, context) {
context.succeed('Hello, World!');
};
  1. After changing the function, we will need to hit the Save button in the top-right to save the function configuration. We can also use the IDE provided in Lambda to easily create new files and add other components needed to run our code:

  1. Further down, we can set Environment variables that can be used directly by our function code. We can also add additional key-value tags that will only be seen from the AWS management environment:

  1. Going further, we get to the Execution role section, where we can modify the execution role to change the permissions for our function code. We can also set the size of our function in the Basic settings section. Here, we can set the memory we want to use for our function. The amount of memory defines the amount of CPU to be added to the function; these scale together, as does the price of each function execution. We also have the ability to set the timeout. Initially, the timeout was limited to 300 seconds, but it has been extended to 900 seconds since November, 2018.
Note that the Lambda change to a 15-minute timeout might not yet be reflected in the exam, and the old five-minute value may be the correct answer!

Please refer to the following screenshot:

  1. At the bottom of the page, we can configure the network. We can either run the Lambda with the default (recommended) network environment, or we can tie the Lambda into a VPC. We would usually only do so if some kind of compliance or governance reason existed.
  2. We can also configure the Dead Letter Queue in the Debugging and error handling section. Here, we can specify how to handle executions that fail. We can specify how many retries we want to have and, if the function fails on all retries, to forward the diagnostic output to a Dead Letter Queue.
  3. By default, each region in each account has the ability to execute 1000 concurrent Lambda executions. In the Concurrency section, we have the ability to either use the default concurrency or reserve a certain amount of concurrent executions for our function. We would preferably do that for mission-critical Lambda functions. This is a soft limit, and if you require the ability to execute more than 1,000 concurrent Lambda functions in a region, you can contact AWS support to raise your account limit.
  1. Lastly, in the Auditing and compliance section, we have the ability to tie in the Lambda function with CloudTrail, which can record each and every API call to the Lambda function, thereby giving us a complete auditing trail of all actions and invocations of the Lambda function:

  1. Now that we have reached the end of the configuration section, we can scroll up and click on the monitoring section. Here, we have a quick overview of the execution statistics and performance of our Lambda function. For detailed information, we would still be using the CloudWatch interface, which we will look at in the next chapter:

Our function is now ready for invocation, and now we can configure the appropriate triggers that will invoke the service and execute the function.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.190.217.253