Skip to content

Commit 8e7ec88

Browse files
authored
Merge pull request #17 from cookielab/multiple_domains
feat(multiple domains): Add possibility to ACM for multiple zones + create DNS record in extra zones + CF aliases
2 parents 2620855 + 641dee9 commit 8e7ec88

File tree

3 files changed

+28
-4
lines changed

3 files changed

+28
-4
lines changed

README.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,7 @@ module "static-site" {
9898

9999
| Name | Source | Version |
100100
|------|--------|---------|
101-
| <a name="module_certificate"></a> [certificate](#module\_certificate) | terraform-aws-modules/acm/aws | 5.0.0 |
101+
| <a name="module_certificate"></a> [certificate](#module\_certificate) | terraform-aws-modules/acm/aws | 5.1.1 |
102102
| <a name="module_gitlab"></a> [gitlab](#module\_gitlab) | ./modules/gitlab | n/a |
103103
| <a name="module_s3_bucket"></a> [s3\_bucket](#module\_s3\_bucket) | terraform-aws-modules/s3-bucket/aws | 4.1.2 |
104104

@@ -116,6 +116,7 @@ module "static-site" {
116116
| [aws_kms_alias.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_alias) | resource |
117117
| [aws_kms_key.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key) | resource |
118118
| [aws_kms_key_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/kms_key_policy) | resource |
119+
| [aws_route53_record.extra](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
119120
| [aws_route53_record.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
120121
| [aws_caller_identity.current](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/caller_identity) | data source |
121122
| [aws_cloudfront_cache_policy.managed_caching_disabled](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/cloudfront_cache_policy) | data source |
@@ -136,6 +137,7 @@ module "static-site" {
136137
| <a name="input_domains"></a> [domains](#input\_domains) | List of domain aliases. You can also specify wildcard eg.: `*.example.com` | `list(string)` | n/a | yes |
137138
| <a name="input_enable_deploy_user"></a> [enable\_deploy\_user](#input\_enable\_deploy\_user) | Toggle s3 deploy user creation | `bool` | `true` | no |
138139
| <a name="input_encrypt_with_kms"></a> [encrypt\_with\_kms](#input\_encrypt\_with\_kms) | Enable server side s3 bucket encryption with KMS key | `bool` | `false` | no |
140+
| <a name="input_extra_domains"></a> [extra\_domains](#input\_extra\_domains) | Map of extra\_domains with domain name and zone\_id | `map(string)` | `{}` | no |
139141
| <a name="input_functions"></a> [functions](#input\_functions) | n/a | <pre>object({<br> viewer_request = optional(string)<br> viewer_response = optional(string)<br> })</pre> | `{}` | no |
140142
| <a name="input_gitlab_environment"></a> [gitlab\_environment](#input\_gitlab\_environment) | GitLab environment name | `string` | `"*"` | no |
141143
| <a name="input_gitlab_project_id"></a> [gitlab\_project\_id](#input\_gitlab\_project\_id) | Integrates with GitLab CI/CD to deploy site and invalidate CloudFront cache | `string` | `null` | no |

main.tf

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,18 @@ module "certificate" {
1717
}
1818

1919
source = "terraform-aws-modules/acm/aws"
20-
version = "5.0.0"
20+
version = "5.1.1"
2121

2222
domain_name = local.main_domain
2323
zone_id = var.domain_zone_id
2424

25-
subject_alternative_names = local.alternative_domains
25+
subject_alternative_names = concat(local.alternative_domains, keys(var.extra_domains))
2626

2727
validation_method = "DNS"
2828
wait_for_validation = true
2929

30+
zones = var.extra_domains
31+
3032
tags = local.tags
3133
}
3234

@@ -229,7 +231,7 @@ resource "aws_cloudfront_distribution" "this" {
229231
}
230232
}
231233

232-
aliases = var.domains
234+
aliases = concat(var.domains, keys(var.extra_domains))
233235

234236
enabled = true
235237
is_ipv6_enabled = true
@@ -349,6 +351,20 @@ resource "aws_route53_record" "this" {
349351
}
350352
}
351353

354+
resource "aws_route53_record" "extra" {
355+
for_each = var.extra_domains
356+
357+
zone_id = each.value
358+
name = each.key
359+
type = "A"
360+
361+
alias {
362+
name = aws_cloudfront_distribution.this.domain_name
363+
zone_id = aws_cloudfront_distribution.this.hosted_zone_id
364+
evaluate_target_health = false
365+
}
366+
}
367+
352368
resource "aws_cloudfront_response_headers_policy" "this" {
353369
count = length(var.s3_cors_rule) > 0 ? 1 : 0
354370
name = "${var.s3_bucket_name}-cors"

variables.tf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,3 +159,9 @@ variable "response_header_access_control_allow_credentials" {
159159
type = bool
160160
default = false
161161
}
162+
163+
variable "extra_domains" {
164+
type = map(string)
165+
description = "Map of extra_domains with domain name and zone_id"
166+
default = {}
167+
}

0 commit comments

Comments
 (0)