【问题标题】:Call provider in a module调用模块中的提供者
【发布时间】:2021-10-29 10:14:33
【问题描述】:

在我的模块中调用提供程序时遇到一些问题,我需要在我的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-services terraform


    【解决方案1】:

    整个模块的syntax to specify a provider 与为资源指定提供者略有不同。这是因为一个模块可能会声明使用多个提供者的多个资源,因此您提供一个提供者映射到一个模块。您的代码应如下所示:

    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
    }
    

    【讨论】:

    • 警告:提供者 aws 未定义│ │ 在 main.tf 第 6 行,模块“networking”中:│ 6: aws = aws.first
    • 我认为您需要在模块中添加一个provider 块。它可以是一个最小的块、空的或只有一个版本要求。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-03-10
    • 2019-07-03
    • 2019-04-04
    • 2011-12-09
    • 2018-09-05
    • 2020-12-01
    • 2018-12-21
    相关资源
    最近更新 更多