I am using terraform version 1.1.0 and AWS provider version. Below the config file I used to declare required providers block.
terraform {
required_providers {
aws = {
version = “>= 3.68.0”
source = “hashicorp/aws”
configuration_aliases = [aws.stage, aws.research]
}
}
}
provider “aws” {
alias = “stage”
region = “us-east-1”
}
We are using terraform modules and for each environment we are declaring the terraform
provider as below and use the provider in the module as below
module “route53_dns_forwarder” {
source = “…/…/TerraformModules/route53_dns_forwarder”
providers = {
aws = aws.stage
}
}
When I try to run the terraform validate I am getting the below warning message.
Module module.stage does not declare a provider named aws. If you wish to specify a provider configuration for the module, add an entry for aws in the required_providers block within the module.
The work arounds we found on google did not work. Any ideas or suggestions on this.
you shoudl add a configuration_aliases in the child module, e.g.
terraform {
required_version = ">= 1.0.0"
required_providers {
aws = {
source = "hashicorp/aws"
version = ">= 3.45.0"
configuration_aliases = [ aws.org ]
}
}
}
Hey @chnaresh26 Like @vikasreddy1697 mentioned above, the configuration_aliases
declaration will need to be within the child module. I recently answered a similar question in an issue on the AWS Provider GitHub repository that you may find helpful:
opened 04:26AM - 14 Jan 22 UTC
question
service/ec2
<!---
Please note the following potential times when an issue might be in Terra… form core:
* [Configuration Language](https://www.terraform.io/docs/configuration/index.html) or resource ordering issues
* [State](https://www.terraform.io/docs/state/index.html) and [State Backend](https://www.terraform.io/docs/backends/index.html) issues
* [Provisioner](https://www.terraform.io/docs/provisioners/index.html) issues
* [Registry](https://registry.terraform.io/) issues
* Spans resources across multiple providers
If you are running into one of these scenarios, we recommend opening an issue in the [Terraform core repository](https://github.com/hashicorp/terraform/) instead.
--->
### Community Note
* Please vote on this issue by adding a 👍 [reaction](https://blog.github.com/2016-03-10-add-reactions-to-pull-requests-issues-and-comments/) to the original issue to help the community and maintainers prioritize this request
* Please do not leave "+1" or other comments that do not add relevant new information or questions, they generate extra noise for issue followers and do not help prioritize the request
* If you are interested in working on this issue or have submitted a pull request, please leave a comment
### Info
* The provider aws is undefined, but I have defined in the `providers.tf`
* Vpc_id `data.aws_vpc.default.id` only get the value from the region `us-east-1`, not `us-west-1`
```
2022-01-14T12:15:51.751+0800 [TRACE] Context.Input: Prompting for provider arguments
2022-01-14T12:15:51.751+0800 [TRACE] Context.Input: Provider provider.aws declared at providers.tf:1,1-15
2022-01-14T12:15:51.751+0800 [TRACE] Context.Input: Provider provider.aws.usw1 declared at providers.tf:7,1-15
2022-01-14T12:15:51.751+0800 [TRACE] Context.Input: Provider provider.terraform implied by data block at data.tf:5,1-45
2022-01-14T12:15:51.752+0800 [TRACE] Context.Input: Input for provider.aws.usw1: map[string]cty.Value{}
2022-01-14T12:15:51.752+0800 [TRACE] Context.Input: No schema available for provider type "terraform"
2022-01-14T12:15:51.752+0800 [TRACE] Context.Input: Input for provider.aws: map[string]cty.Value{}
╷
│ Warning: Provider aws is undefined
│
│ on main.tf line 253, in module "security_group2":
│ 253: aws = aws.usw1
│
│ Module module.security_group2 does not declare a provider named aws.
│ If you wish to specify a provider configuration for the module, add an entry for aws in the required_providers block within the module.
╵
```
### Terraform CLI and Terraform AWS Provider Version
### Affected Resource(s)
aws_security_group
### Terraform Configuration Files
Please include all Terraform configurations required to reproduce the bug. Bug reports without a functional reproduction may be closed without investigation.
backend.tf
```
terraform {
required_version = ">= 0.13"
required_providers {
aws = {
source = "hashicorp/aws"
configuration_aliases = [ aws.usw1 ]
}
}
backend "s3" {
bucket = "terraform"
key = "terraform_prod-sg.tfstate"
region = "ap-southeast-1"
access_key = "xxxxxx"
secret_key = "xxxxxx"
encrypt = "true"
}
}
```
data.tf
```
data "aws_vpc" "default" {
default = true
}
```
main.tf
```
locals {
ingress_rules = {
consul = ["consul-tcp", "consul-webui-tcp", "consul-dns-tcp", "consul-dns-udp"]
consul-2 = ["consul-serf-lan-tcp", "consul-serf-lan-udp", "consul-serf-wan-tcp", "consul-serf-wan-udp"]
demo = ["mongodb-tcp"]
}
ingress_cidr_blocks = {
consul = concat(
["172.31.96.0/22"],
)
consul-2 = concat(
["172.31.96.0/22"],
)
demo = concat(
["172.31.96.0/22"],
)
}
ingress_with_cidr_blocks = {
consul = []
consul-2 = []
demo = []
}
ingress_ipv6_cidr_blocks = {
consul = []
consul-2 = []
demo = []
}
egress_rules = {
consul = []
consul-2 = []
demo = []
}
}
variable "access_key" {
}
variable "secret_key" {
}
variable "user" {
default = "lyc"
}
variable "groups_use1" {
default = [
"consul",
"consul-2",
]
}
variable "groups_usw1" {
default = [
"demo",
]
}
variable "environment" {
default = "prod"
}
variable "region" {
default = {
"consul": "us-east-1",
"consul-2": "us-east-1",
"demo": "us-west-1",
}
}
variable "project" {
default = "app"
}
module "security_group" {
for_each = toset(var.groups_use1)
source = "../../modules/aws-security-group"
service = format("%s-%s", var.project, trimsuffix(each.key, "-2"))
environment = var.environment
region = substr(each.key, -2, -1) == "-2" ? format("%s-%s", var.region[each.key], "2") : var.region[each.key]
description = format("Security group for %s", trimsuffix(each.key, "-2"))
vpc_id = data.aws_vpc.default.id
ingress_rules = local.ingress_rules[each.key]
ingress_cidr_blocks = local.ingress_cidr_blocks[each.key]
ingress_with_cidr_blocks = local.ingress_with_cidr_blocks[each.key]
ingress_ipv6_cidr_blocks = local.ingress_ipv6_cidr_blocks[each.key]
egress_rules = local.egress_rules[each.key]
}
module "security_group2" {
for_each = toset(var.groups_usw1)
source = "../../modules/aws-security-group"
service = format("%s-%s", var.project, trimsuffix(each.key, "-2"))
environment = var.environment
region = substr(each.key, -2, -1) == "-2" ? format("%s-%s", var.region[each.key], "2") : var.region[each.key]
description = format("Security group for %s", trimsuffix(each.key, "-2"))
vpc_id = data.aws_vpc.default.id
ingress_rules = local.ingress_rules[each.key]
ingress_cidr_blocks = local.ingress_cidr_blocks[each.key]
ingress_with_cidr_blocks = local.ingress_with_cidr_blocks[each.key]
ingress_ipv6_cidr_blocks = local.ingress_ipv6_cidr_blocks[each.key]
egress_rules = local.egress_rules[each.key]
providers = {
aws = aws.usw1
}
}
```
providers.tf
```
provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
provider "aws" {
alias = "usw1"
region = "us-west-1"
access_key = var.access_key
secret_key = var.secret_key
}
```
### Debug Output
<!---
Please provide a link to a GitHub Gist containing the complete debug output. Please do NOT paste the debug output in the issue; just paste a link to the Gist.
To obtain the debug output, see the [Terraform documentation on debugging](https://www.terraform.io/docs/internals/debugging.html).
--->
### Panic Output
### Expected Behavior
### Actual Behavior
### Steps to Reproduce
1. `terraform plan`
### Important Factoids
### References
<!---
Information about referencing Github Issues: https://help.github.com/articles/basic-writing-and-formatting-syntax/#referencing-issues-and-pull-requests
Are there any other GitHub issues (open or closed) or pull requests that should be linked here? Vendor documentation? For example:
--->
* #0000
@vikasreddy1697 @justinretzolk Thanks much for your help. We are able to resolve the issue.