Skip to content

Instantly share code, notes, and snippets.

@milancermak
Created March 7, 2019 20:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save milancermak/67a6cd12fcd08ca0f393ebb658f3eb4d to your computer and use it in GitHub Desktop.
Save milancermak/67a6cd12fcd08ca0f393ebb658f3eb4d to your computer and use it in GitHub Desktop.
Experiments with DynamoDB and Connection HTTP headers
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)}
---
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