-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add resource creation * Add examples
- Loading branch information
Showing
15 changed files
with
376 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
data "cloudflare_zone" "this" { | ||
name = var.zone_name | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Certificate with Subject Alternate Names (SAN) | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Requirements | ||
|
||
No requirements. | ||
|
||
## Providers | ||
|
||
No providers. | ||
|
||
## Modules | ||
|
||
| Name | Source | Version | | ||
|------|--------|---------| | ||
| <a name="module_this"></a> [this](#module\_this) | ../../ | n/a | | ||
|
||
## Resources | ||
|
||
No resources. | ||
|
||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| <a name="input_region"></a> [region](#input\_region) | The AWS region this module is strictly bound to. | `string` | `"eu-central-1"` | no | | ||
| <a name="input_cloudflare_api_token"></a> [cloudflare\_api\_token](#input\_cloudflare\_api\_token) | The Cloudflare API token. | `string` | n/a | yes | | ||
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | A domain name for which the certificate should be issued | `string` | `"example.com"` | no | | ||
|
||
## Outputs | ||
|
||
No outputs. | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
|
||
## License | ||
|
||
**[MIT License](../../LICENSE)** | ||
|
||
Copyright (c) 2021 **[Flaconi GmbH](https://github.com/flaconi)** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
module "this" { | ||
source = "../../" | ||
|
||
cloudflare_api_token = var.cloudflare_api_token | ||
|
||
zone_name = var.domain_name | ||
domain_name = "test-san.${var.domain_name}" | ||
|
||
subject_alternative_names = [ | ||
"subdomain.test-san.${var.domain_name}", | ||
"test-other.${var.domain_name}" | ||
] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
provider "aws" { | ||
region = var.region | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
variable "region" { | ||
type = string | ||
description = "The AWS region this module is strictly bound to." | ||
default = "eu-central-1" | ||
} | ||
|
||
variable "cloudflare_api_token" { | ||
description = "The Cloudflare API token." | ||
type = string | ||
} | ||
|
||
variable "domain_name" { | ||
description = "A domain name for which the certificate should be issued" | ||
type = string | ||
default = "example.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
# Simple example with single domain | ||
|
||
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
## Requirements | ||
|
||
No requirements. | ||
|
||
## Providers | ||
|
||
No providers. | ||
|
||
## Modules | ||
|
||
| Name | Source | Version | | ||
|------|--------|---------| | ||
| <a name="module_this"></a> [this](#module\_this) | ../../ | n/a | | ||
|
||
## Resources | ||
|
||
No resources. | ||
|
||
## Inputs | ||
|
||
| Name | Description | Type | Default | Required | | ||
|------|-------------|------|---------|:--------:| | ||
| <a name="input_region"></a> [region](#input\_region) | The AWS region this module is strictly bound to. | `string` | `"eu-central-1"` | no | | ||
| <a name="input_cloudflare_api_token"></a> [cloudflare\_api\_token](#input\_cloudflare\_api\_token) | The Cloudflare API token. | `string` | n/a | yes | | ||
| <a name="input_domain_name"></a> [domain\_name](#input\_domain\_name) | A domain name for which the certificate should be issued | `string` | `"example.com"` | no | | ||
|
||
## Outputs | ||
|
||
No outputs. | ||
|
||
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK --> | ||
|
||
## License | ||
|
||
**[MIT License](../../LICENSE)** | ||
|
||
Copyright (c) 2021 **[Flaconi GmbH](https://github.com/flaconi)** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
module "this" { | ||
source = "../../" | ||
|
||
cloudflare_api_token = var.cloudflare_api_token | ||
|
||
zone_name = var.domain_name | ||
domain_name = "test.${var.domain_name}" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
provider "aws" { | ||
region = var.region | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
variable "region" { | ||
type = string | ||
description = "The AWS region this module is strictly bound to." | ||
default = "eu-central-1" | ||
} | ||
|
||
variable "cloudflare_api_token" { | ||
description = "The Cloudflare API token." | ||
type = string | ||
} | ||
|
||
variable "domain_name" { | ||
description = "A domain name for which the certificate should be issued" | ||
type = string | ||
default = "example.com" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
locals { | ||
# Get distinct list of domains and SANs | ||
distinct_domain_names = distinct( | ||
[for s in concat([var.domain_name], var.subject_alternative_names) : replace(s, "*.", "")] | ||
) | ||
|
||
# Get the list of distinct domain_validation_options, with wildcard | ||
# domain names replaced by the domain name | ||
validation_domains = var.create_certificate ? distinct( | ||
[for k, v in aws_acm_certificate.this[0].domain_validation_options : merge( | ||
tomap(v), { domain_name = replace(v.domain_name, "*.", "") } | ||
)] | ||
) : [] | ||
} | ||
|
||
resource "aws_acm_certificate" "this" { | ||
count = var.create_certificate ? 1 : 0 | ||
|
||
domain_name = var.domain_name | ||
subject_alternative_names = var.subject_alternative_names | ||
validation_method = "DNS" | ||
|
||
options { | ||
certificate_transparency_logging_preference = var.certificate_transparency_logging_preference ? "ENABLED" : "DISABLED" | ||
} | ||
|
||
tags = var.tags | ||
|
||
lifecycle { | ||
create_before_destroy = true | ||
} | ||
} | ||
|
||
resource "cloudflare_record" "validation" { | ||
count = var.create_certificate && var.validate_certificate ? length(local.distinct_domain_names) : 0 | ||
|
||
zone_id = data.cloudflare_zone.this.id | ||
name = element(local.validation_domains, count.index)["resource_record_name"] | ||
type = element(local.validation_domains, count.index)["resource_record_type"] | ||
value = replace(element(local.validation_domains, count.index)["resource_record_value"], "/.$/", "") | ||
ttl = var.dns_ttl | ||
proxied = false | ||
|
||
allow_overwrite = var.validation_allow_overwrite_records | ||
|
||
depends_on = [aws_acm_certificate.this] | ||
} | ||
|
||
resource "aws_acm_certificate_validation" "this" { | ||
count = var.create_certificate && var.validate_certificate && var.wait_for_validation ? 1 : 0 | ||
|
||
certificate_arn = aws_acm_certificate.this[0].arn | ||
|
||
validation_record_fqdns = cloudflare_record.validation.*.hostname | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
output "acm_certificate_arn" { | ||
description = "The ARN of the certificate" | ||
value = element(concat(aws_acm_certificate_validation.this.*.certificate_arn, aws_acm_certificate.this.*.arn, [""]), 0) | ||
} | ||
|
||
output "acm_certificate_domain_validation_options" { | ||
description = "A list of attributes to feed into other resources to complete certificate validation. Can have more than one element, e.g. if SANs are defined." | ||
value = flatten(aws_acm_certificate.this.*.domain_validation_options) | ||
} | ||
|
||
output "validation_dns_record_fqdns" { | ||
description = "List of FQDNs built using the zone domain and name." | ||
value = cloudflare_record.validation.*.hostname | ||
} | ||
|
||
output "distinct_domain_names" { | ||
description = "List of distinct domains names used for the validation." | ||
value = local.distinct_domain_names | ||
} | ||
|
||
output "validation_domains" { | ||
description = "List of distinct domain validation options. This is useful if subject alternative names contain wildcards." | ||
value = local.validation_domains | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
provider "cloudflare" { | ||
api_token = var.api_token | ||
api_token = var.cloudflare_api_token | ||
} |
Oops, something went wrong.