# The import block, and the resources nobody wrote down

> Terraform 1.5 made import part of the plan, and added config generation. Every account I have worked in has a drawer of things created by hand in 2019, and until this month adopting them was too tedious to schedule.

- Author: Deyan Peev (https://deyanpeev.com)
- Published: 2023-06-24
- Tags: Terraform, Infrastructure as Code, AWS, Migration, CI/CD
- URL: https://deyanpeev.com/posts/the-import-block-and-the-resources-nobody-wrote-down

Terraform 1.5 arrived in June with config-driven import and `check` blocks.
Import is the one I care about, because every AWS account I've worked in has
the same drawer in it. A security group somebody made in the console during an
incident, a bucket that predates the repository, a role attached to a thing no
one can identify.

Those resources are in use and Terraform can't see them. Adopting them was
always possible, it just was never worth the afternoon.

# The old terraform import

The old `terraform import` was a CLI command that mutated state. You ran it
against a resource address and an ID, it wrote the object into state, and then you
had to write the configuration yourself, from scratch, reading the console
attribute by attribute.

If you got the configuration wrong, the next plan proposed changing the live
resource to match what you had written. On a security group in front of a
production database, that's the sort of plan you read four times and then decide
to do next quarter.

It was also imperative in a declarative tool, so it left no record in the
repository. It was one resource per invocation, so forty resources meant forty
commands in right order. And there was no dry run. The import had already
happened by the time you found out whether your configuration matched.

# Import in the plan

Import is now a block in the configuration:

```hcl
import {
  to = aws_security_group.legacy_db
  id = "sg-0a1b2c3d4e5f67890"
}
```

That block takes part in the normal plan and apply cycle. `terraform plan` tells
you what importing would do *before* anything happens, including whether your
configuration matches the real object. That's the review that was missing.

And you no longer have to write the configuration by hand:

```bash
terraform plan -generate-config-out=generated.tf
```

Terraform reads the live resource and writes HCL for anything named in an import
block that has no configuration yet. The output needs editing. It's verbose,
it hardcodes values that should be variables or references, and it will include
attributes you would rather not pin. But editing generated HCL against a real
object is a different job from reconstructing it from a console page.

The workflow that comes out of this is the one I wanted. Add import blocks for a
batch, generate, read the generated configuration as a diff, tidy it, plan until
the diff is empty, apply, then delete the import blocks because they've done
their job. It all happens in a pull request where it can be reviewed, and it is
one plan for forty resources instead of forty commands.

# check blocks, which are not preconditions

1.5 also adds `check` blocks, and it's easy to confuse them with the
[preconditions from 1.2](https://deyanpeev.com/posts/terraform-learned-to-say-what-it-assumed).

```hcl
check "health" {
  data "http" "app" {
    url = "https://${aws_lb.app.dns_name}/health"
  }

  assert {
    condition     = data.http.app.status_code == 200
    error_message = "Application health check returned ${data.http.app.status_code}."
  }
}
```

A failed precondition halts the run. A failed check produces a warning and lets
the run finish. That sounds weaker, but it's meant to be. These are assertions
about the world and not about the configuration, and the world being briefly
wrong is not a reason to abandon an apply that is halfway through.

It gives you somewhere to put the checks that used to live in a smoke-test
script running after the pipeline, where they were easy to forget.

# The caveat

Generated configuration is a starting point that looks like an ending point. It
arrives complete, it plans cleanly, and it's very tempting to merge as-is. Do
that and you have adopted a resource along with every incidental attribute it
happened to have, hardcoded, with no variables and no references to the
resources it should be pointed at.

The value of importing something is that it becomes reviewable and reproducible.
Merging generated HCL unread gets you the first half and not the second. The
resource is in state, and the configuration still doesn't say why it is the way
it is.

So the rule I'm applying is that generated configuration is the first draft of
a pull request and gets edited before anyone reviews it. Even as a first draft
it beats reconstructing the resource from a console page.
