Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to add a file in S3 using terraform #17554

Closed
shubhasismathur opened this issue Mar 11, 2018 · 3 comments
Closed

Unable to add a file in S3 using terraform #17554

shubhasismathur opened this issue Mar 11, 2018 · 3 comments

Comments

@shubhasismathur
Copy link

Hello Team,

I have facing one issue. Whenever I am trying to add a file in S3 in the same .tf file, the error comes that S3 bucket doesnt exist. However If I apply the below tf file again, it works.
So my requirement is that the below file should work such that it creates the bucket and so also add the file in the same bucket. RIght now its not doing it for the first time have to re apply to insert the file.'

Below is the file:

#1. Populating account details
provider "aws" {
access_key = "XXXXXXXXX"
secret_key = "XXXXXXXXX"
region = "us-east-1"
}

#2. CReation of Bucket "shubhasisterraform"
resource "aws_s3_bucket" "b" {
bucket = "shubhasisterraform"
acl = "private"
tags {
Name = "My bucket"
Environment = "Dev"
}
}

#3. adding of file accountdetails in S3 bucket(shubhasisterraform)
resource "aws_s3_bucket_object" "bucket" {
bucket = "shubhasisterraform"
key = "new_object_key"
source = "/root/S3/accountdetails"
etag = "${md5(file("/root/S3/accountdetails"))}"
}

@jbardin
Copy link
Member

jbardin commented Mar 12, 2018

Hi @shubhasismathur,

In order for terraform to know that the bucket must be created before the object, you must somehow reference the bucket from that object. For example, your resource might look like

resource "aws_s3_bucket_object" "bucket" {
  bucket = "${aws_s3_bucket.b.id}"
  key = "new_object_key"
  source = "/root/S3/accountdetails"
  etag = "${md5(file("/root/S3/accountdetails"))}"
}

@jbardin jbardin closed this as completed Mar 12, 2018
@Aliabbask08
Copy link

@jbardin thanks:)

@ghost
Copy link

ghost commented Jul 25, 2019

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@ghost ghost locked and limited conversation to collaborators Jul 25, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants