首页 > 解决方案 > 调用模块中的提供者

问题描述

在我的模块中调用提供程序时遇到一些问题,我是否需要在我的networking/main.tf 中更改提供程序?

调用似乎不起作用的模块时,我是否只是在 main.tf 中声明。

任何帮助都会很棒,我想调整我的工作并且需要使用多个 aws 帐户和区域。TGW等

# --- 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
  }
 }

这是错误还有什么?

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

标签: amazon-web-servicesterraform

解决方案


为整个模块指定提供者的语法与为资源指定提供者略有不同。这是因为一个模块可能声明了使用多个提供程序的多个资源,因此您提供了一个模块的提供程序映射。您的代码应如下所示:

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
}

推荐阅读