skip to Main Content

I am trying to import existing s3 buckets to my terraform code. i have a lot of buckets in the s3 so I want to collect under a single resource name. For example, let’s consider 3 baskets running on s3, 2 of them are created with terraform but 1 of them is not created with terraform.

  • terraformed-bucket
  • terraformed-bucket-2
  • nonterraformed-bucket

I have one resource name for these two buckets. I want to import nonterraformed-bucket to existing resource name that used for terraformed-buckets when migrating to terraform code. but i cant :/

resource "aws_s3_bucket" "tfer--buckets" {
  count         = "${length(var.bucket_names)}"
  bucket        = "${element(var.bucket_names, count.index)}"

  # count         = length(local.bucket_names)
  # bucket        = local.bucket_names[count.index]
  force_destroy = "false"

  grant {
    id          = "674f4d195ff567a2eeb7ee328c84410b02484f646c5f1f595f83ecaf5cfbf"
    permissions = ["FULL_CONTROL"]
    type        = "CanonicalUser"
  }

  object_lock_enabled = "false"
  request_payer       = "BucketOwner"

  server_side_encryption_configuration {
    rule {
      apply_server_side_encryption_by_default {
        sse_algorithm = "AES256"
      }

      bucket_key_enabled = "true"
    }
  }

  versioning {
    enabled    = "false"
    mfa_delete = "false"
  }
}

and my variables:

variable "bucket_names" {
  type    = list
  default = ["terraformed-bucket", "terraformed-bucket-2"]
}

these are the states in my terraform code

mek-bash@%: terraform state list
aws_s3_bucket.tfer--buckets[0]
aws_s3_bucket.tfer--buckets[1]

i tried to import nonterraformed-bucket to existing this resource:

resource "aws_s3_bucket" "tfer–buckets" {}

with this command:
terraform import aws_s3_bucket.tfer--buckets nonterraformed-bucket

but still the output of terraform state list is the same. nothing changed:

mek-bash@%: terraform import aws_s3_bucket.tfer--buckets nonterraformed-bucket 

Import successful!

The resources that were imported are shown above. These resources are now in
your Terraform state and will henceforth be managed by Terraform.

mek-bash@%: terraform state list                                       
aws_s3_bucket.tfer--buckets[0]
aws_s3_bucket.tfer--buckets[1]

I don’t want to use separate resources for each bucket. So I want to import each outside bucket with the same name as the others. So I want to include it as [2] in the same resource name. justlike:

mek-bash@%: terraform state list
aws_s3_bucket.tfer--buckets[0]
aws_s3_bucket.tfer--buckets[1]
aws_s3_bucket.tfer--buckets[2] (should represent nonterraformed-bucket)

Do you have any suggestions for this? Or is there a way to import non-terraformed resources into a single resource name?

2

Answers


  1. Chosen as BEST ANSWER

    It worked with:

    terraform import 'aws_s3_bucket.tfer--buckets[2]' nonterraformed-bucket

    it fixed after quotes 'aws_s3_bucket.tfer--buckets[2]'


  2. You have to add your your nonterraformed-bucket into bucket_names:

    variable "bucket_names" {
      type    = list
      default = ["terraformed-bucket", "terraformed-bucket-2", "nonterraformed-bucket"]
    }
    

    and then import it as [2] (third bucket):

    terraform import aws_s3_bucket.tfer--buckets[2] nonterraformed-bucket
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search