20

Terraform v0.11.11

We have a quite big AWS setup created with Terraform. Different regions are applied separately, and that is why we have most of the things in modules which are imported like this:

module "assets" {
  source       = "../modules/aws-assets"
  environment  = "${var.environment}"
  upload_users = "${var.upload_users}"
}

We have one s3 bucket created manually, and I try to import it. Terraform import command does not find the configuration file and errors.

Error: resource address "aws_s3_bucket.machine-learning" does not exist in the configuration.

Before importing this resource, please create its configuration in the root module. For example:

resource "aws_s3_bucket" "machine-learning" {
  # (resource arguments)
}

terraform apply/plan do find the configuration and wants to create it which is not what we want?

Thanks for you help!

================================

Edit:

now moved configuration to root and ended up with different error...

3 Answers 3

32

The terraform import command uses the "Resource Address" syntax, which is a way to talk about objects in a configuration from outside of that configuration. (This is as opposed to references in the main Terraform language, which are always resolved in the context of a particular module.)

To refer to a resource that is declared in a child module, you can add a module path to the beginning of the address:

terraform import module.assets.aws_s3_bucket.machine-learning BUCKET-NAME

If you aren't sure which address to use for a particular resource instance, you can run terraform plan to see which instance address Terraform is proposing to create, and then use that same address with terraform import instead to tell Terraform to use the existing object.

For addresses that include sequences like [0] and ["foo"] to represent one of multiple instances of a module or resource, you'll need to use escaping or quoting to make sure your shell doesn't interpret those as its own metacharacters, and instead passes them on literally to Terraform:

  • On Unix-style shells, use single quotes to make the inner address be taken literally: terraform import 'aws_instance.example["foo"]'

  • On Windows, from the command interpreter cmd.exe, escape any quotes with a backslash:

    terraform import "aws_instance.example[\"foo\"]"

    (PowerShell's escaping rules for external programs are very awkward and so I would not suggest using PowerShell to run a command like this.)

2
  • To expand on this if there are multiple modules calling each other you may have something like this: terraform import module.my-bucket.module.my-generic-s3-bucket.aws_s3_bucket.this my-bucket-name Oct 19, 2021 at 12:57
  • I had to dig through my existing TF state in order to find the correct path, building it up to find the piece I was looking for. (new to terraform here, especially errors!)
    – ps2goat
    Feb 11 at 15:54
6

Run terraform plan first and use the names in the output.

Goal here is to import in the resources that are listed in the plan output so next time you run plan they won't be listed for add.

So aws_s3_bucket.machine-learning didn't exist in the plan output I bet. It could have been module.aws_s3_bucket.machine-learning or even module.aws_s3_bucket.machine-learning[0] depending on what your aws-assets.tf does.

3
  • This works! Thanks! Jun 2, 2021 at 13:39
  • Thats the best hack! :D Jan 5 at 11:19
  • Very helpful! Assuming you've correctly defined the resource/module (not necessarily an empty one), terraform plan shows the ADDR and, in some cases at least, the ID is also exposed (perhaps less directly). Use those as terraform import ADDR ID ... and you're manual changes are now part of your state! (But see escaping hints for indices in the other answer.) Feb 16 at 3:29
4

enter image description here

main.tf

module "assets" {
  source       = "./modules"
}

modules/aws-assests.tf

resource "aws_s3_bucket" "machine-learning" {
  # (resource arguments)
}

To install the aws_s3_bucket module run the below command

# terraform init
3
  • Sorry, but this does not help anything. My config was that already(module was in ./modules/aws-assets but otherwise exactly the same. It gives same error still: ❯ terraform import aws_s3_bucket.machine-learning machine-learning Error: resource address "aws_s3_bucket.machine-learning" does not exist in the configuration. Before importing this resource, please create its configuration in the root module. For example: resource "aws_s3_bucket" "machine-learning" { # (resource arguments) }
    – Bysmyyr
    Apr 17, 2020 at 5:42
  • I have tested it. I tried as above it is working perfectly for me. Apr 17, 2020 at 12:04
  • This seems to be answering a totally different question: "How to create and initialize a skeleton project that uses a module". This answer does not address the question at hand: "How to terraform import a manually created resource defined as a module". Feb 15 at 22:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.