1 Answer
- Newest
- Most votes
- Most comments
0
Hi,
to inline your Lambda code directly in the CloudFormation template, follow this example https://github.com/aws-samples/aws-cloudformation-inline-python-lambda-example/blob/main/base-cfn-lambda.yml from this repo https://github.com/aws-samples/aws-cloudformation-inline-python-lambda-example
.....
Code:
ZipFile: |
# Imports
import os
import boto3
import botocore
import logging
import random
import string
# Set up clients and resources
ddbclient = boto3.client('dynamodb')
# Set up the logger
logger = logging.getLogger()
logger.setLevel(logging.INFO)
#logger.setLevel(logging.DEBUG) # Very verbose
# Env variables from CFN
tablename = os.environ.get('TableName')
keyname = os.environ.get('KeyName')
def lambda_handler(event, context):
# Generate a random string to ensure no duplicates are put into DDB table
randomstring = (''.join(random.choice(string.ascii_letters) for i in range(10)))
logger.info('Random string generated: %s', randomstring)
def ddb_client(tablename, keyname, stringdata):
response = ddbclient.put_item(
Item={
keyname: {
'S': stringdata
}
},
ReturnConsumedCapacity='TOTAL',
TableName=tablename
)
return(response)
try:
ddb_response = ddb_client(tablename, keyname, randomstring)
logger.info(ddb_response)
except botocore.exceptions.ClientError as error:
# Put your error handling logic here
raise error
return(ddb_response)
DynamoDBTable:
Type: AWS::DynamoDB::Table
......
Hope it helps, Didier
Relevant content
- asked 9 months ago
- asked a year ago
- Accepted Answerasked 2 months ago
- AWS OFFICIALUpdated 3 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
thanks for your answer,I want know how to real-time monitoring of A bucket, copying files to B bucket while adding them to A bucket ,and when I build a new environment I want copy A bucket's object to B bucket. and how to create tables and indexes for DocumentDB by CloudFormat and Lambda Function.I can hardly find any information for this two questions.