diff --git a/packages/cdktf/lib/terraform-element.ts b/packages/cdktf/lib/terraform-element.ts index 2011bcd826..a8318ea84a 100644 --- a/packages/cdktf/lib/terraform-element.ts +++ b/packages/cdktf/lib/terraform-element.ts @@ -29,6 +29,10 @@ export class TerraformElement extends Construct { public get friendlyUniqueId() { const node = this.constructNode + if (this.stack.useStaticNames) { + return node.id; + } + const components = node.scopes.slice(1).map(c => Node.of(c).id); return components.length > 0 ? makeUniqueId(components) : ''; } diff --git a/packages/cdktf/lib/terraform-stack.ts b/packages/cdktf/lib/terraform-stack.ts index 632d42c2d0..b4707317c3 100644 --- a/packages/cdktf/lib/terraform-stack.ts +++ b/packages/cdktf/lib/terraform-stack.ts @@ -17,6 +17,7 @@ export class TerraformStack extends Construct { public readonly artifactFile: string; private readonly rawOverrides: any = {} private readonly cdktfVersion: string; + private staticNames = false; constructor(scope: Construct, id: string) { super(scope, id); @@ -72,6 +73,14 @@ export class TerraformStack extends Construct { curr[lastKey] = value; } + public useConstructIdsAsNames() { + this.staticNames = true; + } + + public get useStaticNames(): boolean { + return this.staticNames; + } + public allProviders(): TerraformProvider[] { const providers: TerraformProvider[] = []; diff --git a/packages/cdktf/test/__snapshots__/stack.test.js.snap b/packages/cdktf/test/__snapshots__/stack.test.js.snap index 55c35f97f0..bd07c6b462 100644 --- a/packages/cdktf/test/__snapshots__/stack.test.js.snap +++ b/packages/cdktf/test/__snapshots__/stack.test.js.snap @@ -87,3 +87,91 @@ exports[`stack synthesis merges all elements into a single output 1`] = ` } }" `; + +exports[`use static names 1`] = ` +"{ + \\"//\\": { + \\"metadata\\": { + \\"version\\": \\"stubbed\\", + \\"stackName\\": \\"MyStack\\" + } + }, + \\"provider\\": { + \\"test\\": [ + { + \\"access_key\\": \\"foo\\" + } + ] + }, + \\"resource\\": { + \\"aws_bucket\\": { + \\"Resource1\\": { + \\"foo\\": \\"Resource1\\", + \\"prop1\\": \\"bar1\\", + \\"prop2\\": 1234, + \\"prop3\\": { + \\"name\\": \\"should be overwritten in resource 2\\", + \\"value\\": 5678 + }, + \\"//\\": { + \\"metadata\\": { + \\"path\\": \\"MyStack/Resource1\\", + \\"uniqueId\\": \\"Resource1\\" + } + } + } + }, + \\"aws_topic\\": { + \\"Resource2\\": { + \\"foo\\": \\"Resource2\\", + \\"prop1\\": \\"bar1\\", + \\"prop3\\": { + \\"name\\": \\"test\\", + \\"value\\": 5678 + }, + \\"//\\": { + \\"metadata\\": { + \\"path\\": \\"MyStack/Resource2\\", + \\"uniqueId\\": \\"Resource2\\" + } + }, + \\"provisioner\\": [ + { + \\"local-exec\\": { + \\"command\\": \\"echo 'Hello World' >example.txt\\" + } + } + ] + } + } + }, + \\"module\\": { + \\"EksModule\\": { + \\"cluster_name\\": \\"my_cluster_name\\", + \\"source\\": \\"terraform-aws-modules/eks/aws\\", + \\"version\\": \\"7.0.1\\", + \\"//\\": { + \\"metadata\\": { + \\"path\\": \\"MyStack/EksModule\\", + \\"uniqueId\\": \\"EksModule\\" + } + } + } + }, + \\"output\\": { + \\"eks_version\\": { + \\"value\\": \\"7.0.1\\" + } + }, + \\"terraform\\": { + \\"backend\\": { + \\"remote\\": { + \\"organization\\": \\"test\\", + \\"workspaces\\": { + \\"name\\": \\"test\\" + } + } + } + } +}" +`; diff --git a/packages/cdktf/test/stack.test.ts b/packages/cdktf/test/stack.test.ts index a10611c4f0..ae8b9387df 100644 --- a/packages/cdktf/test/stack.test.ts +++ b/packages/cdktf/test/stack.test.ts @@ -47,6 +47,52 @@ test('stack synthesis merges all elements into a single output', () => { expect(Testing.synth(stack)).toMatchSnapshot(); }); +test('use static names', () => { + const app = Testing.stubVersion(new App({stackTraces: false})); + const stack = new TerraformStack(app, 'MyStack'); + stack.useConstructIdsAsNames(); + + new TestProvider(stack, 'test-provider', { + accessKey: 'foo' + }) + + new MyResource(stack, 'Resource1', { + terraformResourceType: 'aws_bucket' + }); + + const overrideResource = new MyResource(stack, 'Resource2', { + terraformResourceType: 'aws_topic', + }) + overrideResource.addOverride('//', 'this is a comment'); + overrideResource.addOverride('prop2', undefined); + overrideResource.addOverride('prop3.name', 'test'); + overrideResource.addOverride('provisioner', [{ + 'local-exec': { + command: "echo 'Hello World' >example.txt" + } + }]); + + const eks = new MyModule(stack, 'EksModule', { + source: 'terraform-aws-modules/eks/aws', + version: '7.0.1', + }); + + new TerraformOutput(stack, "eks_version", { + value: eks.version + }) + + stack.addOverride('terraform.backend', { + remote: { + organization: 'test', + workspaces: { + name: 'test' + } + } + }); + + expect(Testing.synth(stack)).toMatchSnapshot(); +}); + class MyModule extends TerraformModule { protected synthesizeAttributes() { return {