Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions README.md

Large diffs are not rendered by default.

31 changes: 30 additions & 1 deletion data.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
locals {
azs = length(var.azs) == 0 ? slice(data.aws_availability_zones.current.names, 0, var.az_count) : var.azs
# az_count has been provided slice based on az_count otherwise return those provided
azs = var.az_count != null ? slice(data.aws_availability_zones.current.names, 0, var.az_count) : data.aws_availability_zones.current.names

# references to module.calculate_subnets output
calculated_subnets = module.calculate_subnets.subnets_by_type
Expand Down Expand Up @@ -123,6 +124,34 @@ data "aws_availability_zones" "current" {
name = "opt-in-status"
values = ["opt-in-not-required"]
}

dynamic "filter" {
for_each = var.azs != null ? [1] : []
content {
name = "zone-name"
values = var.azs
}
}

lifecycle {
# Check at least one variable `var.azs` or `var.az_count` has been provided
precondition {
condition = (var.azs != null && var.az_count != null) ? false : true
error_message = "Both `var.azs` and `var.az_count` provided. You must provide a value for `var.azs` or `var.az_count` but not both."
}

# Check that only `var.azs` or `var.az_count` was provided (and not both)
precondition {
condition = (var.azs == null && var.az_count == null) ? false : true
error_message = "You must provide a value for the variable `var.az_count` or `var.azs`."
}

# Check if `var.azs` was provided that the filter returns the correct number of AZs (i.e. all exist in the current partition/region)
postcondition {
condition = (var.azs == null) ? true : (length(self.names) == length(var.azs) ? true : false)
error_message = "One or more of the Availability Zones provided in `var.azs` does not exist. Please check the availability zones are available in the current region."
}
}
}

# search for existing vpc with var.vpc_id if not creating
Expand Down
6 changes: 6 additions & 0 deletions examples/provide_specific_azs/.header.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Deploy VPC subnets to a specific set of Availability Zones

This example shows how you can use this module to deploy a VPC defining a list of specific Availability Zones where subnets should be deployed. This example creates the following:

* VPC with IPv4 CIDR block named "specific-azs"
* Private subnets deployed in each of the Availability Zones defined in the input variable `azs` with the name prefix "vpc-specific-azs-private" and a /18 netmask
37 changes: 37 additions & 0 deletions examples/provide_specific_azs/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<!-- BEGIN_TF_DOCS -->
# Deploy VPC subnets to a specific set of Availability Zones

This example shows how you can use this module to deploy a VPC defining a list of specific Availability Zones where subnets should be deployed. This example creates the following:

* VPC with IPv4 CIDR block named "specific-azs"
* Private subnets deployed in each of the Availability Zones defined in the input variable `azs` with the name prefix "vpc-specific-azs-private" and a /18 netmask

## Requirements

No requirements.

## Providers

No providers.

## Modules

| Name | Source | Version |
|------|--------|---------|
| <a name="module_vpc_specific_azs"></a> [vpc\_specific\_azs](#module\_vpc\_specific\_azs) | ../.. | n/a |

## Resources

No resources.

## Inputs

| Name | Description | Type | Default | Required |
|------|-------------|------|---------|:--------:|
| <a name="input_azs"></a> [azs](#input\_azs) | A list of AZs to use. e.g. `azs = ["us-east-1a","us-east-1c"]` Incompatible with `az_count` | `list(string)` | <pre>[<br> "us-east-1a",<br> "us-east-1c"<br>]</pre> | no |
| <a name="input_cidr_block"></a> [cidr\_block](#input\_cidr\_block) | IPv4 CIDR range to assign to VPC if creating VPC or to associate as a secondary IPv6 CIDR. Overridden by var.vpc\_id output from data.aws\_vpc. | `string` | `"10.0.0.0/16"` | no |

## Outputs

No outputs.
<!-- END_TF_DOCS -->
17 changes: 17 additions & 0 deletions examples/provide_specific_azs/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module "vpc_specific_azs" {
# source = "aws-ia/vpc/aws"
# version = ">= 4.3.1"
source = "../.."

name = "specific-azs"
cidr_block = var.cidr_block
azs = var.azs


subnets = {
private = {
name_prefix = "vpc-specific-azs-private"
netmask = 18
}
}
}
Empty file.
15 changes: 15 additions & 0 deletions examples/provide_specific_azs/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
variable "azs" {
type = list(string)
description = "A list of AZs to use. e.g. `azs = [\"us-east-1a\",\"us-east-1c\"]` Incompatible with `az_count`"
default = [
"us-east-1a",
"us-east-1c",
]
}

variable "cidr_block" {
type = string
description = "IPv4 CIDR range to assign to VPC if creating VPC or to associate as a secondary IPv6 CIDR. Overridden by var.vpc_id output from data.aws_vpc."
default = "10.0.0.0/16"
}

17 changes: 17 additions & 0 deletions test/examples_provide_specific_azs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package test

import (
"testing"

"github.com/gruntwork-io/terratest/modules/terraform"
)

func TestExamplesSpecificAzs(t *testing.T) {

terraformOptions := &terraform.Options{
TerraformDir: "../examples/provide_specific_azs",
}

defer terraform.Destroy(t, terraformOptions)
terraform.InitAndApply(t, terraformOptions)
}
10 changes: 8 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,14 @@ variable "create_vpc" {

variable "az_count" {
type = number
default = 0
description = "Searches region for # of AZs to use and takes a slice based on count. Assume slice is sorted a-z."
description = "Searches region for # of AZs to use and takes a slice based on count. Assume slice is sorted a-z. Required if `azs` is not provided."
default = null
}

variable "azs" {
type = list(string)
description = "(Optional) A list of AZs to use. e.g. `azs = [\"us-east-1a\",\"us-east-1c\"]` Incompatible with `az_count`"
default = null
}

variable "azs" {
Expand Down