Evgol
Evgol

Reputation: 57

How to get URL S3 object using Terraform?

I uploaded my file in a S3 bucket using Terraform. How can I get its URL like S3://bucket/object?

Im going to use

output "my_bucket_file_version" {
  value = aws_s3_bucket_object.object.version_id
}

but I suppose that it wont be work.

Upvotes: 1

Views: 5051

Answers (2)

Angel Chaidez
Angel Chaidez

Reputation: 11

I don't know if this will help you but I had to do some manipulation of the outputs, and I had to enter some information manually, however this will not work if in the future AWS decides to add/change their domain structure (highly-unlikely). You will have to follow AWS domain structure

https://<bucket-name>.s3.<region>.amazonaws.com/<object-key>
output "url" {
    description = "url of the bucket"
    value       = "https://${aws_s3_bucket.s3_bucket.id}.s3.${aws_s3_bucket.s3_bucket.region}.amazonaws.com/index.html"
}

Upvotes: 1

Martin Atkins
Martin Atkins

Reputation: 74739

The s3:// URI scheme is not actually represented in the S3 API and is instead a UI-level detail in some tools that work with S3 buckets. Since the hashicorp/aws provider for Terraform is largely just reflecting details from the underlying API, it doesn't currently have any attribute which provides a ready-to-use s3:// URI.

However, this URI convention has a documented structure and so it's possible to build it from parts:

  • Literal s3:// prefix
  • Bucket name
  • Literal / to delimit the object key.
  • The object key

To construct this you'll need both the S3 bucket name and the object key. The aws_s3_bucket_object resource type includes attributes for both of those:

output "object_s3_uri" {
  value = "s3://${aws_s3_bucket_object.object.bucket}/${aws_s3_bucket_object.object.key}"
}

The documentation for the s3:// URL scheme doesn't mention anything about how to refer to a specific version of an object, so I don't think it's possible to construct a URI for a particular object version.

Upvotes: 6

Related Questions