Answers: <p>If you already have your S3 bucket recognized by Terraform and it was created by the code you pasted, you schould not be having any diffculties if the operation could be done in place. </p>
  
  <p>There are a few important MUST KNOW things about how TF handle resources. MUST KNOW because in the end of the day it's infrastructure :) Some of the parameters are immutable, although it might not seem that intuitive changing the resource identifier would trigger delete/create scenario.</p>
  
  <p>Could you paste your TF plan output ? </p>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-23T21:22:30.377Z
Answers: 2020-03-23T21:22:30.377Z
Answers: Recoba20
Answers: <p>You'll have to modify the underlying module to make changes here. </p>
  
  <p>Be sure you're aware of the impact this will have on other modules using your <code>s3_website</code> module. You might want to start using <a href="https://www.terraform.io/docs/modules/sources.html#selecting-a-revision" rel="nofollow noreferrer">module source versioning</a>.</p>
  
Answers: CC BY-SA 4.0
Answers: 2020-03-22T16:49:09.927Z
Answers: 2020-03-22T16:49:09.927Z
Answers: thisguy123
Body: <p>I already have my S3 bucket called <code>foo-name</code> created and existing with terraform. I want to modify the existing S3 bucket and a policy. But when I do this, it seems to want to add a new s3 bucket instead. I imagine <code>s3_website</code> in <code>resource &quot;aws_s3_bucket&quot; &quot;s3_bucket&quot;</code> is incorrect. But I don't know what I would use in place of that because of the way the S3 bucket was created.</p>
  <p>How do I get it to modify my existing S3 bucket that was created in <code>module s3_website {}</code>?</p>
  <pre><code>provider &quot;aws&quot; {
    alias = &quot;east1&quot;
    region = &quot;us-east-1&quot;
  }
  
  module &quot;acm_cert&quot; {
    source = &quot;foo-git&quot;
  
    cluster = var.cluster
  
    domain      = var.website_domain
    dns_zone    = var.zone_name
  
    tags       = var.tags
    extra_tags = var.extra_tags
  
     providers = {
      aws = aws.east1
    }
  }
  
  module s3_website {
    source = &quot;foo-git&quot;
  
    cluster        = var.cluster
    env            = var.tags.Environment
    app            = var.tags.Application
    website_domain = var.website_domain
    s3_prefix      = var.s3_prefix
  
    certificate_arn = module.acm_cert.certificate_arn
    error_document = &quot;index.html&quot;
    index_document = &quot;index.html&quot;
  
    custom_error_response = [
      {
          &quot;error_code&quot;         = 404
          &quot;response_code&quot;      = 200
          &quot;response_page_path&quot; = &quot;/index.html&quot;
      },
      {
          &quot;error_code&quot;         = 403
          &quot;response_code&quot;      = 200
          &quot;response_page_path&quot; = &quot;/index.html&quot;
      },
    ]
  
    tags = var.tags
  }
  
  data &quot;aws_route53_zone&quot; &quot;base&quot; {
    name = var.zone_name
  }
  
  resource &quot;aws_route53_record&quot; &quot;this&quot; {
    zone_id = data.aws_route53_zone.base.zone_id
    name    = var.website_domain
    type    = &quot;A&quot;
  
    alias {
      name                   = module.s3_website.domain_name
      zone_id                = module.s3_website.zone_id
      evaluate_target_health = false
    }
  }
  
  resource &quot;aws_s3_bucket&quot; &quot;s3_website&quot; {
    bucket = &quot;foo-name&quot;
    policy = &lt;&lt;POLICY
  {
    &quot;Version&quot;: &quot;2012-10-17&quot;,
    &quot;Statement&quot;: [
      {
        &quot;Sid&quot;: &quot;PublicReadGetObject&quot;,
        &quot;Effect&quot;: &quot;Allow&quot;,
        &quot;Principal&quot;: &quot;*&quot;,
        &quot;Action&quot;: &quot;s3:GETObject&quot;,
        &quot;Resource&quot;: &quot;arn:aws:s3:::foo-name/*&quot;
      }
    ]
  }
  POLICY
  }
  </code></pre>
  
Tags: <amazon-web-services><terraform>
Title: Terraform - how to modify a existing S3 bucket that was created from module?
UserDisplayName: dman
ThreadText: # Terraform - how to modify a existing S3 bucket that was created from module?
  
  (Asked by: username_0 on 2020-03-22)
  
  I already have my S3 bucket called `foo-name` created and existing with terraform. I want to modify the existing S3 bucket and a policy. But when I do this, it seems to want to add a new s3 bucket instead. I imagine `s3_website` in `resource "aws_s3_bucket" "s3_bucket"` is incorrect. But I don't know what I would use in place of that because of the way the S3 bucket was created.
  
  How do I get it to modify my existing S3 bucket that was created in `module s3_website {}`?
  
  ```
  provider "aws" {
    alias = "east1"
    region = "us-east-1"
  }
  
  module "acm_cert" {
    source = "foo-git"
  
    cluster = var.cluster
  
    domain      = var.website_domain
    dns_zone    = var.zone_name
  
    tags       = var.tags
    extra_tags = var.extra_tags
  
     providers = {
      aws = aws.east1
    }
  }
  
  module s3_website {
    source = "foo-git"
  
    cluster        = var.cluster
    env            = var.tags.Environment
    app            = var.tags.Application
    website_domain = var.website_domain
    s3_prefix      = var.s3_prefix
  
    certificate_arn = module.acm_cert.certificate_arn
    error_document = "index.html"
    index_document = "index.html"
  
    custom_error_response = [
      {
          "error_code"         = 404
          "response_code"      = 200
          "response_page_path" = "/index.html"
      },
      {
          "error_code"         = 403
          "response_code"      = 200
          "response_page_path" = "/index.html"
      },
    ]
  
    tags = var.tags
  }
  
  data "aws_route53_zone" "base" {
    name = var.zone_name
  }
  
  resource "aws_route53_record" "this" {
    zone_id = data.aws_route53_zone.base.zone_id
    name    = var.website_domain
    type    = "A"
  
    alias {
      name                   = module.s3_website.domain_name
      zone_id                = module.s3_website.zone_id
      evaluate_target_health = false
    }
  }
  
  resource "aws_s3_bucket" "s3_website" {
    bucket = "foo-name"
    policy = <<POLICY
  {
    "Version": "2012-10-17",
    "Statement": [
      {
        "Sid": "PublicReadGetObject",
        "Effect": "Allow",
        "Principal": "*",
        "Action": "s3:GETObject",
        "Resource": "arn:aws:s3:::foo-name/*"
      }
    ]
  }
  POLICY
  }
  
  ```
  
  **Tags:** amazon-web-services, terraform
  
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2020-03-23. Score: 2
  
  If you already have your S3 bucket recognized by Terraform and it was created by the code you pasted, you schould not be having any diffculties if the operation could be done in place.
  
  There are a few important MUST KNOW things about how TF handle resources. MUST KNOW because in the end of the day it's infrastructure :) Some of the parameters are immutable, although it might not seem that intuitive changing the resource identifier would trigger delete/create scenario.
  
  Could you paste your TF plan output ?
  
  
  ---
  
  ### Answer by username_2 on 2020-03-22. Score: 1
  
  You'll have to modify the underlying module to make changes here.
  
  Be sure you're aware of the impact this will have on other modules using your `s3_website` module. You might want to start using [module source versioning](https://www.terraform.io/docs/modules/sources.html#selecting-a-revision).
  
  
  ---
  
  