Create Public S3 Bucket Using Terraform – Unable to Create Bucket (aws:4566 Unreachable)

Hi All,

terraform i

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default ➜ cat main.tf
provider “aws” {
region = “us-east-1”
}
resource “aws_s3_bucket” “public_bucket” {
bucket = “xfusion-s3-9469”
}
resource “aws_s3_bucket_acl” “example” {
bucket = aws_s3_bucket.public_bucket.id
acl = “public-read”
}

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default ➜

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default ➜ terraform inint
Terraform has no command named “inint”. Did you mean “taint”?

To see all of Terraform’s top-level commands, run:
terraform -help

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default :heavy_multiplication_x: terraform init

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default :heavy_multiplication_x: terraform init
Initializing the backend…
Initializing provider plugins…

  • Finding latest version of hashicorp/aws…
  • Installing hashicorp/aws v6.28.0…
  • Installed hashicorp/aws v6.28.0 (signed by HashiCorp)
    Terraform has created a lock file .terraform.lock.hcl to record the provider
    selections it made above. Include this file in your version control repository
    so that Terraform can guarantee to make the same selections by default when
    you run “terraform init” in the future.

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running “terraform plan” to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default ➜ terraform fmt

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default ➜ terraform validate
Success! The configuration is valid.

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default ➜ terraform plan

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:

  • create

Terraform will perform the following actions:

aws_s3_bucket.public_bucket will be created

  • resource “aws_s3_bucket” “public_bucket” {
    • acceleration_status = (known after apply)

    • acl = (known after apply)

    • arn = (known after apply)

    • bucket = “xfusion-s3-9469”

    • bucket_domain_name = (known after apply)

    • bucket_prefix = (known after apply)

    • bucket_region = (known after apply)

    • bucket_regional_domain_name = (known after apply)

    • force_destroy = false

    • hosted_zone_id = (known after apply)

    • id = (known after apply)

    • object_lock_enabled = (known after apply)

    • policy = (known after apply)

    • region = “us-east-1”

    • request_payer = (known after apply)

    • tags_all = (known after apply)

    • website_domain = (known after apply)

    • website_endpoint = (known after apply)

    • cors_rule (known after apply)

    • grant (known after apply)

    • lifecycle_rule (known after apply)

    • logging (known after apply)

    • object_lock_configuration (known after apply)

    • replication_configuration (known after apply)

    • server_side_encryption_configuration (known after apply)

    • versioning (known after apply)

    • website (known after apply)
      }

aws_s3_bucket_acl.example will be created

  • resource “aws_s3_bucket_acl” “example” {
    • acl = “public-read”

    • bucket = (known after apply)

    • id = (known after apply)

    • region = “us-east-1”

    • access_control_policy (known after apply)
      }

Plan: 2 to add, 0 to change, 0 to destroy.

───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────

Note: You didn’t use the -out option to save this plan, so Terraform can’t guarantee to take exactly these actions if you run
“terraform apply” now.

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default ➜ terraform apply

Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the
following symbols:

  • create

Terraform will perform the following actions:

aws_s3_bucket.public_bucket will be created

  • resource “aws_s3_bucket” “public_bucket” {
    • acceleration_status = (known after apply)

    • acl = (known after apply)

    • arn = (known after apply)

    • bucket = “xfusion-s3-9469”

    • bucket_domain_name = (known after apply)

    • bucket_prefix = (known after apply)

    • bucket_region = (known after apply)

    • bucket_regional_domain_name = (known after apply)

    • force_destroy = false

    • hosted_zone_id = (known after apply)

    • id = (known after apply)

    • object_lock_enabled = (known after apply)

    • policy = (known after apply)

    • region = “us-east-1”

    • request_payer = (known after apply)

    • tags_all = (known after apply)

    • website_domain = (known after apply)

    • website_endpoint = (known after apply)

    • cors_rule (known after apply)

    • grant (known after apply)

    • lifecycle_rule (known after apply)

    • logging (known after apply)

    • object_lock_configuration (known after apply)

    • replication_configuration (known after apply)

    • server_side_encryption_configuration (known after apply)

    • versioning (known after apply)

    • website (known after apply)
      }

aws_s3_bucket_acl.example will be created

  • resource “aws_s3_bucket_acl” “example” {
    • acl = “public-read”

    • bucket = (known after apply)

    • id = (known after apply)

    • region = “us-east-1”

    • access_control_policy (known after apply)
      }

Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
Terraform will perform the actions described above.
Only ‘yes’ will be accepted to approve.

Enter a value: yes

aws_s3_bucket.public_bucket: Creating…

│ Error: creating S3 Bucket (xfusion-s3-9469): operation error S3: CreateBucket, https response error StatusCode: 0, RequestID: , HostID: , request send failed, Put “http://xfusion-s3-9469.aws:4566/”: dial tcp: lookup xfusion-s3-9469.aws on 10.96.0.10:53: no such host

│ with aws_s3_bucket.public_bucket,
│ on main.tf line 4, in resource “aws_s3_bucket” “public_bucket”:
│ 4: resource “aws_s3_bucket” “public_bucket” {

bob@iac-server ~/terraform via :diamond_shape_with_a_dot_inside: default :heavy_multiplication_x:

HI @sainathk

You need to add a ws_s3_bucket_public_access_block as explained here and then create an aws_s3_bucket_acl and refer to the public_access_block and your aws_s3-bucket in that using depends_on.

Something like this:

resource "aws_s3_bucket" "public_bucket" {
  bucket = "nautilus-s3-9469"

  tags = {
    Name        = "nautilus-s3-9469"
  }
}

resource "aws_s3_bucket_public_access_block" "bucket_public_access" {
  bucket = aws_s3_bucket.public_bucket.id

  block_public_acls       = false
  block_public_policy     = false
  ignore_public_acls      = false
  restrict_public_buckets = false
}

resource "aws_s3_bucket_acl" "public_bucket_acl" {
  depends_on = [
    aws_s3_bucket_public_access_block.bucket_public_access
  ]
  bucket = aws_s3_bucket.public_bucket.id
  acl    = "public-read"
}