使用Terraform部署到多个AWS账户吗? [英] Deploying to multiple AWS accounts with Terraform?

查看:115
本文介绍了使用Terraform部署到多个AWS账户吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在寻找一种方法,能够在Terraform中同时部署到多个AWS账户并干dry. AWS具有使用Stacks进行此操作的概念,但是我不确定Terraform中是否可以执行此操作?如果是这样,有什么解决方案吗?

I've been looking for a way to be able to deploy to multiple AWS accounts simultaneously in Terraform and coming up dry. AWS has the concept of doing this with Stacks but I'm not sure if there is a way to do this in Terraform? If so what would be some solutions?

您可以阅读有关Cloudformation解决方案的更多信息这里.

You can read more about the Cloudformation solution here.

推荐答案

您可以定义多个提供商别名,可用于在不同区域甚至不同的AWS账户中运行操作.

You can define multiple provider aliases which can be used to run actions in different regions or even different AWS accounts.

因此要在您的默认区域(如果未在环境变量/~/.aws/configure/etc中定义,则提示您执行某些操作)以及在美国东部1中执行以下操作:

So to perform some actions in your default region (or be prompted for it if not defined in environment variables/~/.aws/configure/etc) and also in US East 1 you'd have something like this:

provider "aws" {
  # ...
}

# Cloudfront ACM certs must exist in US-East-1
provider "aws" {
  alias  = "cloudfront-acm-certs"
  region = "us-east-1"
}

然后您将这样引用它们:

You'd then refer to them like so:

data "aws_acm_certificate" "ssl_certificate" {
  provider    = "aws.cloudfront-acm-certs"
  ...
}

resource "aws_cloudfront_distribution" "cloudfront" {
  ...
  viewer_certificate {
    acm_certificate_arn = "${data.aws_acm_certificate.ssl_certificate.arn}"
    ...
  }
}

因此,如果您想同时在多个帐户中进行操作,则可以

So if you want to do things across multiple accounts at the same time then you could assume a role in the other account with something like this:

provider "aws" {
  # ...
}

# Assume a role in the DNS account so we can add records in the zone that lives there
provider "aws" {
  alias   = "dns"
  assume_role {
    role_arn     = "arn:aws:iam::ACCOUNT_ID:role/ROLE_NAME"
    session_name = "SESSION_NAME"
    external_id  = "EXTERNAL_ID"
  }
}

并像这样引用它:

data "aws_route53_zone" "selected" {
  provider     = "aws.dns"
  name         = "test.com."
}

resource "aws_route53_record" "www" {
  provider = "aws.dns"
  zone_id  = "${data.aws_route53_zone.selected.zone_id}"
  name     = "www.${data.aws_route53_zone.selected.name}"
  ...
}

或者,您可以通过其他多种方式为不同的AWS账户提供凭证,例如在提供程序中对它们进行硬编码或使用其他Terraform变量 AWS SDK特定环境变量或使用配置的配置文件.

Alternatively you can provide credentials for different AWS accounts in a number of other ways such as hardcoding them in the provider or using different Terraform variables, AWS SDK specific environment variables or by using a configured profile.

这篇关于使用Terraform部署到多个AWS账户吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆