-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #95 from sixfeetup/nvernooy/67-cloudfront
Add cloudfront CDN
- Loading branch information
Showing
9 changed files
with
179 additions
and
14 deletions.
There are no files selected for viewing
21 changes: 21 additions & 0 deletions
21
{{cookiecutter.project_slug}}/terraform/modules/application/acm.tf
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
resource "aws_acm_certificate" "cert" { | ||
domain_name = var.domain | ||
validation_method = "DNS" | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "aws_route53_record" "cert" { | ||
name = tolist(aws_acm_certificate.cert.domain_validation_options)[0].resource_record_name | ||
type = tolist(aws_acm_certificate.cert.domain_validation_options)[0].resource_record_type | ||
zone_id = data.aws_route53_zone.route_zone.id | ||
records = [tolist(aws_acm_certificate.cert.domain_validation_options)[0].resource_record_value] | ||
ttl = 300 | ||
} | ||
|
||
resource "aws_acm_certificate_validation" "cert" { | ||
certificate_arn = aws_acm_certificate.cert.arn | ||
validation_record_fqdns = aws_route53_record.cert.*.fqdn | ||
} |
69 changes: 69 additions & 0 deletions
69
{{cookiecutter.project_slug}}/terraform/modules/application/cloudfront.tf
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,72 @@ | ||
resource "aws_cloudfront_origin_access_identity" "static_storage" { | ||
comment = "${var.application}-${var.environment}" | ||
} | ||
|
||
resource "aws_cloudfront_distribution" "ec2_cluster" { | ||
enabled = true | ||
aliases = [var.domain_zone] | ||
is_ipv6_enabled = true | ||
price_class = "PriceClass_100" | ||
default_root_object = "" | ||
|
||
origin { | ||
domain_name = var.cluster_domain | ||
origin_id = var.cluster_id | ||
|
||
custom_origin_config { | ||
http_port = 80 | ||
https_port = 443 | ||
origin_protocol_policy = "match-viewer" | ||
origin_ssl_protocols = ["TLSv1.1", "TLSv1.2"] | ||
} | ||
} | ||
|
||
default_cache_behavior { | ||
# Using the CachingOptimized policy: | ||
cache_policy_id = data.aws_cloudfront_cache_policy.caching_optimized.id | ||
# Using the AllViewerExceptHostHeader origin policy | ||
origin_request_policy_id = data.aws_cloudfront_origin_request_policy.all_viewer_except_host.id | ||
|
||
allowed_methods = ["DELETE", "GET", "HEAD", "OPTIONS", "PATCH", "POST", "PUT"] | ||
cached_methods = ["GET", "HEAD"] | ||
viewer_protocol_policy = "redirect-to-https" | ||
target_origin_id = var.cluster_id | ||
} | ||
|
||
viewer_certificate { | ||
ssl_support_method = "sni-only" | ||
acm_certificate_arn = aws_acm_certificate.cert.arn | ||
minimum_protocol_version = "TLSv1.2_2018" | ||
} | ||
|
||
restrictions { | ||
geo_restriction { | ||
restriction_type = "none" | ||
} | ||
} | ||
|
||
tags = local.common_tags | ||
} | ||
|
||
resource "aws_iam_user" "cloudfront_invalidator" { | ||
name = "${var.environment}-cloudfront-invalidator" | ||
} | ||
|
||
resource "aws_iam_user_policy" "cloudfront_invalidator" { | ||
name = "${var.environment}-cloudfront-invalidator" | ||
user = aws_iam_user.cloudfront_invalidator.name | ||
policy = data.aws_iam_policy_document.cloudfront_invalidator.json | ||
} | ||
|
||
data "aws_iam_policy_document" "cloudfront_invalidator" { | ||
statement { | ||
sid = "CloudfrontInvalidation" | ||
actions = ["cloudfront:CreateInvalidation"] | ||
effect = "Allow" | ||
resources = [var.cluster_arn] | ||
} | ||
} | ||
|
||
resource "aws_iam_access_key" "cloudfront_invalidator" { | ||
user = aws_iam_user.cloudfront_invalidator.name | ||
} |
7 changes: 7 additions & 0 deletions
7
{{cookiecutter.project_slug}}/terraform/modules/application/data.tf
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
data "aws_cloudfront_cache_policy" "caching_optimized" { | ||
name = "Managed-CachingOptimized" | ||
} | ||
|
||
data "aws_cloudfront_origin_request_policy" "all_viewer_except_host" { | ||
name = "Managed-AllViewerExceptHostHeader" | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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