can terraform be used simply to create resources in different AWS regions?
Asked Answered
I

2

10

I have the following deploy.tf file:

provider "aws" {
  region          = "us-east-1"
}

provider "aws" {
  alias           = "us_west_1"
  region          = "us-west-2"
}

resource "aws_us_east_1" "my_test" {
  # provider        = "aws.us_east_1"
  count           = 1
  ami             = "ami-0820..."
  instance_type   = "t2.micro"
}

resource "aws_us_west_1" "my_test" {
  provider        = "aws.us_west_1"
  count           = 1
  ami             = "ami-0d74..."
  instance_type   = "t2.micro"
}

I am trying to use it to deploy 2 servers, one in each region. I keep getting errors like:

aws_us_east_1.narc_test: Provider doesn't support resource: aws_us_east_1

I have tried setting alias's for both provider blocks, and referring to the correct region in a number of different ways. I've read up on multi region support, and some answers suggest this can be accomplished with modules, however, this is a simple test, and I'd like to keep it simple. Is this currently possible?

Inspirational answered 30/12, 2018 at 21:1 Comment(2)
As mentioned by @p0fi you've just tried to create a resource of type aws_us_west_1 instead of an aws_instance resource. Otherwise your example should be fine. As such I'm voting to close as a typo/basic mistake because it looks like you otherwise had this covered.Southing
I agree it should be closed - I would have done it myself, except, I wasn't sure if @p0fi would lose the rep for helping me, which would be a most unwanted side effect...Inspirational
Z
13

Yes it can be used to create resources in different regions even inside just one file. There is no need to use modules for your test scenario.

Your error is caused by a typo probably. If you want to launch an ec2 instance the resource you wanna create is aws_instance and not aws_us_west_1 or aws_us_east_1.

Sure enough Terraform does not know this kind of resource since it does simply not exist. Change it to aws_instance and you should be good to go! Additionally you should probably name them differently to avoid double naming using my_test for both resources.

Zymogenic answered 31/12, 2018 at 0:34 Comment(2)
thanks, nice spot - must have been bleary eyed last night. btw, I ended up using modules - it was super easy after following the pattern here: github.com/hashicorp/terraform/issues/…Inspirational
Terraform 0.11 and earlier required quotes, but quoted references are now deprecated. You should not use quote in the provider. Use provider= aws.us_west_1Noheminoil
B
7

Step 1

Add region alias in the main.tf file where you gonna execute the terraform plan.

provider "aws" {
  region = "eu-west-1"
  alias = "main"
}
    
provider "aws" {
  region = "us-east-1"
  alias = "useast1"
}

Step 2

Add providers block inside your module definition block

module "lambda_edge_rule" {
  providers = {
    aws = aws.useast1
  }
  source            = "../../../terraform_modules/lambda"
  tags              = var.tags
}

Step 3

Define "aws" as providers inside your module. ( source = ../../../terraform_modules/lambda")

terraform {
    required_providers {
    aws = {
          source  = "hashicorp/aws"
          version = ">= 2.7.0"
        }
      }
    }
    
resource "aws_lambda_function" "lambda" {
  function_name     = "blablabla"
    .
    .
    .
    .
    .
    .
    .
}

Note: Terraform version v1.0.5 as of now.

Barite answered 28/8, 2021 at 13:57 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.