In the AWS Console, click on the Services menu in the top-left of the screen and select Lambda by either using the filter box or clicking on the service in the list. When you first go to the Lambda service page within the AWS Console, you will be presented with a welcome page:
Clicking on the Create a function button will take us straight to the process of launching our first serverless function.
There are four steps to creating a function; the first thing we need to do is select a blueprint:
For the basic hello world function, we are going to be using a pre-built template called hello-world-python; enter this into the filter and you should be presented with two results, one is for Python 2.7 and the second uses Python 3.6:
Selecting hello-world-python and then clicking Export will give you the option of downloading the code used in the function in the lambda_function.py file and the template which is used by Lambda during step 3. This can be found in the template.yaml file.
The code itself, as you would imagine, is pretty basic. It does nothing other than return a value it is passed. If you are not following along, the contents of the lambda_function.py file are:
from __future__ import print_function
import json
print('Loading function')
def lambda_handler(event, context):
#print("Received event: " + json.dumps(event, indent=2))
print("value1 = " + event['key1'])
print("value2 = " + event['key2'])
print("value3 = " + event['key3'])
return event['key1'] # Echo back the first key value
#raise Exception('Something went wrong')
The template.yaml file contains the following:
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: A starter AWS Lambda function.
Resources:
helloworldpython:
Type: 'AWS::Serverless::Function'
Properties:
Handler: lambda_function.lambda_handler
Runtime: python2.7
CodeUri: .
Description: A starter AWS Lambda function.
MemorySize: 128
Timeout: 3
Role: !<tag:yaml.org,2002:js/undefined> ''
As you can see, the template file configures both the Runtime, which in our case is python2.7, and some sensible settings for the MemorySize and Timeout values.
To continue to step 2, click on the function name, which for us is hello-world-python, and you will be taken to the page where we can choose how the function is triggered:
We are not going to be using a trigger just yet and we will look at these in a little more detail in the next function we launch; so for now, click on Next.
Step 3 is where we configure our function. There is quite a bit of information to enter here, but luckily a lot of the detail we need to enter has been pre-populated from the template we looked at earlier, as you can see from the following screenshot:
The details we need to enter are as follows: anything with a * is required and the information in italics was pre-populated and can be left as-is.
The following list shows all of the form fields and what should be entered:
- Basic information:
- Name*: myFirstFunction
- Description: A starter AWS Lambda function
- Runtime: Python 2.7
- Lambda function code:
- Code entry type: This contains the code for the function, there is no need to edit this
- Enable encryption helpers: Leave unticked
- Environment variables: Leave empty
- Lambda function handler and role:
- Handler*: lambda_function.lambda_handler
- Role*: Leave Create new role from template(s) selected
- Role name*: myFirstFunctionRole
- Policy templates: We do not need a policy template for this function, leave blank
Leave the Tags and Advanced settings at the default values. Once the preceding information has been entered, click on the Next button to take us to step 4, which is the final step before our function is created.
Review the details on the page. If you are happy that everything has been entered correctly, click on the Create function button at the bottom of the page; if you need to change any information, click on the Previous button.
After a few seconds, you will receive a message confirming that your function has been created:
In the preceding screenshot, there is a Test button. Clicking this will allow you to invoke your function. Here you will be able to customize the values posted to the function. As you can see from the following screenshot, I have changed the values for key1 and key2:
Once you have edited the input, clicking on Save and test will store your updated input and then invoke the function:
Clicking on Details in the Execution result message will show you both the results of the function being invoked and also the resources used:
START RequestId: 36b2103a-90bc-11e7-a32a-171ef5562e33 Version: $LATEST
value1 = hello world
value2 = this is my first serverless function
value3 = value3
END RequestId: 36b2103a-90bc-11e7-a32a-171ef5562e33
The report for the request with the 36b2103a-90bc-11e7-a32a-171ef5562e33 ID looks like this:
- Duration: 0.26 ms
- Billed Duration: 100 ms
- Memory Size: 128 MB
- Max Memory Used: 19 MB
As you can see, it took 0.26 ms for the function to run and we were charged the minimum duration of 100 ms for this. The function could consume up to 128 MB of RAM, but we only used 19 MB during the execution.
Returning to the command line, running the following command again shows that our function is now listed:
$ aws lambda list-functions
The output of the preceding command is as follows:
We can also invoke our function from the command line by running the following command:
$ aws lambda invoke \
--invocation-type RequestResponse \
--function-name myFirstFunction \
--log-type Tail \
--payload '{"key1":"hello", "key2":"world", "key3":"again"}' \
outputfile.txt
As you can see from the preceding command, the aws lambda invoke command requires several flags:
- --invocation-type: There are three types of invocation:
- RequestResponse: This is the default option; it sends the request, which in our case is defined in the --payload section of the command. Once the request has been made, the client waits for a response.
- Event: This sends the request and triggers an event. The client does not wait for a response and instead you receive an event ID back.
- DryRun: This calls the function, but never actually executes it—this is useful when testing that the details used to invoke the function actually have the correct permissions.
- --function-name: This is the name of the function we want to invoke.
- --log-type: There is currently a single option here, Tail. This returns the result of the --payload, which is the data we want to send the function; typically this will be JSON.
- outputfile.txt: The final part of the command defines where we want to store the output of the command; in our case it is a file called outputfile.txt which is being stored in the current working directory.
When invoking the command from the command line, you should get something like the following result:
Returning to the AWS Console and remaining on the myFirstFunction page, click on Monitoring and you will be presented with some basic statistics about your function:
As you can see from the preceding graphs, there are details on how many times your function has been invoked, how long it takes, and also if there are any errors.
Clicking on View logs in CloudWatch will open a new tab which lists the log streams for myFirstFunction. Clicking on the name of the log stream will then take you to a page which gives you the results for each time the function has been invoked both as testing in the AWS Console and also from the command-line client:
Both the Monitoring page and logs are extremely useful when it comes to debugging your Lambda functions.