使用Terraform v1.2.5,我正在尝试部署一个AWS VPC Peer。但是,以下代码验证失败:
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "~> 4.1"
}
}
}
provider "aws" {
region = "us-east-1"
}
data "aws_vpc" "accepter" {
provider = aws.accepter
id = "${var.accepter_vpc_id}"
}
locals {
accepter_account_id = "${element(split(":", data.aws_vpc.accepter.arn), 4)}"
}
resource "aws_vpc_peering_connection" "requester" {
description = "peer_to_${var.accepter_profile}"
vpc_id = "{$var.requester_vpc_id}"
peer_vpc_id = "${data.aws_vpc.accepter.id}"
peer_owner_id = "${local.accepter_account_id}"
}
当验证这个地形代码时,我收到以下错误:
$ terraform validate
╷
│ Error: Provider configuration not present
│
│ To work with data.aws_vpc.accepter its original provider configuration at provider["registry.terraform.io/hashicorp/aws"].accepter is
│ required, but it has been removed. This occurs when a provider configuration is removed while objects created by that provider still exist
│ in the state. Re-add the provider configuration to destroy data.aws_vpc.accepter, after which you can remove the provider configuration
│ again.
我缺少什么或错误配置导致此错误?
您需要提供此处引用的别名提供程序版本:
data "aws_vpc" "accepter" {
provider = aws.accepter # <--- missing aliased provider
id = var.accepter_vpc_id
}
要解决这个问题,您只需要添加相应的别名提供商块[1]:
provider "aws" {
alias = "accepter"
region = "us-east-1" # make sure the region is right
}
[1] https://developer.hashicorp.com/terraform/language/providers/configuration alias-multiple-provider-configurations
由于VPC对等连接是"区域间"的;对等我们需要2个AWS区域。因此,实现这一目标的方法是创建AWS提供商和AWS别名提供商块。
参见示例:
provider "aws" {
region = "us-east-1"
# Requester's credentials.
}
provider "aws" {
alias = "peer"
region = "us-west-2"
# Accepter's credentials.
}
您的资源不知道使用哪个提供程序,您提供的提供程序不存在。
你有两个选择:
- 为provider添加别名:
provider "aws" {
region = "us-east-1"
alias = "accepter"
}
- 从
data "aws_vpc" "accepter"
中删除provider = aws.accepter
不知何故,我想第一个对你更有效。