If you are developing Terraform you will at some point work with Terraform imports. A simple web search yields plenty of results for simple imports of Terraform resources. However, often missing are some of the more complex or nuanced imports one might encounter in the real world (such as importing modules or resources created from for_each and count).

This guide will quickly cover the generic examples you find easily on the web, focus on some more unique stuff usually hidden in forum posts, and provide a handful of techniques I’ve picked up since Terraform imports became a functionality.

This guide assumes the reader has a good understanding of Terraform, Terraform modules, state file manipulation, and CI/CD. I’ll be using AWS for the examples.

Resource Import

This is perhaps the most prevalent example when searching for Terraform imports. Quite simply you have a resource defined in your Terraform code, some infrastructure out in the environment matching your Terraform resource definition, and you want to import that infrastructure into your Terraform state.

This example is for an aws_iam_user. I’ve already created a user named “bill” via the AWS IAM console and I would like to import this user into my Terraform state. Easy enough!

My Terraform code:

resource "aws_iam_user" "bill" {
  name = "bill"
  tags = { "foo" = "bar" }
}

A simple command:

$ terraform import aws_iam_user.bill bill

#aws #terraform-import #infrastructure-as-code #terraform-modules #terraform

Terraform Imports: Resources, Modules, for_each, and Count
8.80 GEEK