12

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...

17

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
1
  • 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 yesterday
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
2
  • 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 '20 at 5:42
  • I have tested it. I tried as above it is working perfectly for me. Apr 17 '20 at 12:04
4

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.

1

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.