Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(example): improve microservice-http-endpoint-python3 example #415

Merged
merged 7 commits into from
May 25, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
167 changes: 0 additions & 167 deletions examples/apps/microservice-http-endpoint-python3/index.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@

import boto3
import json
import os

print('Loading function')
dynamo = boto3.client('dynamodb')
table_name = os.environ['TABLE_NAME']


def respond(err, res=None):
Expand All @@ -21,18 +23,19 @@ def lambda_handler(event, context):
access to the request and response payload, including headers and
status code.

To scan a DynamoDB table, make a GET request with the TableName as a
query string parameter. To put, update, or delete an item, make a POST,
PUT, or DELETE request respectively, passing in the payload to the
DynamoDB API as a JSON body.
TableName provided by template.yaml.

To scan a DynamoDB table, make a GET request with optional query string parameter.
To put, update, or delete an item, make a POST, PUT, or DELETE request respectively,
passing in the payload to the DynamoDB API as a JSON body.
'''
#print("Received event: " + json.dumps(event, indent=2))
print("Received event: " + json.dumps(event, indent=2))

operations = {
'DELETE': lambda dynamo, x: dynamo.delete_item(**x),
'GET': lambda dynamo, x: dynamo.scan(**x),
'POST': lambda dynamo, x: dynamo.put_item(**x),
'PUT': lambda dynamo, x: dynamo.update_item(**x),
'DELETE': lambda dynamo, x: dynamo.delete_item(TableName=table_name, **x),
'GET': lambda dynamo, x: dynamo.scan(TableName=table_name, **x) if x else dynamo.scan(TableName=table_name),
'POST': lambda dynamo, x: dynamo.put_item(TableName=table_name, **x),
'PUT': lambda dynamo, x: dynamo.update_item(TableName=table_name, **x),
}

operation = event['httpMethod']
Expand Down
24 changes: 21 additions & 3 deletions examples/apps/microservice-http-endpoint-python3/template.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,30 @@ Transform: 'AWS::Serverless-2016-10-31'
Description: >-
A simple backend (read/write to DynamoDB) with a RESTful API endpoint using
Amazon API Gateway.
Globals:
#https://github.com/awslabs/serverless-application-model/blob/develop/docs/globals.rst
Function:
Runtime: python3.6
MemorySize: 512
#VpcConfig:
#Tracing:
#AutoPublishAlias:
Environment:
Variables:
TABLE_NAME:
Ref: Table



Resources:
microservicehttpendpointpython3:
Type: 'AWS::Serverless::Function'
Properties:
Handler: lambda_function.lambda_handler
Runtime: python3.6
CodeUri: .
Description: >-
A simple backend (read/write to DynamoDB) with a RESTful API endpoint
using Amazon API Gateway.
MemorySize: 512
Timeout: 10
Policies:
- Version: '2012-10-17'
Expand All @@ -32,10 +45,15 @@ Resources:
- Ref: 'AWS::Region'
- ':'
- Ref: 'AWS::AccountId'
- ':table/*'
- ':table/'
- Ref: Table
Events:
Api1:
Type: Api
Properties:
Path: /MyResource
Method: ANY

Table:
Type: AWS::Serverless::SimpleTable

Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
❯ ./test.sh https://vxldx8ck9i.execute-api.us-west-2.amazonaws.com/Prod/MyResource ./test-payload.json
+ http POST https://vxldx8ck9i.execute-api.us-west-2.amazonaws.com/Prod/MyResource Item:=@./test-payload.json
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 414
Content-Type: application/json
Date: Thu, 03 May 2018 21:13:56 GMT
Via: 1.1 d159f3d447f69796b1a04dffe8d243c0.cloudfront.net (CloudFront)
X-Amz-Cf-Id: 60kAxGheadAiJo-ksjknQr-rAh-WZ2EaJXMT_cYH5nzgwIudXHUu9g==
X-Amzn-Trace-Id: Root=1-5aeb7b94-457eb371f443bfc06c5dadb3
X-Cache: Miss from cloudfront
x-amz-apigw-id: GVA_OEZIPHcFleg=
x-amzn-RequestId: e3e943ab-4f16-11e8-af74-e7a92312c1b8

{
"ResponseMetadata": {
"HTTPHeaders": {
"connection": "keep-alive",
"content-length": "2",
"content-type": "application/x-amz-json-1.0",
"date": "Thu, 03 May 2018 21:13:56 GMT",
"server": "Server",
"x-amz-crc32": "2745614147",
"x-amzn-requestid": "VQGS8JQIH2G9CDO8P65U6HMKEFVV4KQNSO5AEMVJF66Q9ASUAAJG"
},
"HTTPStatusCode": 200,
"RequestId": "VQGS8JQIH2G9CDO8P65U6HMKEFVV4KQNSO5AEMVJF66Q9ASUAAJG",
"RetryAttempts": 0
}
}

+ http GET https://vxldx8ck9i.execute-api.us-west-2.amazonaws.com/Prod/MyResource
HTTP/1.1 200 OK
Connection: keep-alive
Content-Length: 485
Content-Type: application/json
Date: Thu, 03 May 2018 21:13:57 GMT
Via: 1.1 0419e0c9f6ea9a42ab82db8feb536aee.cloudfront.net (CloudFront)
X-Amz-Cf-Id: Nsflnq2N-dxYOskE8ui1ULydLO9fg7-HaCmbtjMwZ1A7Pr_wgEJUhA==
X-Amzn-Trace-Id: Root=1-5aeb7b95-f0ceaed903bf48b8605b6899
X-Cache: Miss from cloudfront
x-amz-apigw-id: GVA_UHO5PHcFo7w=
x-amzn-RequestId: e444d07b-4f16-11e8-8db6-6940d66a847c

{
"Count": 1,
"Items": [
{
"id": {
"S": "id_string"
}
}
],
"ResponseMetadata": {
"HTTPHeaders": {
"connection": "keep-alive",
"content-length": "63",
"content-type": "application/x-amz-json-1.0",
"date": "Thu, 03 May 2018 21:13:57 GMT",
"server": "Server",
"x-amz-crc32": "3041197985",
"x-amzn-requestid": "9NFOFDHCCR5OE0V81IUC70Q6FNVV4KQNSO5AEMVJF66Q9ASUAAJG"
},
"HTTPStatusCode": 200,
"RequestId": "9NFOFDHCCR5OE0V81IUC70Q6FNVV4KQNSO5AEMVJF66Q9ASUAAJG",
"RetryAttempts": 0
},
"ScannedCount": 1
}

Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"id": {
"S": "id_string"
}
}
19 changes: 19 additions & 0 deletions examples/apps/microservice-http-endpoint-python3/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#/bin/bash -e

# Test invocation using httpie (http) in place of curl for syntax simplicity
#
# Args: $1: API GW path
# $2 json payload data file
#
# eg: ./test.sh https://<API_ID>.execute-api.<YOUR_REGION>.amazonaws.com/Prod/MyResource ./test-payload.json

set -x

# Write one item

http POST $1 Item:=@$2

# Read all back

http GET $1