-
Notifications
You must be signed in to change notification settings - Fork 9.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
terraform env command doesn't work with different AWS account ID that needs 2 IAM roles #13700
Comments
Hi @jrlonan, This is working as expected right now, though I think we can handle a similar workflow with some manual intervention. I'll work on an example with the new backend system. I think we will want to at least have a documented way to access different environments with different credentials, even if it still requires the user to properly setup the S3 bucket policies. |
@jbardin By the way, is there any way to fix the terraform |
Yes, besides being able to write to the corresponding state file, the credentials you're operating with will need to be able to list all keys in the bucket (or at least keys prefixed with You can't use environments if you're going to try and reconfigure the backend between switching your logical "environments" locally. The point of the When you ran |
@jbardin as for the sake of this use case.
do you see any drawbacks with the mentioned approach ? |
Off the top of my head, I think that would work as long as you don't migrate the states when switching. It might be good to just remove I don't like the idea of having to recommend users touch the I mentioned it in an unrelated issue, but I was thinking about a flag like |
I'm doing this as follows: I have an AWS organisation, with multiple accounts. I always authenticate with the AWS credentials of the root account. That account can So, given: variable "account_ids" {
description = "Terraform state environments mapped to the target account ID"
type = "map"
default = {
"dev" = "111111111111"
"staging" = "222222222222"
"prod" = "333333333333"
}
} provider "aws" {
allowed_account_ids = ["${lookup(var.account_ids, terraform.env)}"]
region = "${var.region}"
assume_role {
role_arn = "arn:aws:iam::${lookup(var.account_ids, terraform.env)}:role/OrganizationAccountAccessRole"
}
}
... then
This will plan/apply into the correct AWS account by assuming the correct role, and will nicely fail if you have forgotten to select the |
Since the release of workspace for the new backend has this been addressed? Please provide documentation on the correct way to use workspaces around separate aws accounts or the work around as I seem to can't find it anywhere. Thank you. |
Are there any plans to support this? An ideal solution would allow the usage of multiple accounts and allow the state to be stored in different buckets depending on the account. That way you can keep your dev/stg/prd states in your dev/stg/accounts. |
Did anyone find a recommended way to do this with workspaces? |
I don't think this works back as far as Terraform 0.9, but recent versions have direct support for assuming roles, so you can specify the The S3 backend docs have a guide on setting up cross-account access which shows one way to do this using Workspaces are generally not the best way to separate different environments. They work better for creating temporary separate deployments for development/testing purposes. To fully isolate your environments, it's better to instead have a separate root configuration for each and use modules to define the common elements. The various separate root modules then give you a proper place to keep the various differences between environments, such as different backend configuration. |
@apparentlymart Thank you for this, I find this concept very interesting and will go to read about modules. The headline seems relevant in as much as it says:
I presume in my case the "reuse" part would be what I'm after. e.g. it might not be best practice, but I could create a single module called "infrastructure" which I can "reuse" in each environment To make sure I understand correctly though, are modules more suitable than workspaces even in the condition that the infrastructure being managed is 100% identical? In my specific case it would seem that workspaces fit my needs all apart from being able to "tie" a workspace to a particular AWS account. I touched on this here and with you being a contributor would love your input on that stackoverflow question. |
Hi @davidgoate! I wrote a more lengthy comment on a similar topic over in #18632 recently. I think that answers the questions you asked here. |
As the |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. |
Summary
Configuring terraform env with 2 AWS account ID (1 staging env account, 1 production env account) does not work.
Terraform Version
0.9.3
Affected Resource(s)
terraform env
Expected Behavior
terraform env
should be able to be used with 2 AWS accountsActual Behavior
terraform env
does not work for 2 separate AWS accountsSteps to Reproduce
default
environment runs on stagingproduction
environmentterraform env new prod
production
envproduction
environmentNOTE: S3 configuration is already under a separate file, called
backend.tf
terraform plan
, but terraform shows that it wants to recreate existing resourcesdefault
, access deniedterraform env switch default
worksproduction
env, runterraform env delete production
, access deniedterraform env delete production
, access deniedproduction
terraform environment, because it seems like everytime i want to delete, Terraform wants to check both environments, and my STS assume-role has access tokens for only 1 environment at a timeImportant Factoids
Previously in terraform 0.8, i use the
remote
command to switch environments:production
environment roleI would expect the same with the new
env
andinit
commands but apparently it's not that simple anymore.The text was updated successfully, but these errors were encountered: