Skip to content

Commit e1fcea6

Browse files
bedgebrettstack
authored andcommitted
docs(example): improve microservice-http-endpoint-python3 example (#415)
* Remove unnecessary index.js" * Moved common config to template Globals and uncomment debug log. * Add sample payload and commands for testing. * Rename test script as a script. * Exit from script after command. * Simplify use case for easier startup. - Move table creation into template. - Eliminates need to provide table name in every REST call. - Fix GET with no params. - Split test.sh script from output log. * Address requested changes.
1 parent c7549e7 commit e1fcea6

File tree

6 files changed

+127
-179
lines changed

6 files changed

+127
-179
lines changed

examples/apps/microservice-http-endpoint-python3/index.js

Lines changed: 0 additions & 167 deletions
This file was deleted.

examples/apps/microservice-http-endpoint-python3/lambda_function.py

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11

22
import boto3
33
import json
4+
import os
45

56
print('Loading function')
67
dynamo = boto3.client('dynamodb')
8+
table_name = os.environ['TABLE_NAME']
79

810

911
def respond(err, res=None):
@@ -21,18 +23,19 @@ def lambda_handler(event, context):
2123
access to the request and response payload, including headers and
2224
status code.
2325
24-
To scan a DynamoDB table, make a GET request with the TableName as a
25-
query string parameter. To put, update, or delete an item, make a POST,
26-
PUT, or DELETE request respectively, passing in the payload to the
27-
DynamoDB API as a JSON body.
26+
TableName provided by template.yaml.
27+
28+
To scan a DynamoDB table, make a GET request with optional query string parameter.
29+
To put, update, or delete an item, make a POST, PUT, or DELETE request respectively,
30+
passing in the payload to the DynamoDB API as a JSON body.
2831
'''
29-
#print("Received event: " + json.dumps(event, indent=2))
32+
print("Received event: " + json.dumps(event, indent=2))
3033

3134
operations = {
32-
'DELETE': lambda dynamo, x: dynamo.delete_item(**x),
33-
'GET': lambda dynamo, x: dynamo.scan(**x),
34-
'POST': lambda dynamo, x: dynamo.put_item(**x),
35-
'PUT': lambda dynamo, x: dynamo.update_item(**x),
35+
'DELETE': lambda dynamo, x: dynamo.delete_item(TableName=table_name, **x),
36+
'GET': lambda dynamo, x: dynamo.scan(TableName=table_name, **x) if x else dynamo.scan(TableName=table_name),
37+
'POST': lambda dynamo, x: dynamo.put_item(TableName=table_name, **x),
38+
'PUT': lambda dynamo, x: dynamo.update_item(TableName=table_name, **x),
3639
}
3740

3841
operation = event['httpMethod']

examples/apps/microservice-http-endpoint-python3/template.yaml

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@ Transform: 'AWS::Serverless-2016-10-31'
33
Description: >-
44
A simple backend (read/write to DynamoDB) with a RESTful API endpoint using
55
Amazon API Gateway.
6+
Globals:
7+
#https://github.com/awslabs/serverless-application-model/blob/develop/docs/globals.rst
8+
Function:
9+
Runtime: python3.6
10+
MemorySize: 512
11+
#VpcConfig:
12+
#Tracing:
13+
#AutoPublishAlias:
14+
Environment:
15+
Variables:
16+
TABLE_NAME:
17+
Ref: Table
18+
19+
20+
621
Resources:
722
microservicehttpendpointpython3:
823
Type: 'AWS::Serverless::Function'
924
Properties:
1025
Handler: lambda_function.lambda_handler
11-
Runtime: python3.6
1226
CodeUri: .
1327
Description: >-
1428
A simple backend (read/write to DynamoDB) with a RESTful API endpoint
1529
using Amazon API Gateway.
16-
MemorySize: 512
1730
Timeout: 10
1831
Policies:
1932
- Version: '2012-10-17'
@@ -32,10 +45,15 @@ Resources:
3245
- Ref: 'AWS::Region'
3346
- ':'
3447
- Ref: 'AWS::AccountId'
35-
- ':table/*'
48+
- ':table/'
49+
- Ref: Table
3650
Events:
3751
Api1:
3852
Type: Api
3953
Properties:
4054
Path: /MyResource
4155
Method: ANY
56+
57+
Table:
58+
Type: AWS::Serverless::SimpleTable
59+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
❯ ./test.sh https://vxldx8ck9i.execute-api.us-west-2.amazonaws.com/Prod/MyResource ./test-payload.json
2+
+ http POST https://vxldx8ck9i.execute-api.us-west-2.amazonaws.com/Prod/MyResource Item:=@./test-payload.json
3+
HTTP/1.1 200 OK
4+
Connection: keep-alive
5+
Content-Length: 414
6+
Content-Type: application/json
7+
Date: Thu, 03 May 2018 21:13:56 GMT
8+
Via: 1.1 d159f3d447f69796b1a04dffe8d243c0.cloudfront.net (CloudFront)
9+
X-Amz-Cf-Id: 60kAxGheadAiJo-ksjknQr-rAh-WZ2EaJXMT_cYH5nzgwIudXHUu9g==
10+
X-Amzn-Trace-Id: Root=1-5aeb7b94-457eb371f443bfc06c5dadb3
11+
X-Cache: Miss from cloudfront
12+
x-amz-apigw-id: GVA_OEZIPHcFleg=
13+
x-amzn-RequestId: e3e943ab-4f16-11e8-af74-e7a92312c1b8
14+
15+
{
16+
"ResponseMetadata": {
17+
"HTTPHeaders": {
18+
"connection": "keep-alive",
19+
"content-length": "2",
20+
"content-type": "application/x-amz-json-1.0",
21+
"date": "Thu, 03 May 2018 21:13:56 GMT",
22+
"server": "Server",
23+
"x-amz-crc32": "2745614147",
24+
"x-amzn-requestid": "VQGS8JQIH2G9CDO8P65U6HMKEFVV4KQNSO5AEMVJF66Q9ASUAAJG"
25+
},
26+
"HTTPStatusCode": 200,
27+
"RequestId": "VQGS8JQIH2G9CDO8P65U6HMKEFVV4KQNSO5AEMVJF66Q9ASUAAJG",
28+
"RetryAttempts": 0
29+
}
30+
}
31+
32+
+ http GET https://vxldx8ck9i.execute-api.us-west-2.amazonaws.com/Prod/MyResource
33+
HTTP/1.1 200 OK
34+
Connection: keep-alive
35+
Content-Length: 485
36+
Content-Type: application/json
37+
Date: Thu, 03 May 2018 21:13:57 GMT
38+
Via: 1.1 0419e0c9f6ea9a42ab82db8feb536aee.cloudfront.net (CloudFront)
39+
X-Amz-Cf-Id: Nsflnq2N-dxYOskE8ui1ULydLO9fg7-HaCmbtjMwZ1A7Pr_wgEJUhA==
40+
X-Amzn-Trace-Id: Root=1-5aeb7b95-f0ceaed903bf48b8605b6899
41+
X-Cache: Miss from cloudfront
42+
x-amz-apigw-id: GVA_UHO5PHcFo7w=
43+
x-amzn-RequestId: e444d07b-4f16-11e8-8db6-6940d66a847c
44+
45+
{
46+
"Count": 1,
47+
"Items": [
48+
{
49+
"id": {
50+
"S": "id_string"
51+
}
52+
}
53+
],
54+
"ResponseMetadata": {
55+
"HTTPHeaders": {
56+
"connection": "keep-alive",
57+
"content-length": "63",
58+
"content-type": "application/x-amz-json-1.0",
59+
"date": "Thu, 03 May 2018 21:13:57 GMT",
60+
"server": "Server",
61+
"x-amz-crc32": "3041197985",
62+
"x-amzn-requestid": "9NFOFDHCCR5OE0V81IUC70Q6FNVV4KQNSO5AEMVJF66Q9ASUAAJG"
63+
},
64+
"HTTPStatusCode": 200,
65+
"RequestId": "9NFOFDHCCR5OE0V81IUC70Q6FNVV4KQNSO5AEMVJF66Q9ASUAAJG",
66+
"RetryAttempts": 0
67+
},
68+
"ScannedCount": 1
69+
}
70+
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"id": {
3+
"S": "id_string"
4+
}
5+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#/bin/bash -e
2+
3+
# Test invocation using httpie (http) in place of curl for syntax simplicity
4+
#
5+
# Args: $1: API GW path
6+
# $2 json payload data file
7+
#
8+
# eg: ./test.sh https://<API_ID>.execute-api.<YOUR_REGION>.amazonaws.com/Prod/MyResource ./test-payload.json
9+
10+
set -x
11+
12+
# Write one item
13+
14+
http POST $1 Item:=@$2
15+
16+
# Read all back
17+
18+
http GET $1
19+

0 commit comments

Comments
 (0)