AWS CloudFront Distribution
This page shows how to write Terraform and CloudFormation for CloudFront Distribution and write them securely.
aws_cloudfront_distribution (Terraform)
The Distribution in CloudFront can be configured in Terraform with the resource name aws_cloudfront_distribution. The following sections describe 5 examples of how to use the resource and its parameters.
Example Usage from GitHub
resource "aws_cloudfront_distribution" "lib-haxe-org" {
aliases = ["lib.haxe.org"]
enabled = true
is_ipv6_enabled = true
price_class = "PriceClass_100"
resource "aws_cloudfront_distribution" "default" {
viewer_certificate {
cloudfront_default_certificate = true
}
}
resource "aws_cloudfront_distribution" "positive1" {
origin {
domain_name = aws_s3_bucket.b.bucket_regional_domain_name
origin_id = local.s3_origin_id
s3_origin_config {
resource "aws_cloudfront_distribution" "pass" {
enabled = true
default_cache_behavior {
response_headers_policy_id = aws_cloudfront_response_headers_policy.pass.id
}
resource "aws_cloudfront_distribution" "allowed" {
web_acl_id = "arn:1234"
logging_config {}
}
resource "aws_cloudfront_distribution" "denied" {
Security Best Practices for aws_cloudfront_distribution
There are 3 settings in aws_cloudfront_distribution that should be taken care of for security reasons. The following section explain an overview and example code.
Ensure to enable access logging of CloudFront distribution
To avoid attacks, it is better to configure access logging of a CloudFront distribution. The logs are important for the early-stage detection of attacks and incident responses. It is better to enable the feature while being careful of handling cookies.
Ensure to use modern TLS protocols
A CloudFront distribution uses outdated TLS protocols. It's better to adopt TLS v1.2+.
Ensure to use HTTPS as much as possible
To avoid attacks, it is better to use HTTPS as much as possible. CloudFront is available through HTTP, which is vulnerable to Meet-in-the-Middle (MITM) attacks.
Parameters
-
aliasesoptional - set of string -
arnoptional computed - string -
caller_referenceoptional computed - string -
commentoptional - string -
default_root_objectoptional - string -
domain_nameoptional computed - string -
enabledrequired - bool -
etagoptional computed - string -
hosted_zone_idoptional computed - string -
http_versionoptional - string -
idoptional computed - string -
in_progress_validation_batchesoptional computed - number -
is_ipv6_enabledoptional - bool -
last_modified_timeoptional computed - string -
price_classoptional - string -
retain_on_deleteoptional - bool -
statusoptional computed - string -
tagsoptional - map from string to string -
trusted_key_groupsoptional computed - list of object-
enabled- bool -
items- list of object-
key_group_id- string -
key_pair_ids- set of string
-
-
-
trusted_signersoptional computed - list of object-
enabled- bool -
items- list of object-
aws_account_number- string -
key_pair_ids- set of string
-
-
-
wait_for_deploymentoptional - bool -
web_acl_idoptional - string -
custom_error_responseset block-
error_caching_min_ttloptional - number -
error_coderequired - number -
response_codeoptional - number -
response_page_pathoptional - string
-
-
default_cache_behaviorlist block-
allowed_methodsrequired - set of string -
cache_policy_idoptional - string -
cached_methodsrequired - set of string -
compressoptional - bool -
default_ttloptional computed - number -
field_level_encryption_idoptional - string -
max_ttloptional computed - number -
min_ttloptional - number -
origin_request_policy_idoptional - string -
realtime_log_config_arnoptional - string -
smooth_streamingoptional - bool -
target_origin_idrequired - string -
trusted_key_groupsoptional computed - list of string -
trusted_signersoptional computed - list of string -
viewer_protocol_policyrequired - string -
forwarded_valueslist block-
headersoptional computed - set of string -
query_stringrequired - bool -
query_string_cache_keysoptional computed - list of string -
cookieslist block-
forwardrequired - string -
whitelisted_namesoptional computed - set of string
-
-
-
lambda_function_associationset block-
event_typerequired - string -
include_bodyoptional - bool -
lambda_arnrequired - string
-
-
-
logging_configlist block-
bucketrequired - string -
include_cookiesoptional - bool -
prefixoptional - string
-
-
ordered_cache_behaviorlist block-
allowed_methodsrequired - set of string -
cache_policy_idoptional - string -
cached_methodsrequired - set of string -
compressoptional - bool -
default_ttloptional computed - number -
field_level_encryption_idoptional - string -
max_ttloptional computed - number -
min_ttloptional - number -
origin_request_policy_idoptional - string -
path_patternrequired - string -
realtime_log_config_arnoptional - string -
smooth_streamingoptional - bool -
target_origin_idrequired - string -
trusted_key_groupsoptional - list of string -
trusted_signersoptional - list of string -
viewer_protocol_policyrequired - string -
forwarded_valueslist block-
headersoptional computed - set of string -
query_stringrequired - bool -
query_string_cache_keysoptional computed - list of string -
cookieslist block-
forwardrequired - string -
whitelisted_namesoptional - set of string
-
-
-
lambda_function_associationset block-
event_typerequired - string -
include_bodyoptional - bool -
lambda_arnrequired - string
-
-
-
originset block-
domain_namerequired - string -
origin_idrequired - string -
origin_pathoptional - string -
custom_headerset block -
custom_origin_configlist block-
http_portrequired - number -
https_portrequired - number -
origin_keepalive_timeoutoptional - number -
origin_protocol_policyrequired - string -
origin_read_timeoutoptional - number -
origin_ssl_protocolsrequired - set of string
-
-
s3_origin_configlist block-
origin_access_identityrequired - string
-
-
-
origin_groupset block-
origin_idrequired - string -
failover_criterialist block-
status_codesrequired - set of number
-
-
memberlist block-
origin_idrequired - string
-
-
-
restrictionslist block-
geo_restrictionlist block-
locationsoptional computed - set of string -
restriction_typerequired - string
-
-
-
viewer_certificatelist block-
acm_certificate_arnoptional - string -
cloudfront_default_certificateoptional - bool -
iam_certificate_idoptional - string -
minimum_protocol_versionoptional - string -
ssl_support_methodoptional - string
-
Explanation in Terraform Registry
Creates an Amazon CloudFront web distribution. For information about CloudFront distributions, see the [Amazon CloudFront Developer Guide][1]. For specific information about creating CloudFront web distributions, see the [POST Distribution][2] page in the Amazon CloudFront API Reference.
NOTE: CloudFront distributions take about 15 minutes to a deployed state after creation or modification. During this time, deletes to resources will be blocked. If you need to delete a distribution that is enabled and you do not want to wait, you need to use the
retain_on_deleteflag.
AWS::CloudFront::Distribution (CloudFormation)
The Distribution in CloudFront can be configured in CloudFormation with the resource name AWS::CloudFront::Distribution. The following sections describe how to use the resource and its parameters.
Example Usage from GitHub
An example could not be found in GitHub.
Parameters
-
DistributionConfigrequired - DistributionConfig -
Tagsoptional - List of Tag
Explanation in CloudFormation Registry
A distribution tells CloudFront where you want content to be delivered from, and the details about how to track and manage content delivery.
Frequently asked questions
What is AWS CloudFront Distribution?
AWS CloudFront Distribution is a resource for CloudFront of Amazon Web Service. Settings can be wrote in Terraform and CloudFormation.
Where can I find the example code for the AWS CloudFront Distribution?
For Terraform, the HaxeFoundation/haxe-terraform, snyk-labs/infrastructure-as-code-goof and Checkmarx/kics source code examples are useful. See the Terraform Example section for further details.