How to create modules that support multiple AWS regions?

We are trying to create Terraform modules for the next steps in AWS so that we can use them where needed.

  • Create VPC
  • Create Subnets
  • Create instance, etc.

But when creating these modules, we must determine the supplier in all of the above modules. Therefore, we decided to create another module for the provider so that we can call this provider module in other modules (VPC, Subnet, etc.).

The problem with the above approach is that it does not accept the value of the provider and asks for user input for the region.

Terraform configuration is as follows:

$ HOME / modules / providers / main.tf

provider "aws" {
  region = "${var.region}"
}

$ HOME / modules / providers / variables.tf

variable "region" {}

$ HOME / modules / VPC / main.tf

module "provider" {
  source = "../../modules/providers"
  region = "${var.region}"
}

resource "aws_vpc" "vpc" {
  cidr_block = "${var.vpc_cidr}"
  tags = {
    "name" = "${var.environment}_McD_VPC"
  }
}

$ HOME / modules / VPC / variables.tf

variable "vpc_cidr" {}
variable "environment" {}
variable "region" {}

$ HOME / main.tf

module "dev_vpc" {
  source = "modules/vpc"
  vpc_cidr = "${var.vpc_cidr}"
  environment = "${var.environment}"
  region = "${var.region}"
}

$HOME/variables.tf

variable "vpc_cidr" {
  default = "192.168.0.0/16"
}

variable "environment" {
  default = "dev"
}

variable "region" {
  default = "ap-south-1"
}

terraform plan $HOME/ .

Terraform, :

  • Wrap Terraform
  • .
+6
1

, , Terraform , , - , .

, Terraform 0.8, :

module "network" {
  # ...
}

resource "aws_instance" "foo" {
  # ...

  depends_on = ["module.network"]
}

, , modules/vpc/main.tf, :

module "aws_provider" {
  source = "../../modules/providers"
  region = "${var.region}"
}

resource "aws_vpc" "vpc" {
  cidr_block = "${var.vpc_cidr}"
  tags = {
    "name" = "${var.environment}_McD_VPC"
  }
  depends_on = ["module.aws_provider"]
}

terraform graph | dot -Tpng > graph.png , , , .

, Terraform, , , issue, , , .

Terraform, , Terraform -, .

.tf (, environment.tf) , , - Terraform ( ), . , .

+5

Source: https://habr.com/ru/post/1016926/


All Articles