ised-isde-canada / cbmdsp-cp-sp-overall-architecture

1 stars 1 forks source link

Task: JAX-RS 2 https client for DynamoDB or API-Gateway calls from in-container controller #22

Open obriensystems opened 3 years ago

obriensystems commented 3 years ago

Test lambda to dynamodb access first (s3 and dynamoDb managed roles added to execution role aws dynamodb --region us-west-2 create-table --table-name eventstream --attribute-definitions AttributeName=timestamp,AttributeType=S AttributeName=label,AttributeType=S --key-schema AttributeName=timestamp,KeyType=HASH AttributeName=label,KeyType=RANGE --provisioned-throughput ReadCapacityUnits=10,WriteCapacityUnits=5 aws dynamodb put-item --table-name eventstream --item '{"timestamp": {"S":"1002"}, "label": {"S":"auto"}}' --region us-west-2 aws dynamodb get-item --table-name eventstream --key '{"timestamp": {"S":"1002"}, "label": {"S":"auto"}}' --region us-west-2

import uuid import boto3 import json

def lambda_handler(event, context):

Get the service client.

s3 = boto3.client('s3')
db = boto3.client('dynamodb')

# Generate a random S3 key name
upload_key = uuid.uuid4().hex

# Generate the presigned URL for put requests
presigned_url = s3.generate_presigned_url(
    ClientMethod='put_object',
    Params={
        'Bucket': 'lambda.input.uswest2.packet.global',
        'Key': upload_key
    }
)

data = db.get_item(
  TableName='eventstream',
  Key={"timestamp": {"S":"1002"}, "label": {"S":"auto"}}
)
print(data);

# Return the presigned URL
return {
    "upload_url": presigned_url
}

Function Logs START RequestId: 4fe79619-1e5a-4091-8671-47a94c1a4ea7 Version: $LATEST {'Item': {'timestamp': {'S': '1002'}, 'label': {'S': 'auto'}}, 'ResponseMetadata': {'RequestId': 'ENJLR12F2UOEP68IC7F67BN70VVV4KQNSO5AEMVJF66Q9ASUAAJG', 'HTTPStatusCode': 200, 'HTTPHeaders': {'server': 'Server', 'date': 'Tue, 22 Jun 2021 17:19:31 GMT', 'content-type': 'application/x-amz-json-1.0', 'content-length': '56', 'connection': 'keep-alive', 'x-amzn-requestid': 'ENJLR12F2UOEP68IC7F67BN70VVV4KQNSO5AEMVJF66Q9ASUAAJG', 'x-amz-crc32': '2268759621'}, 'RetryAttempts': 0}} END RequestId: 4fe79619-1e5a-4091-8671-47a94c1a4ea7 REPORT RequestId: 4fe79619-1e5a-4091-8671-47a94c1a4ea7 Duration: 1790.06 ms Billed Duration: 1791 ms Memory Size: 128 MB Max Memory Used: 81 MB Init Duration: 271.52 ms