terraform-aws-arc-cloudfront
Overview
SourceFuse AWS Reference Architecture (ARC) Terraform module for managing Cloudfront, S3, Route53 and ACM.
Usage
To see a full example, check out the main.tf file in the example folder.
module "tags" {
source = "sourcefuse/arc-tags/aws"
version = "1.2.3"
environment = "dev"
project = "test"
extra_tags = {
RepoName = "terraform-aws-refarch-cloudfront"
}
}
module "cloudfront" {
source = "sourcefuse/arc-cloudfront/aws"
version = "4.0.1"
origins = [{
origin_type = "custom",
origin_id = "cloudfront-arc",
domain_name = "tst.wpengine.com",
bucket_name = "",
create_bucket = false,
custom_origin_config = {
http_port = 80
https_port = 443
origin_protocol_policy = "match-viewer"
origin_ssl_protocols = ["TLSv1"]
}
}
]
//source = "git::https://github.com/sourcefuse/terraform-aws-refarch-cloudfront?ref=2.0.2"
namespace = "test"
description = "This is a test Cloudfront distribution"
route53_root_domain = "sfrefarch.com" // Used to fetch the Hosted Zone
create_route53_records = var.create_route53_records
aliases = ["cf.sfrefarch.com", "www.cf.sfrefarch.com", "test.sfrefarch.com", "*.sfrefarch.com", "test1.sfrefarch.com"]
enable_logging = var.enable_logging // Create a new S3 bucket for storing Cloudfront logs
default_cache_behavior = {
origin_id = "cloudfront-arc",
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
compress = false
viewer_protocol_policy = "redirect-to-https"
use_aws_managed_cache_policy = true
cache_policy_name = "CachingOptimized"
use_aws_managed_origin_request_policy = true
origin_request_policy_name = "CORS-S3Origin" // It can be custom or aws managed policy name , if custom origin_request_policies variable key should match
lambda_function_association = [{
event_type = "viewer-request"
lambda_arn = aws_lambda_function.this.qualified_arn
include_body = true
}]
}
cache_behaviors = [
{
origin_id = "cloudfront-arc",
path_pattern = "/content/immutable/*"
allowed_methods = ["GET", "HEAD"]
cached_methods = ["GET", "HEAD"]
compress = false
viewer_protocol_policy = "redirect-to-https"
use_aws_managed_cache_policy = false
cache_policy_name = "cache-policy-1" // Note: This has to match cache_polices mentioned below
use_aws_managed_origin_request_policy = false
origin_request_policy_name = "origin-req-policy-1" // Note: This has to match origin_request_policies mentioned below
function_association = [
{
event_type = "viewer-request"
function_arn = aws_cloudfront_function.this.arn
}
]
}
]
viewer_certificate = {
cloudfront_default_certificate = false // false : It will create ACM certificate with details provided in acm_details
minimum_protocol_version = "TLSv1.2_2018"
ssl_support_method = "sni-only"
}
acm_details = {
domain_name = "*.sfrefarch.com",
subject_alternative_names = ["www.cf.sfrefarch.com"]
}
cache_policies = {
"cache-policy-1" = {
default_ttl = 86400,
max_ttl = 31536000,
min_ttl = 0,
cookies_config = {
cookie_behavior = "none",
items = []
},
headers_config = {
header_behavior = "whitelist",
items = ["Authorization", "Origin", "Accept", "Access-Control-Request-Method", "Access-Control-Request-Headers", "Referer"]
},
query_string_behavior = {
header_behavior = "none",
items = []
},
query_strings_config = {
query_string_behavior = "none",
items = []
}
} }
origin_request_policies = {
"origin-req-policy-1" = {
cookies_config = {
cookie_behavior = "none",
items = []
},
headers_config = {
header_behavior = "whitelist",
items = ["Accept", "Accept-Charset", "Accept-Datetime", "Accept-Language",
"Access-Control-Request-Method", "Access-Control-Request-Headers", "CloudFront-Forwarded-Proto", "CloudFront-Is-Android-Viewer",
"CloudFront-Is-Desktop-Viewer", "CloudFront-Is-IOS-Viewer"]
},
query_strings_config = {
query_string_behavior = "none",
items = []
}
} }
custom_error_responses = [{
error_caching_min_ttl = 10,
error_code = "404", // should be unique
response_code = "404",
response_page_path = "/custom_404.html"
}]
s3_kms_details = {
s3_bucket_encryption_type = "SSE-S3", //Encryption for S3 bucket , options : `SSE-S3` , `SSE-KMS`
kms_key_administrators = [],
kms_key_users = [], // Note :- Add users/roles who wanted to read/write to S3 bucket
kms_key_arn = null
}
tags = module.tags.tags
}
Requirements
Name | Version |
---|---|
terraform | >= 1.3.0, < 2.0.0 |
aws | ~> 4.0 |
Providers
Name | Version |
---|---|
aws | 4.67.0 |
aws.acm | 4.67.0 |
Modules
Name | Source | Version |
---|---|---|
kms | ./modules/kms | n/a |
s3_bucket | git::https://github.com/cloudposse/terraform-aws-s3-bucket | 3.1.2 |
s3_bucket_logs | git::https://github.com/cloudposse/terraform-aws-s3-bucket | 3.1.2 |
Resources
Name | Type |
---|---|
aws_acm_certificate.this | resource |
aws_acm_certificate_validation.this | resource |
aws_cloudfront_cache_policy.this | resource |
aws_cloudfront_distribution.this | resource |
aws_cloudfront_origin_access_control.s3 | resource |
aws_cloudfront_origin_request_policy.this | resource |
aws_cloudfront_response_headers_policy.this | resource |
aws_route53_record.root_domain | resource |
aws_route53_record.this | resource |
aws_s3_bucket_policy.cdn_bucket_policy | resource |
aws_caller_identity.this | data source |
aws_partition.this | data source |
aws_route53_zone.this | data source |
aws_s3_bucket.origin | data source |
Inputs
Name | Description | Type | Default | Required |
---|---|---|---|---|
acm_details | Details required for creating certificate eg. { domain_name = "test.com", subject_alternative_names = ["www.test.com"] } |
object({ |
{ |
no |
aliases | Fully qualified domain name for site being hosted | list(string) |
n/a | yes |
cache_behaviors | Set the cache behaviors for the distribution , Note:- You cannot use an origin request policy in a cache behavior without a cache policy. | list(object({ |
[] |
no |
cache_policies | Cache policies, eg. { "cache-policy-1" = { default_ttl = 86400, max_ttl = 31536000, min_ttl = 0, cookies_config = { cookie_behavior = "none", items = [] }, headers_config = { header_behavior = "whitelist", items = ["Authorization", "Origin", "Accept", "Access-Control-Request-Method", "Access-Control-Request-Headers", "Referer"] }, query_string_behavior = { header_behavior = "none", items = [] }, query_strings_config = { query_string_behavior = "none", items = [] } } } |
map(object( |
{} |
no |
cors_configuration | Specifies the allowed headers, methods, origins and exposed headers when using CORS on this bucket | list(object({ |
null |
no |
create_route53_records | made optional route53 | bool |
false |
no |
custom_error_responses | One or more custom error response elements | list(object({ |
[] |
no |
default_cache_behavior | Default cache behavior for the distribution | object({ |
n/a | yes |
default_root_object | Object that you want CloudFront to return (for example, index.html) when an end user requests the root URL. | string |
"index.html" |
no |
description | CloudFron destribution description | string |
n/a | yes |
enable_logging | Enable logging for Clouffront destribution, this will create new S3 bucket | bool |
false |
no |
geo_restriction | Geographic restriction | object({ |
{ |
no |
logging_bucket | S3 bucket used for storing logs | string |
null |
no |
namespace | Namespace for the resources. | string |
null |
no |
origin_request_policies | Origin request policies, eg. { "origin-req-policy" = { cookies_config = { cookie_behavior = "none", items = [] }, headers_config = { header_behavior = "whitelist", items = ["Accept", "Accept-Charset", "Accept-Datetime", "Accept-Language", "Access-Control-Request-Method", "Access-Control-Request-Headers", "CloudFront-Forwarded-Proto", "CloudFront-Is-Android-Viewer", "CloudFront-Is-Desktop-Viewer", "CloudFront-Is-IOS-Viewer"] }, query_strings_config = { query_string_behavior = "none", items = [] } } } |
map(object({ |
{} |
no |
origins | List of Origins for Cloudfront | list(object({ |
[] |
no |
price_class | Price class for this distribution. One of PriceClass_All, PriceClass_200, PriceClass_100. | string |
"PriceClass_All" |
no |
response_headers_policy | Header policies, eg. { "response-header-policy-1" = { default_ttl = 86400, max_ttl = 31536000, min_ttl = 0, cookies_config = { cookie_behavior = "none", items = [] }, headers_config = { header_behavior = "whitelist", items = ["Authorization", "Origin", "Accept", "Access-Control-Request-Method", "Access-Control-Request-Headers", "Referer"] }, query_string_behavior = { header_behavior = "none", items = [] }, query_strings_config = { query_string_behavior = "none", items = [] } } } |
map(object( |
{} |
no |
retain_on_delete | Disables the distribution instead of deleting it when destroying the resource through Terraform. If this is set, the distribution needs to be deleted manually afterwards. | bool |
false |
no |
route53_record_ttl | TTL for Route53 record | string |
60 |
no |
route53_root_domain | Domain to add to route 53 as alias to distribution | string |
n/a | yes |
s3_kms_details | KMS details for S3 encryption | object({ |
{ |
no |
tags | Tags for AWS resources | map(string) |
{} |
no |
viewer_certificate | The SSL configuration for this distribution | object({ |
{ |
no |
web_acl_id | Unique identifier that specifies the AWS WAF web ACL, if any, to associate with this distribution. To specify a web ACL created using the latest version of AWS WAF (WAFv2), use the ACL ARN, for example aws_wafv2_web_acl.example.arn. | string |
null |
no |
Outputs
Name | Description |
---|---|
acm_certificate_arn | Certificate ARN |
cloudfront_arn | CloudFront ARN |
cloudfront_domain_name | CloudFront Domain name |
cloudfront_hosted_zone_id | CloudFront Hosted zone ID |
cloudfront_id | CloudFront ID |
logging_s3_bucket | Logging bucket name |
origin_s3_bucket | Origin bucket name |
Development
Prerequisites
Configurations
- Configure pre-commit hooks
pre-commit install
Git commits
while Contributing or doing git commit please specify the breaking change in your commit message whether its major,minor or patch
For Example
git commit -m "your commit message #major"
By specifying this , it will bump the version and if you dont specify this in your commit message then by default it will consider patch and will bump that accordingly
Tests
- Tests are available in
test
directory - Configure the dependencies
cd test
go mod init github.com/sourcefuse/terraform-aws-refarch-cloudfront
go get github.com/gruntwork-io/terratest/modules/terraform
- Now execute the test
cd test/
go test
Authors
This project is authored by:
- SourceFuse ARC Team