Last active
March 21, 2021 19:51
-
-
Save refayathaque/b520a9ffa9a8b4d8f0eacdcd85aa5b64 to your computer and use it in GitHub Desktop.
Part of "Automate hosting a static website on AWS with Terraform" post
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_s3_bucket" "website_static_files" { | |
bucket = var.BUCKET_NAME | |
acl = "private" | |
policy = data.aws_iam_policy_document.bucket_website_hosting.json | |
website { | |
index_document = "index.html" | |
} | |
force_destroy = true | |
} | |
data "aws_iam_policy_document" "bucket_website_hosting" { | |
statement { | |
actions = [ | |
"s3:GetObject", | |
"s3:ListBucket", | |
] | |
principals { | |
identifiers = [aws_cloudfront_origin_access_identity.origin_access_identity.iam_arn] | |
type = "AWS" | |
} | |
resources = [ | |
"arn:aws:s3:::${var.BUCKET_NAME}", | |
"arn:aws:s3:::${var.BUCKET_NAME}/*", | |
] | |
} | |
} | |
resource "null_resource" "upload_static_files" { | |
provisioner "local-exec" { | |
command = "aws s3 cp index.html s3://${var.BUCKET_NAME}" | |
} | |
depends_on = [aws_s3_bucket.website_static_files] | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment