diff --git a/packages/cdktf/lib/terraform-provisioner.ts b/packages/cdktf/lib/terraform-provisioner.ts index 456a2a613e..7669f3ec92 100644 --- a/packages/cdktf/lib/terraform-provisioner.ts +++ b/packages/cdktf/lib/terraform-provisioner.ts @@ -353,7 +353,10 @@ export interface RemoteExecProvisioner { */ export class TerraformSelf { private static getInterpolation(key: string) { - return `self.${snakeCase(key)}`; + return `self.${key + .split(".") + .map((s) => snakeCase(s)) + .join(".")}`; } /** diff --git a/packages/cdktf/test/__snapshots__/terraform-provisioner.test.ts.snap b/packages/cdktf/test/__snapshots__/terraform-provisioner.test.ts.snap new file mode 100644 index 0000000000..45060f67b9 --- /dev/null +++ b/packages/cdktf/test/__snapshots__/terraform-provisioner.test.ts.snap @@ -0,0 +1,43 @@ +// Jest Snapshot v1, https://goo.gl/fbAQLP + +exports[`self with 1`] = ` +"{ + "provider": { + "test": [ + { + } + ] + }, + "resource": { + "test_resource": { + "bar": { + "name": "test", + "provisioner": [ + { + "local-exec": { + "command": "say 'hello world'", + "environment": { + "ECR_IMAGE_COMMAND": "delete", + "ECR_IMAGE_SOURCE_IMAGE": "\${self.triggers_replace.source_image}", + "ECR_IMAGE_TARGET_IMAGE": "\${self.image_name_with_hash}", + "ECR_IMAGE_TARGET_REGION": "\${self.triggers_replace.target_region}" + }, + "interpreter": [ + "bash" + ], + "when": "destroy" + } + } + ] + } + } + }, + "terraform": { + "required_providers": { + "test": { + "version": "~> 2.0" + } + } + } +}" +`; diff --git a/packages/cdktf/test/terraform-provisioner.test.ts b/packages/cdktf/test/terraform-provisioner.test.ts new file mode 100644 index 0000000000..8081507f6e --- /dev/null +++ b/packages/cdktf/test/terraform-provisioner.test.ts @@ -0,0 +1,35 @@ +// Copyright (c) HashiCorp, Inc +// SPDX-License-Identifier: MPL-2.0 +import { Testing, TerraformStack, TerraformSelf } from "../lib"; +import { TestResource } from "./helper"; +import { TestProvider } from "./helper/provider"; + +test("self with ", () => { + const app = Testing.app(); + const stack = new TerraformStack(app, "test"); + new TestProvider(stack, "foo", {}); + new TestResource(stack, "bar", { + name: "test", + provisioners: [ + { + when: "destroy", + type: "local-exec", + environment: { + ECR_IMAGE_COMMAND: "delete", + ECR_IMAGE_SOURCE_IMAGE: TerraformSelf.getString( + "triggers_replace.sourceImage" + ), + ECR_IMAGE_TARGET_IMAGE: TerraformSelf.getString( + "image_name_with_hash" + ), + ECR_IMAGE_TARGET_REGION: TerraformSelf.getString( + "triggers_replace.target_region" + ), + }, + interpreter: ["bash"], + command: "say 'hello world'", + }, + ], + }); + expect(Testing.synth(stack)).toMatchSnapshot(); +});