Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add import capabilities to cdktf #2972

Merged
merged 25 commits into from
Oct 5, 2023
Merged

Conversation

DanielMSchmidt
Copy link
Contributor

@DanielMSchmidt DanielMSchmidt commented Jun 28, 2023

In this PR is a feature addition regarding integrating Terraform's import functionality into CDKTF.

Two Ways to Import

With Config Generation

There have been changes to the provider generator where a new static method importGenerateConfig appears on all resources.

S3Bucket.importGenerateConfig(this, "bucket", "bucket-to-import");

importGenerateConfig returns the new ImportableResource

public static importGenerateConfig(scope: Construct, name: string, id: string, provider?: cdktf.TerraformProvider) {
    return new cdktf.ImportableResource(scope, name, { terraformResourceType: "aws_acmpca_certificate", importId: id, provider });
      }

ImportableResource adds the import block to our json config, then by running cdktf plan <stack-name> the resulting config (as ran through convert) is returned

Screenshot 2023-09-25 at 12 08 29 PM

The config returned can then be added, then subsequently deployed

Without Config Generation

A new instance method importFrom on TerraformResource

new S3Bucket(this, "bucket", {}).importFrom(bucketId);

It adds needed info (where we are importing from and to) to the global imported variable, whose contents are then used in thetoTerraform method of TerraformResource

....
return {
      import: this.imported
        ? [
            {
              provider: this.imported.provider?.fqn,
              id: this.imported.id,
              to: `${this.terraformResourceType}.${this.friendlyUniqueId}`,
            },
          ]
        : undefined,
      resource: {
        [this.terraformResourceType]: {
          [this.friendlyUniqueId]: attributes,
        },
      },
    };

The resource will then be imported on deploy.

Changes to CLI

In plan of terraform-cli.ts we automatically add the new tag -generate-config-out on the detection of any import block.

Testing

Due to the nature of this feature, integration tests are not included. As doing so would require having resources to be imported from elsewhere. Considering the potential for such tests to flake, I abstained from including any.

Unit Tests

Unit tests have been created for both the additions to the provider generator (tests for properly including new static functions) as well as the additions to TerraformResource (snapshot of json config includes import block and resource to be imported to)

Local Usage

Screenshot 2023-09-25 at 12 09 28 PM

TFC Usage

Screenshot 2023-09-19 at 2 39 50 PM
Screenshot 2023-09-19 at 2 40 00 PM

Other Notes

  1. Due to plannable imports only being available in terraform versions greater than 1.5, validations have been added that check for the proper usage
  • In both importFrom of TerraformResource and in ImportableResource
this.node.addValidation(
      new ValidateTerraformVersion(
        ">=1.5",
        `Import blocks are only supported for Terraform >=1.5. Please upgrade your Terraform version.`
      )
);
  1. As this is the first change of the cdktf package since the recent addition of gradle, I had the pleasure of finding a bug in their examples script– specifically reinstall.sh. As such, changes in those files are the associated bug fix.
  2. As I (and @ansgarm 🤘) perceive that the importFrom function on TerraformResource could potentially have collisions with certain provider's naming, I've added an escaped attribute for such instances in attribute-model.ts.

@DanielMSchmidt DanielMSchmidt requested a review from a team as a code owner June 28, 2023 14:24
@DanielMSchmidt DanielMSchmidt requested review from mutahhir and ansgarm and removed request for a team June 28, 2023 14:24
@DanielMSchmidt DanielMSchmidt marked this pull request as draft June 28, 2023 14:24
@xiehan xiehan linked an issue Jul 20, 2023 that may be closed by this pull request
@xiehan xiehan added this to the 0.19 (tentative) milestone Aug 28, 2023
Copy link
Contributor

github-actions bot commented Nov 5, 2023

I'm going to lock this pull request because it has been closed for 30 days. This helps our maintainers find and focus on the active issues. If you've found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Nov 5, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support terraform import via the cdk cli
5 participants