How to import already created AWS resources in TF CDK

Hi Team,

I’ve been working with TF CDK and able to complete a successful POC - I am looking for ways to import the already existing entities on my AWS Console.

If the existing resources were created with Terraform, you can use the from-terraform-project when initializing a cdktf project to convert the configuration. You’ll likely need to tweak a couple things by hand, but then you should be good to go.

If they weren’t created with Terraform, you have a couple options.

  • Use a tool to generate Terraform and then follow the above procedure. I have not personally used any of these tools, so I can’t speak to how will that will work.
  • Create cdktf code that mirrors the structure of the existing resource. You can then use Terraform to import the existing resources into the new configuration. See Import - Terraform by HashiCorp for more information on importing.

I used Terraform import for existing resources using cdktf code which mirrors the existing structure and it works perfectly.

Hi,

Are you able to modify the imported instances now easily?

Once imported terraform will make the required changes when you update your code and deploy

I’m trying to do this myself. Are you able to provide a code example? It would be very much appreciated.

There isn’t really a code sample to provide. The cdktf code needs to exactly match the existing resources that you are tying to import. There may be some tools that would help there, but you can also write by hand. You may need to use overrideLogicalId to make sure the resource names match.

terraform import doesn’t work as advertised when using cdktf, because there are no resulting terraform files to reference. Anyone saying its the same hasn’t really used cdktf.

Can you explain further what you mean? There aren’t hcl files generated, but cdktf does output Terraform compatible files.

Importing is not supported natively in cdktf, but it is possible to do. Support `terraform import` via the cdk cli · Issue #1134 · hashicorp/terraform-cdk · GitHub has some solutions that people have currently used.

I am using python cdktf with a remote backend stored in an s3 bucket. From my main directory, I do

terraform import aws_dynamodb_table.table_name table_name

I get

Error: No Terraform configuration files

You have to run this from the directory containing the synthesized code, by default that is cdktf.out/stacks/<stack name>. It only exists after you run cdktf synth.