This template sets up a multi-region AWS Managed Microsoft AD with primary and secondary configurations including VPCs and subnets.

Terraform Template

data "aws_availability_zones" "available" {

  filter {
    name = "opt-in-status"
    values = ["opt-in-not-required"]
  }
  state = "available"
}

data "aws_availability_zones" "available-secondary" {

  filter {
    name = "opt-in-status"
    values = ["opt-in-not-required"]
  }
  provider = "aws.secondary"
  state = "available"
}

data "aws_region" "example" {
  provider = "aws.secondary"
}

resource "aws_directory_service_directory" "example" {
  name = "example.com"
  password = "SuperSecretPassw0rd"
  type = "MicrosoftAD"

  vpc_settings {
    subnet_ids = aws_subnet.example[*].id
    vpc_id = aws_vpc.example.id
  }
}

resource "aws_directory_service_region" "example" {
  directory_id = aws_directory_service_directory.example.id
  region_name = data.aws_region.example.name

  tags = {
    Name = "Secondary"
  }

  vpc_settings {
    subnet_ids = aws_subnet.example-secondary[*].id
    vpc_id = aws_vpc.example-secondary.id
  }
}

resource "aws_subnet" "example" {
  availability_zone = "data.aws_availability_zones.available.names[count.index]"
  cidr_block = cidrsubnet(aws_vpc.example.cidr_block, 8, count.index)
  count = 2

  tags = {
    Name = "Primary"
  }
  vpc_id = aws_vpc.example.id
}

resource "aws_subnet" "example-secondary" {
  availability_zone = "data.aws_availability_zones.available-secondary.names[count.index]"
  cidr_block = cidrsubnet(aws_vpc.example-secondary.cidr_block, 8, count.index)
  count = 2
  provider = "aws.secondary"

  tags = {
    Name = "Secondary"
  }
  vpc_id = aws_vpc.example-secondary.id
}

resource "aws_vpc" "example" {
  cidr_block = "10.0.0.0/16"

  tags = {
    Name = "Primary"
  }
}

resource "aws_vpc" "example-secondary" {
  cidr_block = "10.1.0.0/16"
  provider = "aws.secondary"

  tags = {
    Name = "Secondary"
  }
}