Terraform: Create an SNS topic for SES in a different region

Published on

Last Updated on

Estimated Reading Time: 1 min

Let us assume that most of our infrastructure is in ap-southeast-2 (Sydney). But, our Amazon Simple Email Service(SES) is set up in us-west-2 (Oregon). We also want to create Amazon Simple Notification Service(SNS) topics, and Amazon Simple Queue Service (SQS) queues to handle bounce notifications.

Since SES is in a different region, our SNS topics also need to be in that region. So how do we tell terraform to create the topics in the SES region?

Solution

Step 1

Create providers.

provider "aws" {
  region = "ap-southeast-2"
}

provider "aws" {
  region = "us-west-2"
  alias  = "ses_aws"
}
  • ap-southeast-2: The default provider configuration.
  • us-west-2: This is the additional provider configuration. Setting the Alias allows us to create more than 1 provider.

Step 2

For the resources we want to use the us-west-2 region, we specify the provider parameter and set it equal to the alias.

If a provider parameter is not specified, it will fall back to using the provider that doesn't have an alias.

resource "aws_sqs_queue" "example-queue" {
  name                      = "example_queue"
}

resource "aws_sns_topic" "example-topic" {
  name     = "example_topic"
  provider = aws.ses_aws
}

In the above snippet, example-queue will be created in ap-southeast-2 but, example-topic will be created in us-west-2.

Conclusion

We can change the region by creating additional providers and specifying the provider parameter in a resource.