Skip to content

Instantly share code, notes, and snippets.

@mbonig
Created October 10, 2022 01:35
Show Gist options
  • Save mbonig/bc82959356c5ff0b3be3c63d9ff0481b to your computer and use it in GitHub Desktop.
Save mbonig/bc82959356c5ff0b3be3c63d9ff0481b to your computer and use it in GitHub Desktop.
An AWS CDK LogicalId remapper aspect
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