Component: spa-s3-cloudfront
This component is responsible for provisioning:
- S3 bucket
- CloudFront distribution for a Single Page Application
- ACM placed in us-east-1 regardless of the stack region (requirement of CloudFront)
NOTE: The component does not use the ACM created by dns-delegated
, because the ACM region has to be us-east-1.
Usage
Stack Level: Regional
Here are some example snippets for how to use this component:
An import for all instantiations of the spa-s3-cloudfront
component can be created at stacks/spa/spa-defaults.yaml
:
components:
terraform:
spa-s3-cloudfront:
vars:
# lookup GitHub Runner IAM role via remote state
github_runners_deployment_principal_arn_enabled: true
github_runners_component_name: github-runners
github_runners_tenant_name: core
github_runners_environment_name: ue2
github_runners_stage_name : auto
origin_force_destroy: false
origin_versioning_enabled: true
origin_block_public_acls: true
origin_block_public_policy: true
origin_ignore_public_acls: true
origin_restrict_public_buckets: true
origin_encryption_enabled: true
cloudfront_index_document: index.html
cloudfront_ipv6_enabled: false
cloudfront_compress: true
cloudfront_default_root_object: index.html
cloudfront_viewer_protocol_policy: redirect-to-https
An import for all instantiations for a specific SPA can be created at stacks/spa/example-spa.yaml
:
components:
terraform:
example-spa:
component: spa-s3-cloudfront
vars:
name: example-spa
site_subdomain: example-spa
cloudfront_allowed_methods:
- GET
- HEAD
cloudfront_cached_methods:
- GET
- HEAD
cloudfront_custom_error_response:
- error_caching_min_ttl: 1
error_code: 403
response_code: 200
response_page_path: /index.html
cloudfront_default_ttl: 60
cloudfront_min_ttl: 60
cloudfront_max_ttl: 60
Finally, the spa-s3-cloudfront
component can be instantiated in a stack config:
import:
- spa/example-spa
components:
terraform:
example-spa:
component: spa-s3-cloudfront
settings:
spacelift:
workspace_enabled: true
vars: {}
Failover Origins
Failover origins are supported via var.failover_s3_origin_name
and var.failover_s3_origin_region
.
Preview Environments
SPA Preview environments (i.e. subdomain.example.com
mapping to a /subdomain
path in the S3 bucket) powered by Lambda@Edge
are supported via var.preview_environment_enabled
. See the both the variable description and inline documentation for
an extensive explanation for how these preview environments work.
Requirements
Name | Version |
---|---|
terraform | >= 1.0.0 |
aws | >= 4.9.0 |
Providers
Name | Version |
---|---|
aws | >= 4.9.0 |
aws.failover | >= 4.9.0 |
Modules
Name | Source | Version |
---|---|---|
acm_request_certificate | cloudposse/acm-request-certificate/aws | 0.16.3 |
dns_delegated | cloudposse/stack-config/yaml//modules/remote-state | 1.5.0 |
gha_assume_role | ../account-map/modules/team-assume-role-policy | n/a |
gha_role_name | cloudposse/label/null | 0.25.0 |
github_runners | cloudposse/stack-config/yaml//modules/remote-state | 1.5.0 |
iam_roles | ../account-map/modules/iam-roles | n/a |
lambda_edge_preview | ./modules/lambda-edge-preview | n/a |
lambda_edge_redirect_404 | ./modules/lambda_edge_redirect_404 | n/a |
spa_web | cloudposse/cloudfront-s3-cdn/aws | 0.92.0 |
this | cloudposse/label/null | 0.25.0 |
utils | cloudposse/utils/aws | 1.3.0 |
waf | cloudposse/stack-config/yaml//modules/remote-state | 1.5.0 |
Resources
Name | Type |
---|---|
aws_iam_role.github_actions | resource |
aws_shield_protection.shield_protection | resource |
aws_iam_policy_document.github_actions_iam_policy | data source |
aws_s3_bucket.failover_bucket | data source |
Inputs
Name | Description | Type | Default | Required |
---|---|---|---|---|
additional_tag_map | Additional key-value pairs to add to each map in tags_as_list_of_maps . Not added to tags or id .This is for some rare cases where resources want additional configuration of tags and therefore take a list of maps with tag key, value, and additional configuration. | map(string) | {} | no |
attributes | ID element. Additional attributes (e.g. workers or cluster ) to add to id ,in the order they appear in the list. New attributes are appended to the end of the list. The elements of the list are joined by the delimiter and treated as a single ID element. | list(string) | [] | no |
block_origin_public_access_enabled | When set to 'true' the s3 origin bucket will have public access block enabled. | bool | true | no |
cloudfront_access_log_bucket_name | When cloudfront_access_log_create_bucket is false , this is the name of the existing S3 Bucket whereCloudFront Access Logs are to be delivered and is required. IGNORED when cloudfront_access_log_create_bucket is true . | string | "" | no |
cloudfront_access_log_bucket_name_rendering_enabled | If set to true , then the CloudFront origin access logs bucket name will be rendered by calling format("%v-%v-%v-%v", var.namespace, var.environment, var.stage, var.cloudfront_access_log_bucket_name) .Otherwise, the value for cloudfront_access_log_bucket_name will need to be the globally unique name of the access logs bucket.For example, if this component produces an origin bucket named eg-ue1-devplatform-example and cloudfront_access_log_bucket_name is set toexample-cloudfront-access-logs , then the bucket name will be rendered to be eg-ue1-devplatform-example-cloudfront-access-logs . | bool | false | no |
cloudfront_access_log_create_bucket | When true and cloudfront_access_logging_enabled is also true, this module will create a new,separate S3 bucket to receive CloudFront Access Logs. | bool | true | no |
cloudfront_access_log_prefix | Prefix to use for CloudFront Access Log object keys. Defaults to no prefix. | string | "" | no |
cloudfront_access_log_prefix_rendering_enabled | Whether or not to dynamically render ${module.this.id} at the end of var.cloudfront_access_log_prefix . | bool | false | no |
cloudfront_allowed_methods | List of allowed methods (e.g. GET, PUT, POST, DELETE, HEAD) for AWS CloudFront. | list(string) |
| no |
cloudfront_aws_shield_protection_enabled | Enable or disable AWS Shield Advanced protection for the CloudFront distribution. If set to 'true', a subscription to AWS Shield Advanced must exist in this account. | bool | false | no |
cloudfront_aws_waf_component_name | The name of the component used when deploying WAF ACL | string | "waf" | no |
cloudfront_aws_waf_environment | The environment where the WAF ACL for CloudFront distribution exists. | string | null | no |
cloudfront_aws_waf_protection_enabled | Enable or disable AWS WAF for the CloudFront distribution. This assumes that the aws-waf-acl-default-cloudfront component has been deployed to the regional stack correspondingto var.waf_acl_environment . | bool | true | no |
cloudfront_cached_methods | List of cached methods (e.g. GET, PUT, POST, DELETE, HEAD). | list(string) |
| no |
cloudfront_compress | Compress content for web requests that include Accept-Encoding: gzip in the request header. | bool | false | no |
cloudfront_custom_error_response | List of one or more custom error response element maps. |
| [] | no |
cloudfront_default_root_object | Object that CloudFront return when requests the root URL. | string | "index.html" | no |
cloudfront_default_ttl | Default amount of time (in seconds) that an object is in a CloudFront cache. | number | 60 | no |
cloudfront_index_document | Amazon S3 returns this index document when requests are made to the root domain or any of the subfolders. | string | "index.html" | no |
cloudfront_ipv6_enabled | Set to true to enable an AAAA DNS record to be set as well as the A record. | bool | true | no |
cloudfront_lambda_function_association | A config block that configures the CloudFront distribution with lambda@edge functions for specific events. |
| [] | no |
cloudfront_max_ttl | Maximum amount of time (in seconds) that an object is in a CloudFront cache. | number | 31536000 | no |
cloudfront_min_ttl | Minimum amount of time that you want objects to stay in CloudFront caches. | number | 0 | no |
cloudfront_viewer_protocol_policy | Limit the protocol users can use to access content. One of allow-all , https-only , or redirect-to-https . | string | "redirect-to-https" | no |
context | Single object for setting entire context at once. See description of individual variables for details. Leave string and numeric variables as null to use default value.Individual variable settings (non-null) override settings in context object, except for attributes, tags, and additional_tag_map, which are merged. | any |
| no |
custom_origins | A list of additional custom website origins for this distribution. |
| [] | no |
delimiter | Delimiter to be used between ID elements. Defaults to - (hyphen). Set to "" to use no delimiter at all. | string | null | no |
descriptor_formats | Describe additional descriptors to be output in the descriptors output map.Map of maps. Keys are names of descriptors. Values are maps of the form {<br/> format = string<br/> labels = list(string)<br/>} (Type is any so the map values can later be enhanced to provide additional options.)format is a Terraform format string to be passed to the format() function.labels is a list of labels, in order, to pass to format() function.Label values will be normalized before being passed to format() so they will beidentical to how they appear in id .Default is {} (descriptors output will be empty). | any | {} | no |
dns_delegated_environment_name | The environment where dns-delegated component is deployed to | string | "gbl" | no |
enabled | Set to false to prevent the module from creating any resources | bool | null | no |
environment | ID element. Usually used for region e.g. 'uw2', 'us-west-2', OR role 'prod', 'staging', 'dev', 'UAT' | string | null | no |
external_aliases | List of FQDN's - Used to set the Alternate Domain Names (CNAMEs) setting on CloudFront. No new Route53 records will be created for these. Setting process_domain_validation_options to true may cause the component to fail if an external_alias DNS zone is not controlled by Terraform.Setting preview_environment_enabled to true will cause this variable to be ignored. | list(string) | [] | no |
failover_criteria_status_codes | List of HTTP Status Codes to use as the origin group failover criteria. | list(string) |
| no |
failover_s3_origin_environment | The fixed name of the AWS Region where the failover S3 origin exists. Setting this variable will enable use of a failover S3 origin, but it is required for the failover S3 origin to exist beforehand. This variable is used in conjunction with var.failover_s3_origin_format tobuild out the name of the Failover S3 origin in the specified region. For example, if this component creates an origin of name eg-ue1-devplatform-example and this variable is set to uw1 ,then it is expected that a bucket with the name eg-uw1-devplatform-example-failover exists in us-west-1 . | string | null | no |
failover_s3_origin_format | If var.failover_s3_origin_environment is supplied, this is the format to use for the failover S3 origin bucket name whenbuilding the name via format([format], var.namespace, var.failover_s3_origin_environment, var.stage, var.name) and then looking it up via the aws_s3_bucket Data Source.For example, if this component creates an origin of name eg-ue1-devplatform-example and var.failover_s3_origin_environment is set to uw1 , then it is expected that a bucket with the name eg-uw1-devplatform-example-failover exists in us-west-1 . | string | "%v-%v-%v-%v-failover" | no |
forward_cookies | Specifies whether you want CloudFront to forward all or no cookies to the origin. Can be 'all' or 'none' | string | "none" | no |
forward_header_values | A list of whitelisted header values to forward to the origin (incompatible with cache_policy_id ) | list(string) |
| no |
github_actions_allowed_repos | A list of the GitHub repositories that are allowed to assume this role from GitHub Actions. For example, ["cloudposse/infra-live"]. Can contain "*" as wildcard. If org part of repo name is omitted, "cloudposse" will be assumed. | list(string) | [] | no |
github_actions_iam_role_attributes | Additional attributes to add to the role name | list(string) | [] | no |
github_actions_iam_role_enabled | Flag to toggle creation of an IAM Role that GitHub Actions can assume to access AWS resources | bool | false | no |
github_runners_component_name | The name of the component that deploys GitHub Runners, used in remote-state lookup | string | "github-runners" | no |
github_runners_deployment_principal_arn_enabled | A flag that is used to decide whether or not to include the GitHub Runner's IAM role in origin_deployment_principal_arns list | bool | true | no |
github_runners_environment_name | The name of the environment where the CloudTrail bucket is provisioned | string | "ue2" | no |
github_runners_stage_name | The stage name where the CloudTrail bucket is provisioned | string | "auto" | no |
github_runners_tenant_name | The tenant name where the GitHub Runners are provisioned | string | null | no |
id_length_limit | Limit id to this many characters (minimum 6).Set to 0 for unlimited length.Set to null for keep the existing setting, which defaults to 0 .Does not affect id_full . | number | null | no |
label_key_case | Controls the letter case of the tags keys (label names) for tags generated by this module.Does not affect keys of tags passed in via the tags input.Possible values: lower , title , upper .Default value: title . | string | null | no |
label_order | The order in which the labels (ID elements) appear in the id .Defaults to ["namespace", "environment", "stage", "name", "attributes"]. You can omit any of the 6 labels ("tenant" is the 6th), but at least one must be present. | list(string) | null | no |
label_value_case | Controls the letter case of ID elements (labels) as included in id ,set as tag values, and output by this module individually. Does not affect values of tags passed in via the tags input.Possible values: lower , title , upper and none (no transformation).Set this to title and set delimiter to "" to yield Pascal Case IDs.Default value: lower . | string | null | no |
labels_as_tags | Set of labels (ID elements) to include as tags in the tags output.Default is to include all labels. Tags with empty values will not be included in the tags output.Set to [] to suppress all generated tags.Notes: The value of the name tag, if included, will be the id , not the name .Unlike other null-label inputs, the initial setting of labels_as_tags cannot bechanged in later chained modules. Attempts to change it will be silently ignored. | set(string) |
| no |
lambda_edge_redirect_404_enabled | Enable or disable SPA 404 redirects via Lambda@Edge - returns a 302 and a location of / if the request returned 404. | bool | false | no |
name | ID element. Usually the component or solution name, e.g. 'app' or 'jenkins'. This is the only ID element not also included as a tag .The "name" tag is set to the full id string. There is no tag with the value of the name input. | string | null | no |
namespace | ID element. Usually an abbreviation of your organization name, e.g. 'eg' or 'cp', to help ensure generated IDs are globally unique | string | null | no |
ordered_cache | An ordered list of cache behaviors resource for this distribution. List in order of precedence (first match wins). This is in addition to the default cache policy. Set target_origin_id to "" to specify the S3 bucket origin created by this module. |
| [] | no |
origin_allow_ssl_requests_only | Set to true in order to have the origin bucket require requests to use Secure Socket Layer (HTTPS/SSL). This will explicitly deny access to HTTP requests | bool | true | no |
origin_deployment_actions | List of actions to permit origin_deployment_principal_arns to perform on bucket and bucket prefixes (see origin_deployment_principal_arns ) | list(string) |
| no |
origin_deployment_principal_arns | List of role ARNs to grant deployment permissions to the origin Bucket. | list(string) | [] | no |
origin_encryption_enabled | When set to 'true' the origin Bucket will have aes256 encryption enabled by default. | bool | true | no |
origin_force_destroy | A boolean string that indicates all objects should be deleted from the origin Bucket so that the Bucket can be destroyed without error. These objects are not recoverable. | bool | false | no |
origin_s3_access_log_bucket_name | Name of the existing S3 bucket where S3 Access Logs for the origin Bucket will be delivered. Default is not to enable S3 Access Logging for the origin Bucket. | string | "" | no |
origin_s3_access_log_bucket_name_rendering_enabled | If set to true , then the S3 origin access logs bucket name will be rendered by calling format("%v-%v-%v-%v", var.namespace, var.environment, var.stage, var.origin_s3_access_log_bucket_name) .Otherwise, the value for origin_s3_access_log_bucket_name will need to be the globally unique name of the access logs bucket.For example, if this component produces an origin bucket named eg-ue1-devplatform-example and origin_s3_access_log_bucket_name is set toexample-s3-access-logs , then the bucket name will be rendered to be eg-ue1-devplatform-example-s3-access-logs . | bool | false | no |
origin_s3_access_log_prefix | Prefix to use for S3 Access Log object keys. Defaults to logs/${module.this.id} | string | "" | no |
origin_s3_access_logging_enabled | Set true to deliver S3 Access Logs to the origin_s3_access_log_bucket_name bucket.Defaults to false if origin_s3_access_log_bucket_name is empty (the default), true otherwise.Must be set explicitly if the access log bucket is being created at the same time as this module is being invoked. | bool | null | no |
origin_versioning_enabled | Enable or disable versioning for the origin Bucket. Versioning is a means of keeping multiple variants of an object in the same bucket. | bool | false | no |
parent_zone_name | Parent domain name of site to publish. Defaults to format(parent_zone_name_pattern, stage, environment). | string | "" | no |
preview_environment_enabled | Enable or disable SPA Preview Environments via Lambda@Edge, i.e. mapping subdomain.example.com to the /subdomain path in the origin S3 bucket. This variable implicitly affects the following variables: s3_website_enabled s3_website_password_enabled block_origin_public_access_enabled origin_allow_ssl_requests_only forward_header_values cloudfront_default_ttl cloudfront_min_ttl cloudfront_max_ttl * cloudfront_lambda_function_association | bool | false | no |
process_domain_validation_options | Flag to enable/disable processing of the record to add to the DNS zone to complete certificate validation | bool | true | no |
regex_replace_chars | Terraform regular expression (regex) string. Characters matching the regex will be removed from the ID elements. If not set, "/[^a-zA-Z0-9-]/" is used to remove all characters other than hyphens, letters and digits. | string | null | no |
region | AWS Region. | string | n/a | yes |
s3_object_ownership | Specifies the S3 object ownership control on the origin bucket. Valid values are ObjectWriter , BucketOwnerPreferred , and 'BucketOwnerEnforced'. | string | "ObjectWriter" | no |
s3_website_enabled | Set to true to enable the created S3 bucket to serve as a website independently of CloudFront, and to use that website as the origin. Setting preview_environment_enabled will implicitly set this to true . | bool | false | no |
s3_website_password_enabled | If set to true, and s3_website_enabled is also true, a password will be required in the Referrer field of theHTTP request in order to access the website, and CloudFront will be configured to pass this password in its requests. This will make it much harder for people to bypass CloudFront and access the S3 website directly via its website endpoint. | bool | false | no |
site_fqdn | Fully qualified domain name of site to publish. Overrides site_subdomain and parent_zone_name. | string | "" | no |
site_subdomain | Subdomain to plug into site_name_pattern to make site FQDN. | string | "" | no |
stage | ID element. Usually used to indicate role, e.g. 'prod', 'staging', 'source', 'build', 'test', 'deploy', 'release' | string | null | no |
tags | Additional tags (e.g. {'BusinessUnit': 'XYZ'} ).Neither the tag keys nor the tag values will be modified by this module. | map(string) | {} | no |
tenant | ID element _(Rarely used, not included by default)_. A customer identifier, indicating who this instance of a resource is for | string | null | no |
Outputs
Name | Description |
---|---|
cloudfront_distribution_alias | Cloudfront Distribution Alias Record. |
cloudfront_distribution_domain_name | Cloudfront Distribution Domain Name. |
failover_s3_bucket_name | Failover Origin bucket name, if enabled. |
github_actions_iam_role_arn | ARN of IAM role for GitHub Actions |
github_actions_iam_role_name | Name of IAM role for GitHub Actions |
origin_s3_bucket_arn | Origin bucket ARN. |
origin_s3_bucket_name | Origin bucket name. |
References
- cloudposse/terraform-aws-components - Cloud Posse's upstream component