Last active
May 10, 2019 00:51
-
-
Save cm-fujii/57d9e9dc8a3a152fb2896014049c9cf6 to your computer and use it in GitHub Desktop.
Blog-TryDynamoDB
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
import os | |
import json | |
import boto3 | |
import decimal | |
def lambda_handler(event, context): | |
table = boto3.resource('dynamodb').Table(os.getenv('TABLE_NAME')) | |
param = event['pathParameters']['param'] | |
ret_get_item = table.get_item( | |
Key={ | |
'name': param | |
} | |
) | |
# ret_get_itemに「reference」を含む場合、エイリアスレコードのため、本体情報を取得する | |
if 'reference' in ret_get_item['Item']: | |
ret_get_item = table.get_item(Key={ | |
'name': ret_get_item['Item']['reference'] | |
}) | |
# 実体レコードのみ全て取得する | |
ret_scan = table.scan( | |
IndexName=os.getenv('GSI_NAME') | |
) | |
response = { | |
'get_item': ret_get_item['Item'], | |
'scan': ret_scan['Items'] | |
} | |
return { | |
'statusCode': 200, | |
'body': json.dumps(response, cls=DecimalEncoder), | |
} | |
# https://docs.aws.amazon.com/ja_jp/amazondynamodb/latest/developerguide/GettingStarted.Python.03.html | |
# Helper class to convert a DynamoDB item to JSON. | |
class DecimalEncoder(json.JSONEncoder): | |
def default(self, o): | |
if isinstance(o, decimal.Decimal): | |
if abs(o) % 1 > 0: | |
return float(o) | |
else: | |
return int(o) | |
return super(DecimalEncoder, self).default(o) |
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
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: AWS::Serverless-2016-10-31 | |
Description: Sample SAM Template for TryDynamoDB | |
Globals: | |
Function: | |
Timeout: 3 | |
Resources: | |
HelloWorldFunction: | |
Type: AWS::Serverless::Function | |
Properties: | |
CodeUri: hello_world/ | |
Handler: app.lambda_handler | |
Runtime: python3.6 | |
Environment: | |
Variables: | |
TABLE_NAME: !Ref ColorTable | |
GSI_NAME: name_ja_gsi | |
# https://github.com/awslabs/serverless-application-model/blob/master/docs/policy_templates.rst | |
# https://github.com/awslabs/serverless-application-model/blob/develop/samtranslator/policy_templates_data/policy_templates.json | |
Policies: | |
DynamoDBReadPolicy: | |
TableName: !Ref ColorTable | |
Events: | |
HelloWorld: | |
Type: Api | |
Properties: | |
Path: /color/{param} | |
Method: get | |
ColorTable: | |
Type: AWS::DynamoDB::Table | |
Properties: | |
TableName: TryDynamoDB-ColorTable | |
AttributeDefinitions: | |
- AttributeName: name | |
AttributeType: S | |
- AttributeName: name_ja | |
AttributeType: S | |
KeySchema: | |
- AttributeName: name | |
KeyType: HASH | |
ProvisionedThroughput: | |
ReadCapacityUnits: 1 | |
WriteCapacityUnits: 1 | |
GlobalSecondaryIndexes: | |
- IndexName: name_ja_gsi | |
KeySchema: | |
- AttributeName: name_ja | |
KeyType: HASH | |
Projection: | |
ProjectionType: ALL | |
ProvisionedThroughput: | |
ReadCapacityUnits: 1 | |
WriteCapacityUnits: 1 | |
Outputs: | |
HelloWorldApi: | |
Description: "API Gateway endpoint URL for Prod stage for Hello World function" | |
Value: !Sub "https://${ServerlessRestApi}.execute-api.${AWS::Region}.amazonaws.com/Prod/color/{param}" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment