Skip to content

Commit

Permalink
i183: move serverless example to hands-on (#199)
Browse files Browse the repository at this point in the history
  • Loading branch information
tungbq authored Jul 30, 2023
2 parents 0703991 + c6991d3 commit f4a6ef2
Show file tree
Hide file tree
Showing 5 changed files with 205 additions and 205 deletions.
File renamed without changes.
12 changes: 6 additions & 6 deletions serverless/README.md → hands-on/serverless/README.md
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 serverless/lambda_http.py → hands-on/serverless/lambda_http.py
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))
Loading

0 comments on commit f4a6ef2

Please sign in to comment.