Skip to content

Commit 053877a

Browse files
committed
feat(acm-certificate): add module
1 parent 38d35cc commit 053877a

6 files changed

Lines changed: 95 additions & 0 deletions

File tree

acm-certificate/acm.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
locals {
2+
domains = distinct(var.domains)
3+
primary_domain = local.domains[0]
4+
}
5+
6+
resource "aws_acm_certificate" "this" {
7+
domain_name = local.primary_domain
8+
subject_alternative_names = local.domains
9+
validation_method = "DNS"
10+
tags = var.tags
11+
12+
lifecycle {
13+
create_before_destroy = true
14+
}
15+
16+
provider = aws.acm
17+
}
18+
19+
resource "aws_acm_certificate_validation" "this" {
20+
count = var.zone_id == null ? 0 : 1
21+
22+
certificate_arn = aws_acm_certificate.this.arn
23+
validation_record_fqdns = var.zone_id == null ? [] : [for record in aws_route53_record.acm_records : record.fqdn]
24+
25+
provider = aws.acm
26+
}

acm-certificate/outputs.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
output "arn" {
2+
value = var.zone_id == null ? aws_acm_certificate.this.arn : aws_acm_certificate_validation.this[0].certificate_arn
3+
}
4+
5+
output "dns" {
6+
value = {
7+
domains = local.domains
8+
records = {
9+
for dvo in aws_acm_certificate.this.domain_validation_options :
10+
dvo.domain_name => {
11+
type = dvo.resource_record_type
12+
name = dvo.resource_record_name
13+
value = dvo.resource_record_value
14+
}
15+
}
16+
}
17+
}

acm-certificate/providers.tf

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
terraform {
2+
required_version = ">= 1.13"
3+
4+
required_providers {
5+
aws = {
6+
source = "hashicorp/aws"
7+
version = "~> 6"
8+
configuration_aliases = [
9+
aws.acm,
10+
aws.default,
11+
]
12+
}
13+
}
14+
}

acm-certificate/route53.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
#############################################
2+
# Validation for the ACM cert
3+
#############################################
4+
resource "aws_route53_record" "acm_records" {
5+
for_each = var.zone_id == null ? {} : {
6+
for o in aws_acm_certificate.this.domain_validation_options : o.domain_name => o
7+
}
8+
9+
zone_id = var.zone_id
10+
type = each.value.resource_record_type
11+
name = each.value.resource_record_name
12+
records = [each.value.resource_record_value]
13+
ttl = 60
14+
allow_overwrite = true
15+
16+
provider = aws.default
17+
}

acm-certificate/variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "zone_id" {
2+
type = string
3+
description = "The ID of the hosted zone"
4+
default = null
5+
}
6+
7+
variable "domains" {
8+
type = list(string)
9+
description = "List of domain names for your CloudFront distribution. The first domain specified will be classed as the primary domain (used as S3 bucket name, Route53 hosted zone name etc.)"
10+
11+
validation {
12+
condition = length(var.domains) > 0
13+
error_message = "domains requires at least one domain to be specified"
14+
}
15+
}
16+
17+
variable "tags" {
18+
type = map(string)
19+
description = "The tags to apply to all resources created"
20+
}

static-site/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ variable "hosted_zone" {
1313
variable "domains" {
1414
type = list(string)
1515
description = "List of domain names for your CloudFront distribution. The first domain specified will be classed as the primary domain (used as S3 bucket name, Route53 hosted zone name etc.)"
16+
1617
validation {
1718
condition = length(var.domains) > 0
1819
error_message = "domains requires at least one domain to be specified"

0 commit comments

Comments
 (0)