Skip to content

Instantly share code, notes, and snippets.

@abranhe
Created February 21, 2021 11:28
Show Gist options
  • Save abranhe/a8708c9787df27415b27d13213394ea3 to your computer and use it in GitHub Desktop.
Save abranhe/a8708c9787df27415b27d13213394ea3 to your computer and use it in GitHub Desktop.
Cloudformation Lamda Random Value
# Just adding support for a lambda
LambdaExecutionRole:
Type: AWS::IAM::Role
Properties:
AssumeRolePolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Principal:
Service:
- lambda.amazonaws.com
Action:
- sts:AssumeRole
Policies:
- PolicyName: allowLambdaLogging
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: 'Allow'
Action:
- 'logs:*'
Resource: '*'
# Generate a random color for the text in the webserver
# Taken from https://www.itonaut.com/2018/01/03/generate-passwords-in-aws-cloudformation-template/
LambdaFunction:
Type: AWS::Lambda::Function
Properties:
Code:
ZipFile: >
const response = require('cfn-response');
exports.handler = (event, context) => {
// https://stackoverflow.com/a/5365036/7602110
const responseData = {RandomColor: `#${((1<<24) * Math.random() | 0).toString(16)}`};
response.send(event, context, response.SUCCESS, responseData);
};
Handler: index.handler
Runtime: nodejs12.x
Role: !GetAtt LambdaExecutionRole.Arn
MemorySize: 128
Timeout: 20
# Custom Resource
ColorString:
Type: AWS::CloudFormation::CustomResource
Properties:
Length: 7
ServiceToken: !GetAtt LambdaFunction.Arn
@abranhe
Copy link
Author

abranhe commented Feb 21, 2021

Usage:

Color: !GetAtt ColorString.RandomColor

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment