Call provider in a module

Viewed 654

Having some issues calling the provider in my modules, do i need to variable providers in my networking/main.tf ?

Do i just state in my main.tf when calling a module that does not seem to work.

Any help would be great, i want to modulate my work and need to use multple aws accounts and regions. TGW etc

# --- root/main.tf ---

 module "networking" {
 source           = "./networking"
 provider         = aws.first
 vpc_cidr         = local.vpc_cidr1
 access_ip        = var.access_ip
 security_groups  = local.security_groups
 public_sn_count  = 2
 private_sn_count = 3
 max_subnets      = 20
 public_cidrs     = [for i in range(2, 255, 2) : cidrsubnet(local.vpc_cidr1, 8, i)]
 private_cidrs    = [for i in range(1, 255, 2) : cidrsubnet(local.vpc_cidr1, 8, i)]
 nat_gw_eip_count = 0
 nat_gw_count     = 0
}

terraform {
required_version = ">= 0.12"   
}


# Defines the 1st account provider.
 provider "aws" {
  alias = "first"

  region     = var.aws_first_region
  access_key = var.aws_first_access_key
  secret_key = var.aws_first_secret_key
}

 # Defines the 2nd account provider.
 provider "aws" {
 alias = "second"

 region     = var.aws_second_region
 access_key = var.aws_second_access_key
 secret_key = var.aws_second_secret_key
}

data "aws_caller_identity" "first" {
provider = aws.first
}

data "aws_caller_identity" "second" {
provider = aws.second
}

data "aws_caller_identity" "third" {
provider = aws.third
}

# --- networking/main.tf --- 
data "aws_availability_zones" "available" {}

resource "random_integer" "random" {
 min = 1
 max = 100
}

resource "random_shuffle" "public_az" {
 input        = data.aws_availability_zones.available.names
 result_count = var.max_subnets
}

resource "aws_vpc" "wash_VPC" {
 cidr_block           = var.vpc_cidr
 enable_dns_hostnames = true
 enable_dns_support   = true

  tags = {
   Name = "wash_VPC-${random_integer.random.id}"
  } 

  lifecycle {
   create_before_destroy = true
  }
 }

Here is the error what else would have instead?

│ Error: Unsupported argument
│ 
│   on main.tf line 5, in module "networking":
│    5:   provider         = aws.first
│ 
│ An argument named "provider" is not expected her
1 Answers

The syntax to specify a provider for an entire module is slightly different than specifying a provider for a resource. This is because a module may be declaring multiple resources that use multiple providers, so you provide a map of providers to a module. You code should look like this:

module "networking" {
 source           = "./networking"
 providers = {
   aws = aws.first
 }
 vpc_cidr         = local.vpc_cidr1
 access_ip        = var.access_ip
 security_groups  = local.security_groups
 public_sn_count  = 2
 private_sn_count = 3
 max_subnets      = 20
 public_cidrs     = [for i in range(2, 255, 2) : cidrsubnet(local.vpc_cidr1, 8, i)]
 private_cidrs    = [for i in range(1, 255, 2) : cidrsubnet(local.vpc_cidr1, 8, i)]
 nat_gw_eip_count = 0
 nat_gw_count     = 0
}
Related