I have data source module where I am extracting the subnet id by passing subnet name.
File is in the data module:
Module/data/data.tf
data "aws_vpc" "vpc-id" {
provider = "${var.region}"
filter {
name = "tag:Name"
values = ["kdt-vpc"]
}
}
data "aws_subnet" "subnetid" {
provider = "${var.region}"
filter {
name = "vpc-id"
values = [data.aws_vpc.vpc-id.id]
}
filter {
name = "tag:Name"
values = ["kdt-private-subnet-0"]
}
}
Variables.tf
variable "region" {
description = "vpc name"
type = string
default = "aws.west"
}
main.tf where Iam calling the data module.
module "data" {
source = "./data"
region = "aws.west"
}
output "subnet_id" {
value = module.data.subnetid
}
Provider.tf
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
#version = "~> 3.0"
#configuration_aliases = [ aws.alternate ]
}
}
}
provider "aws" {
region = "us-east-1"
}
provider "aws" {
alias = "west"
region = "us-east-2"
}
While running the terraform apply i am getting the below errors
Error: Invalid provider configuration reference
│
│ on data\data.tf line 2, in data "aws_vpc" "vpc-id":
│ 2: provider = "${var.region}"
│
│ The provider argument requires a provider type name, optionally followed by a period and then a
│ configuration alias.
╵
╷
│ Error: Invalid provider configuration reference
│
│ on data\data.tf line 11, in data "aws_subnet" "subnetid":
│ 11: provider = "${var.region}"
│
│ The provider argument requires a provider type name, optionally followed by a period and then a
│ configuration alias.
╵
Error: Unsupported attribute │ │ on main.tf line 18, in output "subnet_id": │ 18: value = module.data.subnetid │ ├──────────────── │ │ module.data is a object │ │ This object does not have an attribute named "subnetid".
Instead of passing a region name to the module, and then trying to use a specific provider based on that, you need to actually pass the provider itself into the module (documented here):
module "data" {
source = "./data"
providers = {
aws = aws.west
}
}
Then just completely remove the provider = "${var.region}"
lines inside the data module code.