Skip to content

Instantly share code, notes, and snippets.

@resistdesign
Created June 9, 2021 05:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save resistdesign/55ac908d3c1e512ac29c35a882e87d11 to your computer and use it in GitHub Desktop.
Save resistdesign/55ac908d3c1e512ac29c35a882e87d11 to your computer and use it in GitHub Desktop.
AWS CDK Template Param Helper
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