Terraform如何知道应用更改的AWS帐户?

huangapple go评论52阅读模式
英文:

How does Terraform knows which AWS account to apply the changes

问题

从Terraform GCP迁移到AWS,我想知道Terraform如何知道要应用更改的AWS账户?在GCP中,您可以为资源/提供者分配一个项目ID,以便我知道Terraform将应用更改的确切项目。

为了为AWS设置Terraform,计划使用"假定角色"。这是否是设置多个账户的最佳实践?(我们只有2个账户,理想情况下,我认为最好有一个单独的dev-ops账户)

  1. 在开发和生产账户中创建一个terraform-role
  2. 在开发账户中创建一个用户infra-user
  3. 在开发/生产账户和用户infra-user之间设置信任策略。
  4. 在Terraform AWS假定角色中使用变量来切换开发/生产。

我猜想当Terraform假定角色时,它还会假定在那个账户中创建资源?

英文:

Coming from Terraform GCP to AWS, I am wondering how does Terraform knows which AWS account to apply the changes to? In GCP, you can assign a project id to the resource/provider so that i know exactly which project Terraform will apply the changes to.

For setting up Terraform for AWS, the plan is to use assume role. Is this the best practice to setup multiple accounts? (We only have 2 account, ideally i think it is better to have a separate dev-ops account )

  1. Create a terraform-role in both dev and prod account.
  2. Create a user infra-user in dev account.
  3. Setup trust policy between terraform-role in dev/prod account and user infra-user
  4. Use variable to toggle between dev/prod in the Terraform AWS assume role.

I am guessing when Terraform assumes the role, it also assumes to create the resources in that account?

答案1

得分: 1

在AWS中,操作的目标帐户始终由发出API调用的主体(用户或角色)隐含表示。如果您使用sts:AssumeRole(或类似的方法)然后使用生成的动态凭证进行API调用,那么您将会针对目标角色所属的帐户发出请求,而不是针对您最初的主体(假定角色的主体)所属的帐户。

AWS的API通常不会将帐户ID作为请求的一部分,因此没有直接类似于在GCP中将每个对象与项目关联的方式。这更多地是AWS本身的设计细节,而不是Terraform的hashicorp/aws提供程序的设计细节。

然而,您可以通过在provider "aws"块中设置allowed_account_ids参数来帮助确保您的帐户ID保持有序。如果设置了该参数,在进行任何其他请求之前,AWS提供程序将调用sts:GetCallerIdentity 并确保响应中指示的Accountallowed_account_ids中的一个匹配,如果不匹配,则返回错误。

英文:

In AWS, the target account for operations is always implied by the principal (user or role) that's making the API call. If you use sts:AssumeRole (or similar) and then make API calls with the resulting dynamic credentials then you'll be making requests against the account that the target role belongs to, rather than as the account that your original principal (the one that assumed the role) belongs to.

AWS APIs don't typically take an account ID as part of the request, so there isn't any direct analog to associating each object with a project in GCP. This is more a design detail of AWS itself than of the hashicorp/aws provider for Terraform.

However, you can help ensure that you keep your account IDs in order by setting the allowed_account_ids argument in your provider "aws" block(s). If you set that then before making any other requests the AWS provider will call sts:GetCallerIdentity and make sure that the Account indicated in the response matches one of those given in allowed_account_ids, returning an error if not.

huangapple
  • 本文由 发表于 2023年6月12日 23:19:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76458070.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定