After we announced support for HTTP APIs in the Serverless Framework we saw a lot of enthusiasm around the benefits of the new HTTP APIs. People were excited about the possibility for significant cost reduction and performance improvement. But, there was still the question of effectively troubleshooting your Lambda infrastructure in combination with the new HTTP API.
Because of this, we’re excited to announce newly released monitoring and debugging support for HTTP APIs. Now you can get automatically instrumented monitoring and debugging tools on top of your HTTP APIs right out of the box. Let’s see how with a simple service.
First, make sure you’ve already done a few things:
npm install -g serverless
After this, you should be able to create an HTTP API pretty easily.
First, let’s create a new project directory and create a serverless.yml
file in it:
mkdir http-api-project
cd http-api-project
touch serverless.yml
Then, add this to your serverless.yml
file:
1
org: yourorg
2
app: http-api-example
3
service: http-api-example-python
4
5
provider:
6
name: aws
7
runtime: python3.8
8
9
functions:
10
getProfileInfo:
11
handler: handler.hello
12
events:
13
- httpApi:
14
method: GET
15
path: /hello
Make sure to replace the org
and app
values with the ones for your Framework Pro account. From there, you can create a new handler.py
file:
touch handler.py
And then add this Python code inside the file:
1
import json
2
3
4
def hello(event, context):
5
body = event['body']
6
print(body)
7
response = {
8
"statusCode": 200,
9
"body": json.dumps({
10
"message": "Hello friends!"
11
})
12
}
13
return response
After you make sure to save both serverless.yml
and handler.py
you can run serverless deploy
to deploy your HTTP API.
From here, just open up the URL in a browser and refresh the page a few times. The URL should look something like this: https://44whsmxq8l.execute-api.us-east-1.amazonaws.com/hello
When you load it up in the browser you should see this:
After you refresh the page a few times, open up your Framework Pro Dashboard and navigate to your app and service. You should now see the recent logs:
And that’s it! You’ve just setup your HTTP API with monitoring and alerting capabilities.
#api #code #serverless #http