Created
October 10, 2022 01:35
-
-
Save mbonig/bc82959356c5ff0b3be3c63d9ff0481b to your computer and use it in GitHub Desktop.
An AWS CDK LogicalId remapper aspect
This file contains hidden or 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
import { CfnElement, CfnResource, IAspect, Stack } from 'aws-cdk-lib'; | |
import { IConstruct } from 'constructs'; | |
export interface IdMap { | |
[key: string]: string; | |
} | |
export class LogicalIdMapper implements IAspect { | |
constructor(private idMap: IdMap) { | |
} | |
visit(node: IConstruct): void { | |
const currentLogicalId = Stack.of(node).getLogicalId(node as CfnElement); | |
// is there a map for this logicalId? | |
if (!!this.idMap[currentLogicalId]) { | |
// if we're on a L1 resource, try to do the override directly | |
if ((node as CfnResource).overrideLogicalId) return (node as CfnResource).overrideLogicalId(this.idMap[currentLogicalId]); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment