Hello, world! in the console

In this section, we're going to create a Lambda function in Python using the AWS Management Console. Since Python code doesn't need to be built, we'll create a function that needs no dependencies so that we can use the inline editor.

Fire up your AWS account and head over to the Lambda console. If you haven't created any functions, you'll be greeted by the home screen. Have a read if you like, and when you're ready, click the Create function button.

We're going to create our function from scratch, but it's worth noting the other options there. Chances are that there will be a blueprint for what you're trying to do, so it pays to remember to check the list of samples before creating functions:

Creating a new Lambda function using the console

Perform the following steps to create your Lambda function:

  1. Enter an appropriate name for your function and select a Python runtime.
  2. For the permissions section, we don't need our Lambda to be able to do very much, so we'll let the service create an IAM role for us with a basic policy document.
Note the execution role name in case you have to update the policy later. Hit Create function when you're done.
  1. Next, you should be presented with the Lambda function console, including all the options for connecting event sources, the incline editor, environment variables, and then a few options for how the function executes. Use the editor to add the code, as follows: 

Overview of a function configuration screen in the Lambda console
  1. Let's boost the function to 512 MB of memory with a 10-second timeout (1).
  2. We should rename the default file (2) and update the pointer to the handler (3). 
  1. We don't need to configure the event source since we'll be invoking the function ourselves, with our own event:

More configuration options on the Function Configuration page
  1. Next, we'll set up our test event so that we can pass in the values that the function is expecting. We're passing in a JSON event object with a text string to print and an integer with the number of times we want it to be printed:

Creating a new test event object
  1. Finally, let's invoke our function. Hit the Test button and have a look at the output:

A snippet of the Lambda console showing the results of an executed Lambda function

At the top of the preceding screenshot, we can see the response data that we were expecting. We also get some information about how long the function took to execute, as well as the resources it consumed. This information can help you fine-tune your function configuration. At the bottom, we can see our log output, which is an extract from the log data stored in CloudWatch Logs.

That's it! You've successfully created a Lambda function from scratch and invoked it with a test event!

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

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