Created
June 9, 2021 05:17
-
-
Save resistdesign/55ac908d3c1e512ac29c35a882e87d11 to your computer and use it in GitHub Desktop.
AWS CDK Template Param Helper
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
export class ParamWithMeta extends CfnParameter { | |
constructor( | |
scope: Construct, | |
id: string, | |
props?: { label?: string; group?: string } & CfnParameterProps | |
) { | |
const { label = id, group = 'Parameters', ...paramProps } = props || {}; | |
super(scope, id, paramProps); | |
if (scope instanceof Stack) { | |
const stackScope: Stack = scope as Stack; | |
stackScope.templateOptions.metadata = | |
stackScope.templateOptions.metadata || {}; | |
stackScope.templateOptions.metadata['AWS::CloudFormation::Interface'] = | |
stackScope.templateOptions.metadata['AWS::CloudFormation::Interface'] || | |
{}; | |
stackScope.templateOptions.metadata[ | |
'AWS::CloudFormation::Interface' | |
].ParameterGroups = | |
stackScope.templateOptions.metadata['AWS::CloudFormation::Interface'] | |
.ParameterGroups || []; | |
stackScope.templateOptions.metadata[ | |
'AWS::CloudFormation::Interface' | |
].ParameterLabels = | |
stackScope.templateOptions.metadata['AWS::CloudFormation::Interface'] | |
.ParameterLabels || {}; | |
const { | |
templateOptions: { | |
metadata: { | |
['AWS::CloudFormation::Interface']: { | |
ParameterGroups = [], | |
ParameterLabels = {}, | |
} = {}, | |
} = {}, | |
} = {}, | |
} = stackScope; | |
const targetParamGrp = ParameterGroups.filter( | |
(pg: Record<any, any>) => pg.Label === group | |
)[0] || { | |
Label: group, | |
Parameters: [], | |
}; | |
targetParamGrp.Parameters.push(label); | |
if (ParameterGroups.indexOf(targetParamGrp) === -1) { | |
ParameterGroups.push(targetParamGrp); | |
} | |
ParameterLabels[id] = { | |
default: label, | |
}; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment