Skip to content

Commit e153d2a

Browse files
LeoDiazLmcascone
andauthored
Add support for user defined aliases in CDN (#22)
* Add support for user defined aliases in CDN * Update README.md Co-authored-by: Max Cascone <[email protected]>
1 parent 8bb9232 commit e153d2a

File tree

5 files changed

+30
-1
lines changed

5 files changed

+30
-1
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Setting `aws_r53_create_sub_cert` to `true` will create a certificate **just for
4444
4545
> :warning: Be very careful here! **Created certificates are fully managed by Terraform**. Therefor **they will be destroyed upon stack destruction**.
4646
47+
> :warning: See note about CDN with aliases if using certificates.
4748
## Example usage
4849

4950
Create `.github/workflow/deploy.yaml` with the following to build on push.
@@ -192,6 +193,7 @@ The following inputs can be used as `step.with` keys
192193
| `aws_site_error_document` | String | Error document set to S3 website config. Defaults to none. Set value to enable it. |
193194
| `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. |
194195
| `aws_site_cdn_enabled` | Boolean | Enable or disables the use of CDN. Defaults to `false`. |
196+
| `aws_site_cdn_aliases` | String | Extra CNAMEs (alternate domain names), if any, for this distribution. Defaults to defined domain if none passed. (See note). |
195197
| `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). |
196198
<hr/>
197199
<br/>
@@ -229,6 +231,16 @@ Because of this reason, the length of the FQDN *MUST* be below 64 characters. Wi
229231

230232
In the case you are using domains and not using a CDN, no cert will be available, and length of the FQDN *MUST* be below 64 characters. Will be adjusted if it exceeds that limit.
231233

234+
## Certificates with CDN
235+
236+
In the case you are using a custom domain name and need to support two alternate domain names, you can use the `aws_site_cdn_aliases`.
237+
If using a certificate, keep in mind that you'll need to specify one that covers the domains being defined.
238+
239+
For example, if the CDN will support `site.bitovi.com` and `site.bitovi.tools`, the same certificate must cover both *bitovi.com* and *bitovi.tools* domains. (You can use sub-domains too). In that case, you'll need to specify the certificate by defining the `aws_r53_cert_arn`.
240+
If that's the case, `aws_site_cdn_aliases` should be set to: `site.bitovi.com,site.bitovi.tools` (Comma separated, no spaces).
241+
242+
If they alternate domain names are child of the same domain, you can use a root cert for both.
243+
232244
## Contributing
233245
We would love for you to contribute to [bitovi/github-actions-deploy-static-site-to-aws](https://github.com/bitovi/github-actions-deploy-static-site-to-aws).
234246
Would you like to see additional features? [Create an issue](https://github.com/bitovi/github-actions-deploy-static-site-to-aws/issues/new) or a [Pull Requests](https://github.com/bitovi/github-actions-deploy-static-site-to-aws/pulls). We love discussing solutions!

action.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ inputs:
6767
aws_site_cdn_enabled:
6868
description: 'Enable or disables the use of CDN. Defaults to `false`.'
6969
required: false
70+
aws_site_cdn_aliases:
71+
description: 'Extra CNAMEs (alternate domain names), if any, for this distribution. Will default to defined domain if none passed.'
72+
required: false
7073
aws_site_cdn_custom_error_codes:
7174
description: 'Customize error codes, Definition done with JSON variables'
7275
required: false
@@ -131,6 +134,7 @@ runs:
131134
AWS_SITE_ERROR_DOCUMENT: ${{ inputs.aws_site_error_document }}
132135
AWS_SITE_BUCKET_NAME: ${{ inputs.aws_site_bucket_name }}
133136
AWS_SITE_CDN_ENABLED: ${{ inputs.aws_site_cdn_enabled }}
137+
AWS_SITE_CDN_ALIASES: ${{ inputs.aws_site_cdn_aliases }}
134138
AWS_SITE_CDN_CUSTOM_ERROR_CODES: ${{ inputs.aws_site_cdn_custom_error_codes }}
135139
# AWS Route53 Domains abd Certificates
136140
AWS_R53_DOMAIN_NAME: ${{ inputs.aws_r53_domain_name }}

scripts/generate_deploy.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ aws_default_region=$(generate_var aws_default_region $AWS_DEFAULT_REGION)
9595
aws_site_source_folder="aws_site_source_folder = \"${SOURCE_FILES}\""
9696
aws_site_bucket_name=$(generate_var aws_site_bucket_name $AWS_SITE_BUCKET_NAME)
9797
aws_site_cdn_enabled=$(generate_var aws_site_cdn_enabled $AWS_SITE_CDN_ENABLED)
98+
aws_site_cdn_aliases=$(generate_var aws_site_cdn_aliases $AWS_SITE_CDN_ALIASES)
9899
aws_site_cdn_custom_error_codes=$(generate_var aws_site_cdn_custom_error_codes $AWS_SITE_CDN_CUSTOM_ERROR_CODES)
99100
aws_site_root_object=$(generate_var aws_site_root_object $AWS_SITE_ROOT_OBJECT)
100101
aws_site_error_document=$(generate_var aws_site_error_document $AWS_SITE_ERROR_DOCUMENT)
@@ -116,6 +117,7 @@ $aws_tf_state_bucket
116117
$aws_site_source_folder
117118
$aws_site_bucket_name
118119
$aws_site_cdn_enabled
120+
$aws_site_cdn_aliases
119121
$aws_site_cdn_custom_error_codes
120122
$aws_site_root_object
121123
$aws_site_error_document

terraform_code/main.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -240,7 +240,7 @@ resource "aws_cloudfront_distribution" "cdn_static_site" {
240240
}
241241
}
242242

243-
aliases = [ var.aws_r53_root_domain_deploy ? "${var.aws_r53_domain_name}" : "${var.aws_r53_sub_domain_name}.${var.aws_r53_domain_name}" ]
243+
aliases = var.aws_site_cdn_aliases != "" ? local.parsed_aliases : [ var.aws_r53_root_domain_deploy ? "${var.aws_r53_domain_name}" : "${var.aws_r53_sub_domain_name}.${var.aws_r53_domain_name}" ]
244244

245245
viewer_certificate {
246246
acm_certificate_arn = local.selected_arn
@@ -257,6 +257,11 @@ resource "aws_cloudfront_distribution" "cdn_static_site" {
257257
]
258258
}
259259

260+
locals {
261+
parsed_aliases = [for n in split(",", var.aws_site_cdn_aliases) : (n)]
262+
}
263+
264+
260265
### CDN Access control
261266
resource "aws_cloudfront_origin_access_control" "default" {
262267
count = var.aws_site_cdn_enabled ? 1 : 0

terraform_code/variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ variable "aws_site_cdn_enabled" {
4949
default = false
5050
}
5151

52+
variable "aws_site_cdn_aliases" {
53+
description = "Aliases or CNAMES for CDN"
54+
type = string
55+
default = ""
56+
}
57+
5258
variable "aws_site_cdn_custom_error_codes" {
5359
description = "Custom error codes for site"
5460
type = string

0 commit comments

Comments
 (0)