-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
i183: move serverless example to hands-on (#199)
- Loading branch information
Showing
5 changed files
with
205 additions
and
205 deletions.
There are no files selected for viewing
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
# Architecture: | ||
[User] -> [API_Gateway] -> [Lambda] -> [DynamoDB] | ||
|
||
# References: | ||
## https://developer.hashicorp.com/terraform/tutorials/aws/lambda-api-gateway | ||
## https://dev.to/rolfstreefkerk/openapi-with-terraform-on-aws-api-gateway-17je | ||
# Architecture: | ||
[User] -> [API_Gateway] -> [Lambda] -> [DynamoDB] | ||
|
||
# References: | ||
## https://developer.hashicorp.com/terraform/tutorials/aws/lambda-api-gateway | ||
## https://dev.to/rolfstreefkerk/openapi-with-terraform-on-aws-api-gateway-17je |
70 changes: 35 additions & 35 deletions
70
serverless/lambda_http.py → hands-on/serverless/lambda_http.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,36 +1,36 @@ | ||
from __future__ import print_function | ||
|
||
import boto3 | ||
import json | ||
|
||
print('Loading function') | ||
|
||
|
||
def lambda_handler(event, context): | ||
'''Provide an event that contains the following keys: | ||
- operation: one of the operations in the operations dict below | ||
- tableName: required for operations that interact with DynamoDB | ||
- payload: a parameter to pass to the operation being performed | ||
''' | ||
#print("Received event: " + json.dumps(event, indent=2)) | ||
|
||
operation = event['operation'] | ||
|
||
if 'tableName' in event: | ||
dynamo = boto3.resource('dynamodb').Table(event['tableName']) | ||
|
||
operations = { | ||
'create': lambda x: dynamo.put_item(**x), | ||
'read': lambda x: dynamo.get_item(**x), | ||
'update': lambda x: dynamo.update_item(**x), | ||
'delete': lambda x: dynamo.delete_item(**x), | ||
'list': lambda x: dynamo.scan(**x), | ||
'echo': lambda x: x, | ||
'ping': lambda x: 'pong' | ||
} | ||
|
||
if operation in operations: | ||
return operations[operation](event.get('payload')) | ||
else: | ||
from __future__ import print_function | ||
|
||
import boto3 | ||
import json | ||
|
||
print('Loading function') | ||
|
||
|
||
def lambda_handler(event, context): | ||
'''Provide an event that contains the following keys: | ||
- operation: one of the operations in the operations dict below | ||
- tableName: required for operations that interact with DynamoDB | ||
- payload: a parameter to pass to the operation being performed | ||
''' | ||
#print("Received event: " + json.dumps(event, indent=2)) | ||
|
||
operation = event['operation'] | ||
|
||
if 'tableName' in event: | ||
dynamo = boto3.resource('dynamodb').Table(event['tableName']) | ||
|
||
operations = { | ||
'create': lambda x: dynamo.put_item(**x), | ||
'read': lambda x: dynamo.get_item(**x), | ||
'update': lambda x: dynamo.update_item(**x), | ||
'delete': lambda x: dynamo.delete_item(**x), | ||
'list': lambda x: dynamo.scan(**x), | ||
'echo': lambda x: x, | ||
'ping': lambda x: 'pong' | ||
} | ||
|
||
if operation in operations: | ||
return operations[operation](event.get('payload')) | ||
else: | ||
raise ValueError('Unrecognized operation "{}"'.format(operation)) |
Oops, something went wrong.