An old colleague of mine reached out to me for creating a random string within cloud formation. If one has not used it in past it can get tricky. I wish amazon created a function for the same but then how would I have showcased my love to SERVERLESS with this blog. I will be using Lambda function for creating random strings and Cloud formation custom function to call that lambda function.
Template Description
RandomizerTemplate.yaml
The parameter needed for a random string is the length of the string. By default, this template will create 6 char string.

Lambda function and respective execution role. I have created this lambda function in python. Since this will be created once and used many times in the future don’t need any expertise for python language. Just use this function as is that will suffice usage.

This template will export Lambda function ARN (RandomizerLambdaArn). This Lambda function arn will be used in the custom resource into a service token section.

Image 3
Calling Randomizer in your template (CreateBucketTemplate.yaml)
This is a way to call randomizer function in the template. Copy-paste this function into a template where a random string is needed.

To get random string use the following commands –
- RandomizerLambda.RandomString for random string
- RandomizerLambda.Lower_RandomString for lower case random string
- RandomizerLambda.Upper_RandomString for upper case random string
- RandomizerLambda.RandomNumber for numeric string

Download Randomizer template
Download code from the following GitHub link.
https://github.com/yogeshagrawal11/cloud/tree/master/aws/Cloud%20Formation/Randomizer
- RandomizerTemplate.yaml : Template to create a Randomizer lambda function.
- CreateBucketTemplate.yaml : Template to create a S3 bucket using Random string.
Prereq
Download template to a folder. Please ensure id used for implementing the template do have access to create the following resources –
- IAM Role
- IAM Policy
- Lambda function. Lambda function indeed will create Log group, Log stream for events.
- A custom resource to call the Lambda function for a random string.
Ensure cloud formation export name is not present in your environment. Export name – “RandomizerLambdaArn”
Implementation
I am working on CLI for implementation. This template can easily be deployed from the AWS console as well.
Download AWS SDK and configure it. Make sure to have proper access. Run the following command to implement the randomizer template.
- To configure aws SDK environment run –
- aws configure
- To validate randomizer template is good
- aws cloudformation validate-template –template-body “file://RandomizerTemplate.yaml”

- To install randomizer stack
- aws cloudformation create-stack –stack-name randomizerStack –disable-rollback –capabilities CAPABILITY_IAM –template-body “file://RandomizerTemplate.yaml”

Ensure Stack is configured successfully

Lambda function is created. Lambda execution role and policy created you can use existing role as well if needed to reduce role count. Lambda function will create AWS Cloudwatch loggroup and logstream for Lambda function metrics and output information. This is very useful. One can use this lambda function and parameters like a project, stackname, application name in stack output which can be tracked as well for accounting or analysis purposes.

Randomizerstack has a default input character length as 6 but it can be changed upon request of the stack.

Outputs do have random string-like, alphanumeric character, numeric character or just lower alphabets(used for S3 bucket name)

Validating bucket creation template.

Creating a bucket using a template. I am passing parameter value of 10 is nothing but I needed 10 character string for bucketname
- aws cloudformation create-stack –stack-name CreateBucketStack –parameters ParameterKey=RandomStringLength,ParameterValue=10 –template-body “file://CreateBucketTemplate.yaml”

A new bucket is created with 10 random string characters.

Creating a new bucket with default 6 char length string.

All 3 stacks are created.

New bucket with default 6 length character.

Default 6 value is assigned to the “RandomStringLength” parameter via RandomizerStack.

Both buckets are created. First bucket created with 10 char string whereas the second one with 6 char.

Clean up
Delete all 3 stacks via cli or GUI

CLI command to delete all 3 stacks
- aws cloudformation delete-stack –stack-name CreateBucketStack1
- aws cloudformation delete-stack –stack-name CreateBucketStack
- aws cloudformation delete-stack –stack-name randomizerStack
Lambda function will create Cloudwatch loggroup and log stream. Delete those log group by going to Cloudwatch -> Log groups -> Select appropriate log group by filtering “randomizer”. Select checkbox. Go to action and click delete.

Conclusion
Use this randomizer template for the need of a randomizer string. Very useful for ami name, autoscaling group name, and S3 bucket names.
PS. Security is not in mind with this blog. The intention is purely to kickstart my builders.
Enjoy !!!!!