AWSTemplateFormatVersion: "2010-09-09" Description: >- Lambda function to generate a random string. **WARNING** This template creates and executes an Amazon Lambda Function You will be billed for the AWS resources used if you create a stack from this template. Parameters: StringLength: Description: The lenght of the string Type: String Resources: RandomStringResource: Type: Custom::RandomString Properties: ServiceToken: !GetAtt "RandomStringFunction.Arn" Length: !Ref StringLength RandomStringFunction: Type: AWS::Lambda::Function Properties: Handler: index.lambda_handler Timeout: 10 Role: !GetAtt "LambdaExecutionRole.Arn" Runtime: python3.6 Code: ZipFile: | import cfnresponse from random import choice from string import ascii_lowercase, digits def random_string(length=8, chars=ascii_lowercase + digits): return "".join(choice(chars) for x in range(length)) def lambda_handler(event, context): print(f"Data in event: {event}") response_data = {} if event["RequestType"] == "Create": string_length = int(event["ResourceProperties"]["Length"]) physicalResourceId = random_string(string_length) response_data = { "RandomString": physicalResourceId } else: # if event["RequestType"] == "Update" or event["RequestType"] == "Delete": physicalResourceId = event["PhysicalResourceId"] cfnresponse.send(event, context, cfnresponse.SUCCESS, response_data, physicalResourceId) LambdaExecutionRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Statement: - Effect: Allow Principal: Service: lambda.amazonaws.com Action: sts:AssumeRole Condition: {} Path: / ManagedPolicyArns: - arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole Outputs: RandomStringFromRandomStringResource: Description: "Name generated by RandomStringResource" Value: !GetAtt RandomStringResource.RandomString