This post covers the basics of using AWS Lambda functions.
For the most accurate data, see the official pricing documentation.
Lambda charges you for each request invocation, and also for how long the request took to run. These prices are probably in USD.
HelloWorldTest1
Sample Data:
{
"body": "{\"name\": \"Test User\"}"
}
Click on the orange lambda icon with the text "HelloWorld" to open the "Function code" form. Expand it to full screen (button on top right) so the Save and Test buttons appear along the top.
Write your Hello World function. This was the shortest one I could come up with that wouldn't throw 500's if you called it with the wrong data.
import json
def lambda_handler(event, context):
name = 'UNDEFINED'
if 'body' in event:
try:
ebody = json.loads(event['body'])
name = ebody['name']
except:
pass
greeting = f'Hello {name}!'
body = json.dumps(greeting)
return {
'statusCode': 200,
'body': body
}
Click Save, then Test. You should get a response with body 'Hello TestUser'
.
Now if you click on the purple API Gateway button it will open the API Gateway form. In there, you can get the URL. Copy the link address.
From bash:
curl \
-X POST \
-H 'Content-Type: application/json' \
-d '{"name": "MyName"}' \
https://wg8p74asah.execute-api.us-east-1.amazonaws.com/default/HelloWorld
It should reply "Hello MyName".
Prominently displayed beneath the function's name in the Designer form is a button to manage Layers.
Here's their official documentation.
Layers are dependencies like archives and libraries.
Any one function can use up to 5 of them, and they have a 250MB limit.
They land in /opt
when extracted.
We don't need layers for this Hello World app.