Is there a way to retrieve data from another Account in Terraform?

Viewed 1624

I want to use the resource "data" in Terraform for example for an sns topic but I don't want too look for a resource in the aws-account, for which I'm deploying my other resources. It should look up to my other aws-account (in the same organization) and find resources in there. Is there a way to make this happen?

data "aws_sns_topic" "topic_alarms_data" {
  name = "topic_alarms"
}
1 Answers

Define an aws provider with credentials to the remote account:

# Default provider that you use:
provider "aws" {
  region = var.context.aws_region
  assume_role {
    role_arn = format("arn:aws:iam::%s:role/TerraformRole", var.account_id)
  }
}

provider "aws" {
  alias = "remote"
  region = var.context.aws_region
  assume_role {
    role_arn = format("arn:aws:iam::%s:role/TerraformRole", var.remote_account_id)
  }
}

data "aws_sns_topic" "topic_alarms_data" {
  provider = aws.remote
  name     = "topic_alarms"
}

Now the topics are loaded from the second provider.

Related