INNER CODE UNIT · Python
lambda_handler
aws-samples/serverless-test-samples · python-test-samples/apigw-lambda-dynamodb/src/app.py:28
def lambda_handler(event: APIGatewayProxyEvent, context: LambdaContext) -> dict:
# Retrieve the table name from the environment, and create a boto3 Table object
dynamodb_table_name = environ["DYNAMODB_TABLE_NAME"]
dynamodb_resource = boto3.resource('dynamodb')
dynamodb_table = dynamodb_resource.Table(dynamodb_table_name)
print(f"Using DynamoDB Table {dynamodb_table_name}.")
# User id field is passed as a path parameter
id = event["pathParameters"]["id"]
# Retrieve the person's name from an id, and construct the message.
dynamodb_response = dynamodb_table.get_item(Key={"PK": f"{id}", "SK": "NAME#"})
# Does the person exist for this id?
if "Item" in dynamodb_response and "data" in dynamodb_response["Item"]:
person_name = dynamodb_response["Item"]["data"]
hello_message = f"Hello {person_name}!"