Skip to content

Commit

Permalink
Merge pull request #19 from mineiros-io/mariux/improve-example
Browse files Browse the repository at this point in the history
implement module_enabled and module_depends_on
  • Loading branch information
mariux authored May 3, 2020
2 parents 5fbf3f2 + 604d6b3 commit 594a7c5
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 10 deletions.
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ ifndef BUILD_TOOLS_DOCKER_IMAGE
BUILD_TOOLS_DOCKER_IMAGE := ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}
endif

USER_UID := $(shell id -u)
USER_GID := $(shell id -g)

GREEN := $(shell tput -Txterm setaf 2)
YELLOW := $(shell tput -Txterm setaf 3)
WHITE := $(shell tput -Txterm setaf 7)
Expand All @@ -36,6 +39,8 @@ docker/pre-commit-hooks:
@echo "${GREEN}Start running the pre-commit hooks with docker${RESET}"
@docker run --rm \
-v ${PWD}:${MOUNT_TARGET_DIRECTORY} \
-u ${USER_UID}:${USER_GID} \
-e HOME=/tmp \
${BUILD_TOOLS_DOCKER_IMAGE} \
sh -c "pre-commit run -a"

Expand All @@ -45,6 +50,8 @@ docker/unit-tests:
@docker run --rm \
-e AWS_ACCESS_KEY_ID \
-e AWS_SECRET_ACCESS_KEY \
-u ${USER_UID}:${USER_GID} \
-e HOME=/tmp \
-v ${PWD}:${MOUNT_TARGET_DIRECTORY} \
${BUILD_TOOLS_DOCKER_IMAGE} \
go test -v -timeout 45m -parallel 128 ./test
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ a record for `www` pointing to localhost.
```hcl
module "repository" {
source = "mineiros-io/route53/aws"
version = "0.1.0"
version = "0.2.0"
name = "mineiros.io"
Expand Down
12 changes: 9 additions & 3 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ locals {
zones = var.name == null ? [] : try(tolist(var.name), [tostring(var.name)], [])
skip_zone_creation = length(local.zones) == 0
run_in_vpc = length(var.vpc_ids) > 0
skip_delegation_set_creation = ! var.enable_module || local.skip_zone_creation || local.run_in_vpc ? true : var.skip_delegation_set_creation
skip_delegation_set_creation = ! var.module_enabled || local.skip_zone_creation || local.run_in_vpc ? true : var.skip_delegation_set_creation

delegation_set_id = var.delegation_set_id != null ? var.delegation_set_id : try(
aws_route53_delegation_set.delegation_set[0].id, null
Expand All @@ -27,14 +27,16 @@ resource "aws_route53_delegation_set" "delegation_set" {
count = local.skip_delegation_set_creation ? 0 : 1

reference_name = var.reference_name

depends_on = [var.module_depends_on]
}

# ---------------------------------------------------------------------------------------------------------------------
# Create the zones
# ---------------------------------------------------------------------------------------------------------------------

resource "aws_route53_zone" "zone" {
for_each = var.enable_module ? toset(local.zones) : []
for_each = var.module_enabled ? toset(local.zones) : []

name = each.value
comment = var.comment
Expand All @@ -53,6 +55,8 @@ resource "aws_route53_zone" "zone" {
{ Name = each.value },
var.tags
)

depends_on = [var.module_depends_on]
}

# ---------------------------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -124,7 +128,7 @@ locals {
# ---------------------------------------------------------------------------------------------------------------------

resource "aws_route53_record" "record" {
for_each = var.enable_module ? local.records : {}
for_each = var.module_enabled ? local.records : {}

zone_id = each.value.zone_id
type = each.value.type
Expand Down Expand Up @@ -167,4 +171,6 @@ resource "aws_route53_record" "record" {
evaluate_target_health = alias.value.evaluate_target_health
}
}

depends_on = [var.module_depends_on]
}
19 changes: 19 additions & 0 deletions outputs.tf
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# ------------------------------------------------------------------------------
# OUTPUT CALCULATED VARIABLES (prefer full objects)
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# OUTPUT ALL RESOURCES AS FULL OBJECTS
# ------------------------------------------------------------------------------
output "zone" {
description = "The created Hosted Zone(s)."
value = aws_route53_zone.zone
Expand All @@ -12,3 +19,15 @@ output "delegation_set" {
description = "The outputs of the created delegation set."
value = try(aws_route53_delegation_set.delegation_set[0], null)
}

# ------------------------------------------------------------------------------
# OUTPUT ALL INPUT VARIABLES
# ------------------------------------------------------------------------------

# ------------------------------------------------------------------------------
# OUTPUT MODULE CONFIGURATION
# ------------------------------------------------------------------------------
output "module_enabled" {
description = "Whether the module is enabled"
value = var.module_enabled
}
23 changes: 17 additions & 6 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,6 @@ variable "delegation_set_id" {
default = null
}

variable "enable_module" {
description = "Whether to enable the module and to create the Route53 Zone and it's associated resources."
type = bool
default = true
}

variable "force_destroy" {
description = "Whether to force destroy all records (possibly managed outside of Terraform) in the zone when destroying the zone."
type = bool
Expand Down Expand Up @@ -158,3 +152,20 @@ variable "zone_id" {
#
# zone_id = "zoneid"
}

# ------------------------------------------------------------------------------
# OPTIONAL MODULE CONFIGURATION PARAMETERS
# These variables are used to configure the module.
# See https://medium.com/mineiros/the-ultimate-guide-on-how-to-write-terraform-modules-part-1-81f86d31f024
# ------------------------------------------------------------------------------
variable "module_enabled" {
type = bool
description = "(optional) Whether to create resources within the module or not. Default is true."
default = true
}

variable "module_depends_on" {
type = list(any)
description = "(optional) A list of external resources the module depends_on. Default is []."
default = []
}

0 comments on commit 594a7c5

Please sign in to comment.