Skip to content

Commit b595caa

Browse files
LeoDiazLllonghi
authored andcommitted
Add cloudfront customizable error response (#14)
* Initial commit * Adding custom_error_response option * Fixing input * map[string] to JSON * Fixing multiple inputs * Fixing README * Bumping README * Fixing default for custom_error_codes
1 parent 2b72cc9 commit b595caa

File tree

5 files changed

+102
-2
lines changed

5 files changed

+102
-2
lines changed

README.md

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656

5757
steps:
5858
- name: Create deploy-bucket
59-
uses: bitovi/[email protected].1
59+
uses: bitovi/[email protected].2
6060
with:
6161
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
6262
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
@@ -72,6 +72,68 @@ jobs:
7272
aws_r53_create_sub_cert: true # Will create and validate a cert for this sub-domain
7373
```
7474
75+
### Advanced example
76+
```yaml
77+
name: Basic deploy
78+
on:
79+
push:
80+
branches: [ main ]
81+
82+
jobs:
83+
deploy-catalog:
84+
runs-on: ubuntu-latest
85+
steps:
86+
- name: Create deploy-bucket
87+
uses: bitovi/[email protected]
88+
with:
89+
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
90+
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
91+
aws_default_region: us-east-1
92+
93+
aws_site_source_folder: packages/catalog/dist
94+
tf_state_file_name_append: catalog
95+
96+
tf_action: 'apply'
97+
tf_state_bucket: 'some-custom-bucket'
98+
tf_state_bucket_destroy: true
99+
100+
aws_site_cdn_enabled: true
101+
aws_site_cdn_custom_error_codes: '[{\"error_caching_min_ttl\":\"0\",\"error_code\":\"403\",\"response_code\":\"200\",\"response_page_path\":\"/index.html\"},{\"error_caching_min_ttl\":\"0\",\"error_code\":\"404\",\"response_code\":\"404\",\"response_page_path\":\"/custom_404.html\"}]'
102+
103+
aws_r53_domain_name: bitovi-sandbox.com
104+
aws_r53_sub_domain_name: catalog-mfe
105+
aws_r53_create_sub_cert: true
106+
107+
aws_site_bucket_name: catalog-mfe
108+
109+
deploy-marketing:
110+
runs-on: ubuntu-latest
111+
steps:
112+
- name: Create deploy-bucket
113+
uses: bitovi/[email protected]
114+
with:
115+
aws_access_key_id: ${{ secrets.AWS_ACCESS_KEY_ID }}
116+
aws_secret_access_key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
117+
aws_default_region: us-east-1
118+
119+
checkout: false
120+
aws_site_source_folder: packages/marketing/dist
121+
tf_state_file_name_append: marketing
122+
123+
tf_action: 'apply'
124+
tf_state_bucket: 'some-custom-bucket'
125+
tf_state_bucket_destroy: true
126+
127+
aws_site_cdn_enabled: true
128+
aws_site_cdn_custom_error_codes: '[{\"error_caching_min_ttl\":\"0\",\"error_code\":\"403\",\"response_code\":\"200\",\"response_page_path\":\"/index.html\"},{\"error_caching_min_ttl\":\"0\",\"error_code\":\"404\",\"response_code\":\"404\",\"response_page_path\":\"/custom_404.html\"}]'
129+
130+
aws_r53_domain_name: bitovi-sandbox.com
131+
aws_r53_sub_domain_name: marketing-mfe
132+
aws_r53_create_sub_cert: true
133+
134+
aws_site_bucket_name: marketing-mfe
135+
```
136+
75137
## Customizing
76138
77139
### Inputs
@@ -122,6 +184,7 @@ The following inputs can be used as `step.with` keys
122184
| `aws_site_root_object` | Boolean | Root object to be served as entry-point. Defaults to `index.html`. |
123185
| `aws_site_bucket_name` | String | AWS S3 bucket name to use for the public files. Defaults to `${org}-${repo}-{branch}-sp`. If using a R53 domain and not a CDN, bucket name will be the FQDN one. See note. |
124186
| `aws_site_cdn_enabled` | Boolean | Enable or disables the use of CDN. Defaults to `false`. |
187+
| `aws_site_cdn_custom_error_codes` | JSON | Custom error codes to define in CDN. Like `[{\"error_caching_min_ttl\":\"0\",\"error_code\":\"403\",\"response_code\":\"200\",\"response_page_path\":\"/index.html\"}]`. See [this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_distribution.html#custom-error-response-arguments). |
125188
<hr/>
126189
<br/>
127190

action.yaml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ inputs:
3333

3434
# Terraform options
3535
tf_action:
36-
desctiption: 'Option to run Terraform apply / destroy option. Will run plan if nothing defined.'
36+
description: 'Option to run Terraform apply / destroy option. Will run plan if nothing defined.'
3737
required: false
3838
tf_state_file_name:
3939
description: 'Change this to be anything you want to. Carefull to be consistent here. A missing file could trigger recreation, or stepping over destruction of non-defined objects.'
@@ -61,6 +61,9 @@ inputs:
6161
aws_site_cdn_enabled:
6262
description: 'Enable or disables the use of CDN. Defaults to `false`.'
6363
required: false
64+
aws_site_cdn_custom_error_codes:
65+
description: 'Customize error codes, Definition done with JSON variables'
66+
required: false
6467

6568
# AWS Route53 Domains and Certificates
6669
aws_r53_domain_name:
@@ -120,6 +123,7 @@ runs:
120123
AWS_SITE_ROOT_OBJECT: ${{ inputs.aws_site_root_object }}
121124
AWS_SITE_BUCKET_NAME: ${{ inputs.aws_site_bucket_name }}
122125
AWS_SITE_CDN_ENABLED: ${{ inputs.aws_site_cdn_enabled }}
126+
AWS_SITE_CDN_CUSTOM_ERROR_CODES: ${{ inputs.aws_site_cdn_custom_error_codes }}
123127
# AWS Route53 Domains abd Certificates
124128
AWS_R53_DOMAIN_NAME: ${{ inputs.aws_r53_domain_name }}
125129
AWS_R53_SUB_DOMAIN_NAME: ${{ inputs.aws_r53_sub_domain_name }}

scripts/generate_deploy.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,6 +88,7 @@ aws_default_region=$(generate_var aws_default_region $AWS_DEFAULT_REGION)
8888
aws_site_source_folder="aws_site_source_folder = \"${SOURCE_FILES}\""
8989
aws_site_bucket_name=$(generate_var aws_site_bucket_name $AWS_SITE_BUCKET_NAME)
9090
aws_site_cdn_enabled=$(generate_var aws_site_cdn_enabled $AWS_SITE_CDN_ENABLED)
91+
aws_site_cdn_custom_error_codes=$(generate_var aws_site_cdn_custom_error_codes $AWS_SITE_CDN_CUSTOM_ERROR_CODES)
9192
aws_site_root_object=$(generate_var aws_site_root_object $AWS_SITE_ROOT_OBJECT)
9293
aws_r53_domain_name=$(generate_var aws_r53_domain_name $AWS_R53_DOMAIN_NAME)
9394
aws_r53_root_domain_deploy=$(generate_var aws_r53_root_domain_deploy $AWS_R53_ROOT_DOMAIN_DEPLOY)
@@ -107,6 +108,7 @@ $aws_tf_state_bucket
107108
$aws_site_source_folder
108109
$aws_site_bucket_name
109110
$aws_site_cdn_enabled
111+
$aws_site_cdn_custom_error_codes
110112
$aws_site_root_object
111113
$aws_r53_domain_name
112114
$aws_r53_sub_domain_name

terraform_code/main.tf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,17 @@ resource "aws_cloudfront_distribution" "cdn_static_site_default_cert" {
167167
}
168168
}
169169

170+
dynamic "custom_error_response" {
171+
for_each = { for idx, val in local.aws_site_cdn_custom_error_codes : idx => val }
172+
173+
content {
174+
error_caching_min_ttl = try(custom_error_response.value.error_caching_min_ttl, null)
175+
error_code = custom_error_response.value.error_code
176+
response_code = try(custom_error_response.value.response_code, null)
177+
response_page_path = try(custom_error_response.value.response_page_path, null)
178+
}
179+
}
180+
170181
viewer_certificate {
171182
cloudfront_default_certificate = true
172183
}
@@ -211,6 +222,17 @@ resource "aws_cloudfront_distribution" "cdn_static_site" {
211222
}
212223
}
213224

225+
dynamic "custom_error_response" {
226+
for_each = { for idx, val in local.aws_site_cdn_custom_error_codes : idx => val }
227+
228+
content {
229+
error_caching_min_ttl = try(custom_error_response.value.error_caching_min_ttl, null)
230+
error_code = custom_error_response.value.error_code
231+
response_code = try(custom_error_response.value.response_code, null)
232+
response_page_path = try(custom_error_response.value.response_page_path, null)
233+
}
234+
}
235+
214236
aliases = [ var.aws_r53_root_domain_deploy ? "${var.aws_r53_domain_name}" : "${var.aws_r53_sub_domain_name}.${var.aws_r53_domain_name}" ]
215237

216238
viewer_certificate {
@@ -389,6 +411,9 @@ locals {
389411
false
390412
)
391413

414+
### Converting JSON to map of strings as GH Actions don't accept map of strings
415+
aws_site_cdn_custom_error_codes = jsondecode(var.aws_site_cdn_custom_error_codes)
416+
392417
### Try looking up for the cert with different names
393418
acm_arn = try(data.aws_acm_certificate.issued["domain"].arn, try(data.aws_acm_certificate.issued["wildcard"].arn, data.aws_acm_certificate.issued["sub"].arn, ""))
394419

terraform_code/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ variable "aws_site_cdn_enabled" {
4343
default = false
4444
}
4545

46+
variable "aws_site_cdn_custom_error_codes" {
47+
description = "Custom error codes for site"
48+
type = string
49+
default = "{}"
50+
}
51+
4652
variable "aws_r53_domain_name" {
4753
description = "root domain name without any subdomains"
4854
type = string

0 commit comments

Comments
 (0)