Home > Blockchain >  import an existing bucket in terraform
import an existing bucket in terraform

Time:05-20

I wanted to create an event notification on an existing s3_bucket (which is not setup by me in this current terraform code).

I came across this answer:

terraform aws_s3_bucket_notification existing bucket

so I tried this. Here, local.bucket_name is the name of the existing bucket.

notification.tf

resource "aws_s3_bucket" "trigger_pipeline" {
  bucket = local.bucket_name
}

terraform import aws_s3_bucket.trigger_pipeline local.bucket_name

However, I am not sure how to use this import statement. Do I use it after the resource block? Do I use it in the beginning of the same file?

If I use it as it is, under the resource block, I get this error:

Invalid block definition: Either a quoted string block label or an opening brace ("{") is expected here.HCL

at the dot here: aws_s3_bucket.trigger_pipeline

Edit:

So first I defined s3 resource as shown in the question above. Then I run terraform init. Next, I run terraform import aws_s3_bucket.trigger_pipeline "myoriginalbucketname" on the CLI. However, I still get the error that:

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

resource "aws_s3_bucket" "trigger_pipeline" {
  # (resource arguments)
}

I guess I am getting the sequence of events wrong

CodePudding user response:

local.bucket_name executes in your bash, not in TF. You have to actually provide the full name:

terraform import aws_s3_bucket.trigger_pipeline "my-bucket-name"
  • Related