Creating a SNS topic for a SES not in the default provider

This entry was posted on
  • terraform
  • devops
  • aws

Lets assume that most of our infrastructure is in ap-southeast-2 (Sydney). But, our SES is setup in us-west-2 (Oregon). We also want to create SNS topics and SQS queues so that we can handle bounce notifications. Now 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 providers.

Step 2

For the resources, we want to use the us-west-2 region we specify the provider parameter and set it to be 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

By creating additional providers and specifiying the provider parameter in a resource, we can change its region.

More like this