Experiments with DynamoDB and Connection HTTP headers
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 json | |
import os | |
import time | |
import uuid | |
import boto3 | |
def set_connection_header(request, operation_name, **kwargs): | |
# request.headers['Connection'] = 'keep-alive' | |
request.headers['Connection'] = 'close' | |
ddb = boto3.client('dynamodb') | |
ddb.meta.events.register('request-created.dynamodb', set_connection_header) | |
TABLE_NAME = os.environ['DDB_TABLE'] | |
def handler(event, context): | |
pk = uuid.uuid4().hex | |
start_ts = time.time() | |
res = ddb.put_item(TableName=TABLE_NAME, | |
Item={'pk': {'S': pk}}) | |
end_ts = time.time() | |
duration_ms = (end_ts - start_ts) * 1000 | |
print(f'PutItem: {duration_ms:.3f}') | |
# for _ in range(9): | |
# ddb.put_item(TableName=TABLE_NAME, | |
# Item={'pk': {'S': uuid.uuid4().hex}}) | |
return {'statusCode': 200, | |
'headers': {'Content-Type': 'application/json'}, | |
'body': json.dumps(res)} |
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 | |
Globals: | |
Function: | |
Handler: main.handler | |
MemorySize: 1024 | |
Runtime: python3.7 | |
Timeout: 10 | |
Resources: | |
DDBTable: | |
Type: AWS::Serverless::SimpleTable | |
Properties: | |
PrimaryKey: | |
Name: pk | |
Type: String | |
SSESpecification: | |
SSEEnabled: true | |
DDBWriter: | |
Type: AWS::Serverless::Function | |
Properties: | |
Environment: | |
Variables: | |
DDB_TABLE: !Ref DDBTable | |
CodeUri: './ddbwriter' | |
Policies: | |
- Statement: | |
Effect: Allow | |
Action: | |
- dynamodb:PutItem | |
Resource: !GetAtt DDBTable.Arn | |
Events: | |
Http: | |
Type: Api | |
Properties: | |
Path: / | |
Method: GET |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment