• IaC Insights
  • Posts
  • How to Accelerate Importing Resources and Generating HCL

How to Accelerate Importing Resources and Generating HCL

You know what is a serious pain? Getting ClickOps resources to be managed with IaC.

Hey folks,

Writing the HCL for every resource that you're importing and starting to manage in IaC can be a painful, tedious experience.

Let's stop doing that.

I know some folks have great IaC setups, but plenty of folks have largely ClickOps infra and are still trying to figure out how to get it managed. If this is you, read on.

Not many know about this yet, but both Terraform and OpenTofu provide a feature to accelerate importing resources. They inspect your live resources that you are planning to import and generate the HCL for the resource’s current configuration.

Once your import blocks are defined in .tf files, including corresponding identifiers that allow TF to look up the underlying cloud resources, you can use a specific TF plan argument to generate their corresponding HCL.

The command looks like this:

tofu plan -generate-config-out="_generated.tf"

When this command is executed, TF calls out to the cloud for each resource specified in your import blocks, wherever they are defined. If it doesn't find corresponding HCL for the resources you're trying to import in your existing .tf files, it will write new HCL for those resources into the output file (in this case, _generated.tf).

For instance, if you are importing aws_s3_bucket.legacy_logs, TF will create a resource "aws_s3_bucket" "legacy_logs" { ... } block in the configured output file.

It will populate that resource block with all settings from the actual S3 bucket so that the HCL matches the actual state of that S3 bucket in your cloud.

Is this process perfect?

No.

You should still carefully review this generated code, move it around as needed, and ensure it makes logical sense in your IaC codebase.

BUT this baseline, auto-generated HCL gives you an incredible starting point. It is very much worth using to your advantage.

If you’ve used this, would love to hear your experience. Hit reply and let me know.

May your resources all import smoothly,

Matt @ Masterpoint

PS If you want to chat about how to move your infra from ClickOps to IaC bliss, grab some time on my calendar here. Or if you’ve found this newsletter helpful, please forward it to a friend.