How to create two s3 buckets in 2 different regions at the same time via terraform

Below is my provider.tf

terraform {
required_version = “>= 0.13”
required_providers {
aws = {
source = “hashicorp/aws”
version = “~> 2.70”
}
}
}

provider “aws” {
region = “eu-west-1”
alias = “ireland”
}

provider “aws” {
region = “us-east-2”
alias = “ohio”
}

Below is my main.tf

resource “aws_s3_bucket” “provider” {
for_each = var.regions
provider = “aws.{each.key}" bucket = "{var.bucket}-${each.value}”
}

I know that provider fails if we put double quotes as above. But i dont want to put 2 resource blocks for 2 providers to create 2 S3 buckets as its not a scalable solution.

i have my terraform.tfvars file as below

regions = {
ireland = “eu-west-1”,
ohio = “us-east-2”
}
bucket = “test-provider”

Please help how we can create two s3 buckets in two different regions at the same time without repeating the code

Regards,
Pranav

@pnateri,

Currently resources with separate providers must be declared independently, so there is no way around having multiple blocks for the two s3 buckets. If you have many resources duplicated across different provider configurations, you can group them together in a module, and pass the desired provider configurations into multiple instances of the same module. This way you can declare the resource only once within the module, and only the module block is repeated.

@jbardin did you mean something like below?

module “test-provider” {
source = “…/…/src/”
bucket = var.bucket
providers = {
aws = aws.ireland
}
}

module “test-provider-ohio” {
source = “…/…/src/”
bucket = var.bucket
providers = {
aws = aws.ohio
}
}

So my whole requirement, i do not want to create the s3 bucket one after the other in two different regions. In other words, i do not want to run terraform apply twice for each of the modules. I wanted to run terraform apply once and want to see the buckets getting created in both the regions simultaneously.

so if i have two modules repeated as above and if have the corresponding tfvars file viz ohio.tfvars and ireland.tfvars, how can i run the whole terraform with one single terraform apply command?
I mean should we have to do terraform apply -var-file=ohio.tfvars and terraform apply -var-file=ireland.tfvars? or is there a better way where we can create the same infra in different regions with one single terraform apply command?

I tried using terragrunt, it creates one after the other too. however, it gives errors if i use terragrunt destroy.

So i wanted is to know, can we create resources in multiple regions using terraform simultaneously with one terraform apply command. If yes, can you please help me with a sample code?

Regards,
Pranav

What you are showing here will create both buckets concurrently, so I’m not sure why you think you need to run Terraform multiple times. Can you show an example of the problem you have with this configuration?

Ok Let me show you the problem. I wanted to just test this concurrency of resource creation in multiple regions. Hence i started off with simply creating two s3 buckets in ireland and ohio region. Below are my configuration files

main.tf:

resource “aws_s3_bucket” “provider” {
bucket = var.bucket
}

provider.tf:

terraform {
required_version = “>= 0.13”
required_providers {
aws = {
source = “hashicorp/aws”
version = “>= 2.7.0”
}
}
}

provider “aws” {
region = “eu-west-1”
alias = “ireland”
}

provider “aws” {
region = “us-east-2”
alias = “ohio”
}

module.tf

module “test-provider” {
source = “…/…/src/”
bucket = var.bucket
providers = {
aws = aws.ireland
}
}

module “test-provider-ohio” {
source = “…/…/src/”
bucket = var.bucket
providers = {
aws = aws.ohio
}
}

terraform.tfvars:

bucket = “test-provider”

variables.tf
variable “bucket” {}

My terraform version

pnateri@C02G70NAMD6M stage % terraform --version
Terraform v1.0.7
on darwin_amd64

Now, when i run terraform apply, i am seeing the below error

Error: missing provider provider[“registry.terraform.io/hashicorp/aws”].ireland

Could you please try reproducing these configurations and help resolve? or let me the where is the miss?

Regards,
Pranav

Sorry @pnateri, I’m not sure how you are reaching that error once the configuration has been validated. It would help to have a complete standalone example to reproduce. I have a feeling there is something wrong with the provider configuration within the module, but I’m not exactly sure what that could be. It would also help greatly if the source was formatted properly so that it can be copied directly without errors from the post.

Hi @jbardin … I have attached the whole folder structure via a bitbucket link. Please check.
Just an FYI. terraform validate command still gives me the same error

https://bitbucket.org/expressplayopsteam/test-provider/src/master/

i hope this should help and unblock you

Regards,
Pranav

@pnateri, the error is because the root module is referencing the provider aws.ireland, but there is no configuration for aws.ireland in the root module. All provider configuration should be done within the root module, and then passed into the individual sub-modules.

See Providers Within Modules - Configuration Language - Terraform by HashiCorp for more details

@jbardin why dont you please tell me exactly what i should tweak in my module repo that i sent?

i have already seen the doc that you sent in the process of troubleshooting

Regards,
Pranav

Hi @pnateri,

You need to move the aws.ireland and aws.ohio configurations into the root module. This is where you are referencing them to pass into the test-provider module instances. Since you are passing these configurations into the module simply as aws, within the module the default provider name can be used.

Okay thanks @jbardin … it worked

Regards,
Pranav