Skip to content

Commit

Permalink
add terraform S3&Cloudfront distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
Hessu committed Feb 6, 2020
1 parent 980f10b commit 0ca86f3
Show file tree
Hide file tree
Showing 4 changed files with 151 additions and 0 deletions.
63 changes: 63 additions & 0 deletions terraform/cloudfront/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
variable "website_endpoint" {
type = string
}
variable "bucket" {
type = string
}

resource "aws_cloudfront_distribution" "d" {
origin {
domain_name = var.website_endpoint
origin_id = "S3-${var.bucket}"
wait_for_deployment = false

custom_origin_config {
http_port = 80
https_port = 443
origin_protocol_policy = "match-viewer"
origin_ssl_protocols = ["TLSv1", "TLSv1.1", "TLSv1.2"]
}
}
default_root_object = "index.html"
enabled = true
is_ipv6_enabled = true

custom_error_response {
error_caching_min_ttl = 3000
error_code = 404
response_code = 200
response_page_path = "/404.html"
}

default_cache_behavior {
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"]
cached_methods = ["GET", "HEAD"]
target_origin_id = "S3-${var.bucket}"

forwarded_values {
query_string = true
cookies {
forward = "none"
}
}

viewer_protocol_policy = "allow-all"
min_ttl = 0
default_ttl = 3600
max_ttl = 86400


}

price_class = "PriceClass_100"

restrictions {
geo_restriction {
restriction_type = "none"
}
}

viewer_certificate {
cloudfront_default_certificate = true
}
}
49 changes: 49 additions & 0 deletions terraform/s3/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
variable "s3bucket" {
type = string
}

resource "aws_s3_bucket" "b" {
bucket = var.s3bucket
acl = "public-read"

cors_rule {
allowed_headers = ["*"]
allowed_methods = ["PUT", "POST"]
allowed_origins = ["*"]
expose_headers = ["ETag"]
max_age_seconds = 3000
}

website {
index_document = "index.html"
error_document = "404.html"
}

policy = <<EOF
{
"Version": "2008-10-17",
"Statement": [
{
"Sid": "PublicReadForGetBucketObjects",
"Effect": "Allow",
"Principal": {
"AWS": "*"
},
"Action": "s3:GetObject",
"Resource": "arn:aws:s3:::${var.s3bucket}/*"
}]
}
EOF
}

output "bucket" {
value = aws_s3_bucket.b.bucket
}

output "website_endpoint" {
value = aws_s3_bucket.b.website_endpoint
}

output "bucket_id" {
value = aws_s3_bucket.b.id
}
22 changes: 22 additions & 0 deletions terraform/s3/policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"Version": "2008-10-17",
"Id": "PolicyForCloudFrontPrivateContent",
"Statement": [
{
"Sid": "1",
"Effect": "Allow",
"Principal": {
"AWS": "arn:aws:iam::cloudfront:user/CloudFront Origin Access Identity EFNACULIZQCT0"
},
"Action": "s3:GetObject",
"Resource": "${aws_s3_bucket.b.arn}/*"
},
{
"Sid": "PublicReadGetObject",
"Effect": "Allow",
"Principal": "*",
"Action": "s3:GetObject",
"Resource": "${aws_s3_bucket.b.arn}/*"
}
]
}
17 changes: 17 additions & 0 deletions terraform/terraform.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
provider "aws" {
region = "eu-west-1"
}
variable s3bucket {
description = "Blog S3 Bucket"
default = "blog.test.gonzague.pagin"
}
module "s3" {
source = "./s3"
s3bucket = var.s3bucket
}
module "cloudfront" {
source = "./cloudfront"

website_endpoint = "${module.s3.website_endpoint}"
bucket = "${module.s3.bucket}"
}

0 comments on commit 0ca86f3

Please sign in to comment.