Skip to content

Commit

Permalink
add: improve tf module
Browse files Browse the repository at this point in the history
  • Loading branch information
jordi.bru committed Jun 14, 2024
1 parent 06d7b58 commit 75caa20
Showing 5 changed files with 17 additions and 39 deletions.
21 changes: 9 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
@@ -2,28 +2,25 @@

Terraform module to create an ACM (AWS Certificate Manager) certificate with Route 53 resources for DNS validation.

## Required Variables
## Common Variables

| Name | Description | Type | Validation | Default |
|-----------|-----------------------------------------------------|--------|------------------------------------------------|---------|
| `stage` | The stage of development (e.g., test, dev, staging, prod). | string | Must be one of `test`, `dev`, `staging`, `prod` | N/A |
| `purpose` | A short description about the purpose of the created resource. | string | Must match the regex `^[a-zA-Z0-9-_]*$` | N/A |
| `owner` | The owner of the deployed infrastructure. | string | Must have more than three characters | N/A |
| Name | Description | Type | Validation | Default | Required |
|-----------|----------------------------------------------------------------|--------|-------------------------------------------------|---------|----------|
| `stage` | The stage of development (e.g., test, dev, staging, prod). | string | Must be one of `test`, `dev`, `staging`, `prod` | | yes |
| `purpose` | A short description about the purpose of the created resource. | string | Must match the regex `^[a-zA-Z0-9-_]*$` | | yes |
| `owner` | The owner of the deployed infrastructure. | string | Must have more than three characters | | yes |

## Custom Variables

| Name | Description | Type | Default |
|-----------------|--------------------------------------------------|--------|----------------------|
| `domain_name` | The name of the domain to attach the certificate.| string | `null` |
| `validate_cert` | Indicate whether to validate the certificate. | bool | `true` |
| `zone_name` | The name of the Route 53 hosted zone. | string | `jordibru.cloud` |
| Name | Description | Type | Validation | Default | Required |
|-------------|-----------------------------|--------|-------------|---------|----------|
| `subdomain` | The name of the subdomain. | string | | | yes |

## Outputs

| Name | Description |
|----------------------------|--------------------------------------------------|
| `certificate_arn` | The ARN of the validated ACM certificate. |
| `domain_validation_options`| The domain validation options for the ACM certificate. |

## Usage

2 changes: 1 addition & 1 deletion examples/basic-acm.tf
Original file line number Diff line number Diff line change
@@ -7,5 +7,5 @@ module "acm" {
purpose = "tfg"

# Custom variables
domain_name = "test.jordibru.cloud"
subdomain = "portfolio" # This will create record portfolio.jordibru.cloud
}
5 changes: 2 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
data "aws_route53_zone" "jordibru_cloud" {
name = var.zone_name
name = "jordibru.cloud"
}

resource "aws_acm_certificate" "domain_certificate" {
domain_name = var.domain_name
domain_name = "${var.subdomain}.jordibru.cloud"
validation_method = "DNS"

lifecycle {
@@ -19,7 +19,6 @@ resource "aws_acm_certificate" "domain_certificate" {
}
}

# record validation
resource "aws_route53_record" "cert_validations" {
for_each = {
for dvo in aws_acm_certificate.domain_certificate.domain_validation_options : dvo.domain_name => {
5 changes: 0 additions & 5 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -2,9 +2,4 @@
output "certificate_arn" {
description = "The ARN of the validated ACM certificate."
value = aws_acm_certificate_validation.validations.certificate_arn
}

output "domain_validation_options" {
description = "The domain validation options for the ACM certificate."
value = aws_acm_certificate.domain_certificate[*].domain_validation_options
}
23 changes: 5 additions & 18 deletions variables.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# REQUIRED VARIABLES
# COMMON VARIABLES

variable "stage" {
description = "The stage of development (e.g., test, dev, staging, prod)."
@@ -34,25 +34,12 @@ variable "owner" {

# CUSTOM VARIABLES

variable "domain_name" {
description = "The name of the domain to attach the certificate."
variable "subdomain" {
description = "The name of the subdomain."
type = string
default = null

validation {
condition = length(var.domain_name) >= 3
error_message = "You must define a domain name with at least three characters that exists on the account."
condition = length(var.subdomain) >= 3
error_message = "You must define a subdomain name with at least three characters."
}
}

variable "validate_cert" {
description = "Indicate whether to validate the certificate."
type = bool
default = true
}

variable "zone_name" {
description = "The name of the Route 53 hosted zone."
type = string
default = "jordibru.cloud"
}

0 comments on commit 75caa20

Please sign in to comment.