Skip to content

Commit

Permalink
Update for S3 security changes
Browse files Browse the repository at this point in the history
  • Loading branch information
chrismusther committed Apr 24, 2023
1 parent 94cf56e commit 595f7ff
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 8 deletions.
4 changes: 2 additions & 2 deletions backend.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
terraform {
required_version = "~> 1.3.0"
required_version = ">= 1.3.0"

required_providers {
archive = {
Expand All @@ -11,7 +11,7 @@ terraform {
source = "hashicorp/aws"
# Ensure this is valid for modules added during the session.
# i.e. cloudposse/cloudfront-s3-cdn/aws
version = "~> 3.64.0"
version = "~> 4.18"
}

github = {
Expand Down
28 changes: 26 additions & 2 deletions modules/static_website/main.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
resource "aws_s3_bucket" "website" {
bucket = "website-${data.aws_region.current.name}-${data.aws_caller_identity.current.account_id}"
acl = "public-read"


website {
index_document = "index.html"
Expand All @@ -12,6 +10,32 @@ resource "aws_s3_bucket" "website" {
force_destroy = true
}

resource "aws_s3_bucket_ownership_controls" "website" {
bucket = aws_s3_bucket.website.id

rule {
object_ownership = "BucketOwnerPreferred"
}
}

resource "aws_s3_bucket_public_access_block" "website" {
bucket = aws_s3_bucket.website.id

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

resource "aws_s3_bucket_acl" "website" {
depends_on = [
aws_s3_bucket_ownership_controls.website,
aws_s3_bucket_public_access_block.website,
]
bucket = aws_s3_bucket.website.id
acl = "public-read"
}

resource "aws_s3_bucket_object" "index" {
bucket = aws_s3_bucket.website.bucket
acl = "public-read"
Expand Down
4 changes: 2 additions & 2 deletions providers.tf
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
provider "aws" {
region = "eu-west-1"

# This profile configuration will be overridden if using aws-vault, so it's
# safe to stay here.
# This profile configuration will need to be commented out if using aws-vault.
profile = "advanced-tf-training"

default_tags {
Expand Down Expand Up @@ -46,6 +45,7 @@ provider "aws" {
provider "aws" {
alias = "read_only"
region = "eu-west-1"
# This profile configuration will need to be commented out if using aws-vault.
profile = "advanced-tf-training"

assume_role {
Expand Down
28 changes: 26 additions & 2 deletions s3_website.tf
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
resource "aws_s3_bucket" "website" {
bucket = "website-${data.aws_region.current.name}-${data.aws_caller_identity.current.account_id}"
acl = "public-read"


website {
index_document = "index.html"
Expand All @@ -12,6 +10,32 @@ resource "aws_s3_bucket" "website" {
force_destroy = true
}

resource "aws_s3_bucket_ownership_controls" "website" {
bucket = aws_s3_bucket.website.id

rule {
object_ownership = "BucketOwnerPreferred"
}
}

resource "aws_s3_bucket_public_access_block" "website" {
bucket = aws_s3_bucket.website.id

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

resource "aws_s3_bucket_acl" "website" {
depends_on = [
aws_s3_bucket_ownership_controls.website,
aws_s3_bucket_public_access_block.website,
]
bucket = aws_s3_bucket.website.id
acl = "public-read"
}

resource "aws_s3_bucket_object" "index" {
bucket = aws_s3_bucket.website.bucket
acl = "public-read"
Expand Down

0 comments on commit 595f7ff

Please sign in to comment.