Last active
January 25, 2021 13:50
-
-
Save allenheltondev/1281088e952ef428e7971ffd0b7ec1af to your computer and use it in GitHub Desktop.
CloudFormation Macro Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: [AWS::Serverless-2016-10-31, AddSSMParametersMacro] | |
Description: SAM template for consuming a CloudFormation Macro | |
Resources: | |
ExampleWithEnvVarFunction: | |
Type: AWS::Serverless::Function | |
Properties: | |
CodeUri: example-with-env-var | |
Handler: function.handler | |
Environment: | |
Variables: | |
LAMBDA_VALUE: ExampleFunction | |
ExampleWithoutEnvVarFunction: | |
Type: AWS::Serverless::Function | |
Properties: | |
CodeUri: example-without-env-var | |
Handler: function.handler | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: AWS::Serverless-2016-10-31 | |
Description: SAM template for creating a CloudFormation Macro | |
Resources: | |
AddSSMParametersFunction: | |
Type: AWS::Serverless::Function | |
Properties: | |
Description: CFN Macro function to automatically add action parameters based on the presence environment variables | |
CodeUri: auto-add-ssm-parameter | |
Handler: function.handler | |
AddSSMParametersMacro: | |
Type: AWS::CloudFormation::Macro | |
Properties: | |
Name: AddSSMParameters | |
Description: Adds SSM Parameters for lambdas with the LAMBDA_VALUE env var | |
FunctionName: | |
Ref: AddSSMParametersFunction |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
exports.handler = async function(event, context) { | |
const lambdas = []; | |
const resourceKeys = Object.keys(resources); | |
for (let i = 0; i < resourceKeys.length; i++) { | |
const resource = resources[resourceKeys[i]]; | |
if (resource.Type === 'AWS::Serverless::Function' && resource.Properties.Environment.Variables.LAMBDA_VALUE) { | |
lambdas.push({ | |
name: resourceKeys[i], | |
details: resource | |
}); | |
} | |
} | |
lambdas.forEach((lambda) => { | |
event.fragment.Resources[`${lambda.name}Parameter`] = { | |
Type: 'AWS::SSM::Parameter', | |
Properties: { | |
Name: `${lambda.name}Parameter`, | |
Type: 'String', | |
Value: lambda.Properties.Environment.Variables.LAMBDA_VALUE | |
} | |
}; | |
}); | |
return { | |
requestId: event.requestId, | |
status: 'success', | |
fragment: event.fragment | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment