-2

I already have a bucket in the AWS account, I just want to upload the code so that the terraform does not create additional resources and deletes the existing ones. Things like lifecycle don't help, terraform gives an error.

resource "aws_s3_bucket" "static_website" {
  for_each = var.env == "ololo" ? { "bucket" = var.bucket_name } : {}  

  bucket = each.value["bucket"]

  website {
    index_document = "index.html"
    error_document = "index.html"
  }
  lifecycle {
    ignore_changes = all
  }
}

resource "aws_s3_bucket_public_access_block" "static_website" {
  for_each = aws_s3_bucket.static_website

  bucket = each.value.id 

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}
variable "bucket_name" {
  type = string
  description = "Name of the bucket."
  default = ""
}
variable "env" {
  type        = string
  description = "Environment variable."
  default     = "dev"
}
highfive
  • 11
  • 2

1 Answers1

0

it would be useful if you add the error message too.

Not clear what is the problem. are the buckets already created and thus us what causes error? maybe you want to import them. You can do it by: terraform import aws_s3_bucket.static_website <arn>

As it is a for each you i think you eill have to oass the key to the resource beimg something like that:

`terraform import aws_s3_bucket.static_website["bucket_name"] <arn>`

obviosly replacing bucket$0_name by the actual bucket name.

Ref import aws_s3_bucket

This way you dont have to remove them before hand.

If not, please try to specify a little bit more and provide some erro info too.

Cheers