_This issue was originally opened by @eric-aldinger as hashicorp/terraform#14747. It was migrated here as part of the provider split. The original body of the issue is below._
I want to be able to log to the same bucket I am managing as a resource in Terraform.
Terraform 0.9.4
Provider: AWS
AWS S3 bucket logging
resource "aws_s3_bucket" "binaries" {
bucket = "binaries"
acl = "${var.s3_acl_map.["public-read-write"]}"
region = "${var.aws_region}"
acceleration_status = "${var.s3_acceleration_map.["off"]}"
policy = "${data.aws_iam_policy_document.reporting_rti_drop.json}"
versioning {
enabled = false
}
logging {
target_bucket = "${self.id}"
target_prefix = "logs/binaries"
}
lifecycle_rule {
id = "logs"
prefix = "logs/"
enabled = true
transition {
days = 30
storage_class = "STANDARD_IA"
}
transition {
days = 60
storage_class = "GLACIER"
}
expiration {
days = 90
}
}
I can log to a prefix in the bucket I have imported or created.
I cannot keep my imported bucket logging
$ terraform import aws_s3_bucket.binaries binaries
...add tf config
$ terraform plan
1 error(s) occurred:
If I remove the logging section in the config it drops my logging
lifecycle_rule.0.transition.6450812.storage_class: "" => "GLACIER"
logging.#: "1" => "0"
logging.3282933444.target_bucket: "binaries" => ""
logging.3282933444.target_prefix: "logs/binaries" => ""
Any update on this issue?
Also having this issue in Terraform version v0.9.8. We have worked around this by specifying the bucket name in terraform.tfvars and referencing the bucket name in the terraform file which creates the bucket.
terraform.tfvars
my_bucket_name = "bucket_name"
create_bucket.tf
resource "aws_s3_bucket" "s3" {
bucket = "${var.my_bucket_name}"
logging {
target_bucket = "${var.my_bucket_name}"
target_prefix = "logs/binaries"
}
This also occurs in when the reference is in tags:
resource "aws_s3_bucket" "storage" {
bucket = "mybucket"
acl = "private"
versioning {
enabled = true
}
tags {
Name = "${self.id}"
Environment = "${var.env}"
Provisioner = "terraform"
}
}
Results in:
resource 'aws_s3_bucket.storage' config: cannot contain self-reference self.id
Hey! Anyone working on this issue?
Hello! Self referencing within a resource is not currently possible. The workaround @dsme94 mentioned could work as you are interpolating a variable that is already known (vs a self reference).
I confirmed that this is still the defined behavior for Terraform: https://www.terraform.io/docs/configuration-0-11/interpolation.html#attributes-of-your-own-resource
It will be the same for Terraform 0.12.
Thanks for reporting! For future questions you can use the community discussion forum.
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 feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!
Most helpful comment
Hey! Anyone working on this issue?