Skip to content

Commit cba3dfa

Browse files
committed
feat: Refresh aws-python-rest-api template
1 parent dadf593 commit cba3dfa

File tree

3 files changed

+99
-11
lines changed

3 files changed

+99
-11
lines changed

Diff for: aws-python-rest-api/README.md

+88-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
title: 'AWS Simple HTTP Endpoint example in Python'
33
description: 'This template demonstrates how to make a simple REST API with Python running on AWS Lambda and API Gateway using the traditional Serverless Framework.'
44
layout: Doc
5-
framework: v1
5+
framework: v2
66
platform: AWS
77
language: python
88
authorLink: 'https://github.com/serverless'
@@ -26,7 +26,7 @@ Run this command to initialize a new project in a new working directory.
2626

2727
## Usage
2828

29-
**Deploy**
29+
### Deployment
3030

3131
This example is made to work with the Serverless Framework dashboard which includes advanced features like CI/CD, monitoring, metrics, etc.
3232

@@ -37,16 +37,99 @@ $ serverless deploy
3737

3838
To deploy without the dashboard you will need to remove `org` and `app` fields from the `serverless.yml`, and you won’t have to run `sls login` before deploying.
3939

40-
**Invoke the function locally.**
40+
After running deploy, you should see output similar to:
41+
42+
```bash
43+
Serverless: Packaging service...
44+
Serverless: Excluding development dependencies...
45+
Serverless: Creating Stack...
46+
Serverless: Checking Stack create progress...
47+
........
48+
Serverless: Stack create finished...
49+
Serverless: Uploading CloudFormation file to S3...
50+
Serverless: Uploading artifacts...
51+
Serverless: Uploading service aws-python-rest-api.zip file to S3 (711.23 KB)...
52+
Serverless: Validating template...
53+
Serverless: Updating Stack...
54+
Serverless: Checking Stack update progress...
55+
.................................
56+
Serverless: Stack update finished...
57+
Service Information
58+
service: aws-python-rest-api
59+
stage: dev
60+
region: us-east-1
61+
stack: aws-python-rest-api-dev
62+
resources: 12
63+
api keys:
64+
None
65+
endpoints:
66+
ANY - https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
67+
functions:
68+
api: aws-python-rest-api-dev-hello
69+
layers:
70+
None
71+
```
72+
73+
_Note_: In current form, after deployment, your API is public and can be invoked by anyone. For production deployments, you might want to configure an authorizer. For details on how to do that, refer to [http event docs](https://www.serverless.com/framework/docs/providers/aws/events/apigateway/).
74+
75+
### Invocation
4176

77+
After successful deployment, you can call the created application via HTTP:
78+
79+
```bash
80+
curl https://xxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
4281
```
82+
83+
Which should result in response similar to the following (removed `input` content for brevity):
84+
85+
```json
86+
{
87+
"message": "Go Serverless v2.0! Your function executed successfully!",
88+
"input": {
89+
...
90+
}
91+
}
92+
```
93+
94+
### Local development
95+
96+
You can invoke your function locally by using the following command:
97+
98+
```bash
4399
serverless invoke local --function hello
44100
```
45101

46-
**Invoke the function**
102+
Which should result in response similar to the following:
103+
104+
```
105+
{
106+
"statusCode": 200,
107+
"body": "{\n \"message\": \"Go Serverless v2.0! Your function executed successfully!\",\n \"input\": \"\"\n}"
108+
}
109+
```
110+
111+
Alternatively, it is also possible to emulate API Gateway and Lambda locally by using `serverless-offline` plugin. In order to do that, execute the following command:
47112

113+
```bash
114+
serverless plugin install -n serverless-offline
48115
```
49-
curl https://xxxxxxxxx.execute-api.us-east-1.amazonaws.com/dev/
116+
117+
It will add the `serverless-offline` plugin to `devDependencies` in `package.json` file as well as will add it to `plugins` in `serverless.yml`.
118+
119+
After installation, you can start local emulation with:
120+
50121
```
122+
serverless offline
123+
```
124+
125+
To learn more about the capabilities of `serverless-offline`, please refer to its [GitHub repository](https://github.com/dherault/serverless-offline).
126+
127+
### Bundling dependencies
51128

129+
In case you would like to include 3rd party dependencies, you will need to use a plugin called `serverless-python-requirements`. You can set it up by running the following command:
130+
131+
```bash
132+
serverless plugin install -n serverless-python-requirements
133+
```
52134

135+
Running the above will automatically add `serverless-python-requirements` to `plugins` section in your `serverless.yml` file and add it as a `devDependency` to `package.json` file. The `package.json` file will be automatically created if it doesn't exist beforehand. Now you will be able to add your dependencies to `requirements.txt` file (`Pipfile` and `pyproject.toml` is also supported but requires additional configuration) and they will be automatically injected to Lambda package during build process. For more details about the plugin's configuration, please refer to [official documentation](https://github.com/UnitedIncome/serverless-python-requirements).

Diff for: aws-python-rest-api/handler.py

+3-6
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,11 @@
33

44
def hello(event, context):
55
body = {
6-
"message": "Go Serverless v1.0! Your function executed successfully!",
7-
"input": event
6+
"message": "Go Serverless v2.0! Your function executed successfully!",
7+
"input": event,
88
}
99

10-
response = {
11-
"statusCode": 200,
12-
"body": json.dumps(body)
13-
}
10+
response = {"statusCode": 200, "body": json.dumps(body)}
1411

1512
return response
1613

Diff for: aws-python-rest-api/serverless.yml

+8
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,17 @@ org: serverlessinc
22
app: aws-python-rest-api
33
service: aws-python-rest-api
44

5+
frameworkVersion: '2'
6+
variablesResolutionMode: '20210219'
7+
configValidationMode: 'error'
8+
9+
510
provider:
611
name: aws
712
runtime: python3.8
13+
lambdaHashingVersion: '20201221'
14+
apiGateway:
15+
shouldStartNameWithService: true
816

917
functions:
1018
hello:

0 commit comments

Comments
 (0)