AWSTemplateFormatVersion: '2010-09-09' Resources: DynamoDBTable: Type: AWS::DynamoDB::Table Properties: TableName: CustomerLambdaUDF AttributeDefinitions: - AttributeName: "id" AttributeType: "S" KeySchema: - AttributeName: "id" KeyType: "HASH" BillingMode: PAY_PER_REQUEST InitRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Principal: Service: - lambda.amazonaws.com Action: - sts:AssumeRole Path: / Policies: - PolicyName: CloudwatchLogs PolicyDocument: Version: 2012-10-17 Statement: - Effect: Allow Action: - logs:CreateLogGroup Resource: - !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:*" - Effect: Allow Action: - logs:CreateLogStream - logs:PutLogEvents Resource: - !Sub "arn:aws:logs:${AWS::Region}:${AWS::AccountId}:log-group:/aws/lambda/*" - Effect: Allow Action: - dynamodb:PutItem Resource: - !Sub "arn:aws:dynamodb:${AWS::Region}:${AWS::AccountId}:table/*" InitFunction: Type: AWS::Lambda::Function Properties: Role: !GetAtt 'InitRole.Arn' Handler: index.handler Runtime: python3.9 Timeout: 30 Code: ZipFile: | import boto3, json, urllib3 http = urllib3.PoolManager() dynamodb = boto3.resource('dynamodb') def send(event, context, responseStatus, responseData): responseUrl = event['ResponseURL'] responseBody = { 'Status' : responseStatus, 'Reason' : "See the details in CloudWatch Log Stream: {}".format(context.log_stream_name), 'PhysicalResourceId' : context.log_stream_name, 'StackId' : event['StackId'], 'RequestId' : event['RequestId'], 'LogicalResourceId' : event['LogicalResourceId'], 'NoEcho' : False, 'Data' : responseData } json_responseBody = json.dumps(responseBody) headers = { 'content-type' : '', 'content-length' : str(len(json_responseBody)) } try: response = http.request('PUT', responseUrl, headers=headers, body=json_responseBody) except Exception as e: print("send(..) failed executing http.request(..):", e) def handler(event, context): print("Received event: " + json.dumps(event, indent=2)) if event['RequestType'] == 'Delete': send(event, context, 'SUCCESS', {'Data': 'Delete complete'}) else: try: table = dynamodb.Table(event['ResourceProperties']['DynamoTableName']) table.put_item(Item={'id':'1','fname':'John','lname':'Doe'}) table.put_item(Item={'id':'2','fname':'Jane','lname':'Doe'}) table.put_item(Item={'id':'3','fname':'Carlos','lname':'Salazar'}) table.put_item(Item={'id':'4','fname':'Diego','lname':'Ramirez'}) table.put_item(Item={'id':'5','fname':'Mary','lname':'Major'}) table.put_item(Item={'id':'6','fname':'Richard','lname':'Roe'}) send(event, context, 'SUCCESS', {'Data': 'Insert complete'}) except Exception as e: print(e) send(event, context, 'FAILED', {'Data': 'Insert failed'}) InitializeDynamoDB: Type: Custom::InitFunction DeletionPolicy: Retain DependsOn: DynamoDBTable Properties: ServiceToken: Fn::GetAtt: [ InitFunction , "Arn" ] DynamoTableName: CustomerLambdaUDF