skip to Main Content

I have multiple Terraform projects, in project A I create a bucket like this:

resource "aws_s3_bucket" "s3_bucket" {
  bucket_prefix = "s3-bucket-"
}

this create a bucket with a random characters in the ends:

s3-bucket-20230927170795326300000001

Now I want to refer the name of this bucket in another project B, so I create:

data "aws_s3_bucket" "s3_bucket" {
  bucket = "s3-bucket-"
}

But this will not work because the name is random, is there any trick to solve this?

2

Answers


  1. You can use the bucket argument in the data source for that but with a full bucket name, e.g:

    data "aws_s3_bucket" "s3_bucket" {
      bucket = "s3-bucket-20230927170795326300000001"
    }
    
    Login or Signup to reply.
  2. You can try importing the bucket into project B:

    terraform import 'aws_s3_bucket.s3_bucket' your-new-bucket-name
    

    https://developer.hashicorp.com/terraform/cli/import

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search