|
1 | | -# Terraform AWS S3 CloudFront static website |
| 1 | +# Terraform AWS CloudFront S3 hosting |
2 | 2 |
|
3 | | -Use this module to provision a CloudFront distribution to serve an static website from a private S3 bucket, along with policies and related resources. |
| 3 | +Use this module to provision a CloudFront distribution to serve an static website or assets from a private S3 bucket, along with policies and related resources. |
| 4 | + |
| 5 | +The `default_root_object` is expected to be `"index.html"` but a [function_association](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution#function_association-1) can be provided to customize requests handling. |
4 | 6 |
|
5 | 7 | Check the [.tool-versions](./.tool-versions) file to see the tools and the versions used in this project. |
6 | 8 |
|
| 9 | +## Website Deployment |
| 10 | + |
| 11 | +The _basic_ steps to deploy an static website with this setup: |
| 12 | + |
| 13 | +1. Build your site |
| 14 | +2. Upload/sync your new build to S3 |
| 15 | +3. Invalidate CloudFront cache |
| 16 | + |
| 17 | +For example: |
| 18 | + |
| 19 | +```bash |
| 20 | +# Build (outputs to the dist directory) |
| 21 | +npm run build |
| 22 | + |
| 23 | +# Sync to the S3 bucket my-web-bucket |
| 24 | +aws s3 sync ./dist s3://my-web-bucket/ --delete |
| 25 | + |
| 26 | +# Invalidate CloudFront cache |
| 27 | +aws cloudfront create-invalidation \ |
| 28 | + --distribution-id $CLOUDFRONT_DIST_ID \ |
| 29 | + --paths "/*" |
| 30 | +``` |
| 31 | + |
7 | 32 | **NOTICE:** The Terraform documentation in this README is added by [terraform-docs](https://terraform-docs.io) running as a pre-commit hook, see the [.pre-commit-config.yaml](./.pre-commit-config.yaml) file. |
8 | 33 |
|
9 | 34 | <!-- BEGIN_TF_DOCS --> |
| 35 | + |
10 | 36 | ## Requirements |
11 | 37 |
|
12 | | -| Name | Version | |
13 | | -|------|---------| |
14 | | -| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | 1.13.3 | |
15 | | -| <a name="requirement_aws"></a> [aws](#requirement\_aws) | ~> 6.15 | |
| 38 | +| Name | Version | |
| 39 | +| ------------------------------------------------------------------------ | ------- | |
| 40 | +| <a name="requirement_terraform"></a> [terraform](#requirement_terraform) | 1.13.3 | |
| 41 | +| <a name="requirement_aws"></a> [aws](#requirement_aws) | ~> 6.15 | |
16 | 42 |
|
17 | 43 | ## Providers |
18 | 44 |
|
19 | | -| Name | Version | |
20 | | -|------|---------| |
21 | | -| <a name="provider_aws"></a> [aws](#provider\_aws) | ~> 6.15 | |
| 45 | +| Name | Version | |
| 46 | +| ------------------------------------------------ | ------- | |
| 47 | +| <a name="provider_aws"></a> [aws](#provider_aws) | ~> 6.15 | |
22 | 48 |
|
23 | 49 | ## Resources |
24 | 50 |
|
25 | | -| Name | Type | |
26 | | -|------|------| |
27 | | -| [aws_cloudfront_distribution.cdn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution) | resource | |
28 | | -| [aws_cloudfront_origin_access_control.oac](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_origin_access_control) | resource | |
29 | | -| [aws_cloudwatch_log_delivery.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery) | resource | |
30 | | -| [aws_cloudwatch_log_delivery_destination.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_destination) | resource | |
31 | | -| [aws_cloudwatch_log_delivery_source.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_source) | resource | |
32 | | -| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | |
33 | | -| [aws_route53_record.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource | |
34 | | -| [aws_s3_bucket.origin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource | |
35 | | -| [aws_s3_bucket_policy.origin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource | |
36 | | -| [aws_cloudfront_cache_policy.optimize](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_cache_policy) | data source | |
37 | | -| [aws_iam_policy_document.origin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | |
| 51 | +| Name | Type | |
| 52 | +| --------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------- | |
| 53 | +| [aws_cloudfront_distribution.cdn](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution) | resource | |
| 54 | +| [aws_cloudfront_origin_access_control.oac](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_origin_access_control) | resource | |
| 55 | +| [aws_cloudwatch_log_delivery.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery) | resource | |
| 56 | +| [aws_cloudwatch_log_delivery_destination.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_destination) | resource | |
| 57 | +| [aws_cloudwatch_log_delivery_source.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_delivery_source) | resource | |
| 58 | +| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource | |
| 59 | +| [aws_route53_record.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource | |
| 60 | +| [aws_s3_bucket.origin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource | |
| 61 | +| [aws_s3_bucket_policy.origin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket_policy) | resource | |
| 62 | +| [aws_cloudfront_cache_policy.optimize](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_cache_policy) | data source | |
| 63 | +| [aws_iam_policy_document.origin](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/iam_policy_document) | data source | |
38 | 64 |
|
39 | 65 | ## Inputs |
40 | 66 |
|
41 | | -| Name | Description | Type | Default | Required | |
42 | | -|------|-------------|------|---------|:--------:| |
43 | | -| <a name="input_acm_cert_arn"></a> [acm\_cert\_arn](#input\_acm\_cert\_arn) | ACM certificate to use if aliases are given. A CloudFront cert is used by default | `string` | `null` | no | |
44 | | -| <a name="input_cdn_aliases"></a> [cdn\_aliases](#input\_cdn\_aliases) | n/a | `list(string)` | `[]` | no | |
45 | | -| <a name="input_env"></a> [env](#input\_env) | Deploy environment, e.g., prod, dev | `string` | n/a | yes | |
46 | | -| <a name="input_function_association"></a> [function\_association](#input\_function\_association) | Forwarded to the aws\_cloudfront\_distribution resource | `any` | `null` | no | |
47 | | -| <a name="input_project"></a> [project](#input\_project) | Name of the project in lowercase without spaces, e.g., myproject | `string` | n/a | yes | |
48 | | -| <a name="input_route53_zone_id"></a> [route53\_zone\_id](#input\_route53\_zone\_id) | Hosted zone where the CDN aliases will be added to | `string` | `null` | no | |
| 67 | +| Name | Description | Type | Default | Required | |
| 68 | +| --------------------------------------------------------------------------------------------- | --------------------------------------------------------------------------------- | -------------- | ------- | :------: | |
| 69 | +| <a name="input_acm_cert_arn"></a> [acm_cert_arn](#input_acm_cert_arn) | ACM certificate to use if aliases are given. A CloudFront cert is used by default | `string` | `null` | no | |
| 70 | +| <a name="input_cdn_aliases"></a> [cdn_aliases](#input_cdn_aliases) | n/a | `list(string)` | `[]` | no | |
| 71 | +| <a name="input_env"></a> [env](#input_env) | Deploy environment, e.g., prod, dev | `string` | n/a | yes | |
| 72 | +| <a name="input_function_association"></a> [function_association](#input_function_association) | Forwarded to the aws_cloudfront_distribution resource | `any` | `null` | no | |
| 73 | +| <a name="input_project"></a> [project](#input_project) | Name of the project in lowercase without spaces, e.g., myproject | `string` | n/a | yes | |
| 74 | +| <a name="input_route53_zone_id"></a> [route53_zone_id](#input_route53_zone_id) | Hosted zone where the CDN aliases will be added to | `string` | `null` | no | |
49 | 75 |
|
50 | 76 | ## Outputs |
51 | 77 |
|
52 | | -| Name | Description | |
53 | | -|------|-------------| |
54 | | -| <a name="output_cloudfront_dist_aliases"></a> [cloudfront\_dist\_aliases](#output\_cloudfront\_dist\_aliases) | CloudFront distribution aliases | |
55 | | -| <a name="output_cloudfront_dist_domain_name"></a> [cloudfront\_dist\_domain\_name](#output\_cloudfront\_dist\_domain\_name) | CloudFront distribution domain name | |
56 | | -| <a name="output_cloudfront_dist_id"></a> [cloudfront\_dist\_id](#output\_cloudfront\_dist\_id) | CloudFront distribution id | |
57 | | -| <a name="output_cloudfront_hosted_zone_id"></a> [cloudfront\_hosted\_zone\_id](#output\_cloudfront\_hosted\_zone\_id) | CloudFront distribution hosted\_zone\_id | |
58 | | -| <a name="output_s3_bucket_name"></a> [s3\_bucket\_name](#output\_s3\_bucket\_name) | Name of the S3 bucket used for the site | |
| 78 | +| Name | Description | |
| 79 | +| -------------------------------------------------------------------------------------------------------------------- | --------------------------------------- | |
| 80 | +| <a name="output_cloudfront_dist_aliases"></a> [cloudfront_dist_aliases](#output_cloudfront_dist_aliases) | CloudFront distribution aliases | |
| 81 | +| <a name="output_cloudfront_dist_domain_name"></a> [cloudfront_dist_domain_name](#output_cloudfront_dist_domain_name) | CloudFront distribution domain name | |
| 82 | +| <a name="output_cloudfront_dist_id"></a> [cloudfront_dist_id](#output_cloudfront_dist_id) | CloudFront distribution id | |
| 83 | +| <a name="output_cloudfront_hosted_zone_id"></a> [cloudfront_hosted_zone_id](#output_cloudfront_hosted_zone_id) | CloudFront distribution hosted_zone_id | |
| 84 | +| <a name="output_s3_bucket_name"></a> [s3_bucket_name](#output_s3_bucket_name) | Name of the S3 bucket used for the site | |
| 85 | + |
59 | 86 | <!-- END_TF_DOCS --> |
0 commit comments