-
Notifications
You must be signed in to change notification settings - Fork 59
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
[Feature Request] Supports workspaces and var files #3
Comments
It's okay for me to create a pull request by me and check it to decide whether or not to adopt a new feature. |
@takanakahiko Thank you for the proposal! Adding workspace and variable file support is ok because they are common use-case for Terraform, but, I don't use them in my work, so I'm not sure what is the best design for refactoring tfstate with the workspace model. If my understanding is correct, using workspace has a single configuration and multiple states. Workspaces are usually related to environments such as prod, test and dev, a configuration change will be applied in a workflow for each environment. When we want to rename a resource in configuration, we should move the resource in all states. Wouldn't it be better to apply one migration to all workspaces in each workflow? The workspace model enforces us exactly equal configurations across workspaces. I don't think it makes sense to apply migration only to some workspaces. I think a migration should correspond to a configuration, and switching workspaces should be implemented by changing the execution environment not to declare it in the migration. If so, we can automatically select workspace in each workflow with an environment variable What do you think of it? |
@minamijoyo Thank you for your reply! For example, what I want to do is support for the following use cases:
# us/ec2.tf
provider "aws" {
region = "us-west-2"
}
resource "aws_instance" "web-us" {
...
tags = {
Name = "HelloUS"
}
} # tokyo/ec2.tf
provider "aws" {
region = "ap-northeast-1"
}
resource "aws_instance" "web-tokyo" {
...
tags = {
Name = "HelloTokyo"
}
} # commonized/ec2.tf
provider "aws" {
region = "${var.region}"
}
resource "aws_instance" "web" {
...
tags = {
Name = "${var.name}"
}
} # commonized/variables-us.tfvars
region = "us-west-2"
name = "HelloUS" # commonized/variables-tokyo.tfvars
region = "ap-northeast-1"
name = "HelloTokyo" The need to apply the migration to only some workspaces is in the following areas: In this example, it is the case of the following migration
The migration file looks like this. migration "multi_state" "mv_us" {
from_dir = "us"
from_workspace = "us"
to_dir = "commonized"
to_workspace = "commonized-us"
to_varfile = "./variables-us.tfvars"
actions = [
"mv aws_instance.web-us aws_instance.web",`
]
}
migration "multi_state" "mv_tokyo" {
from_dir = "tokyo"
from_workspace = "tokyo"
to_dir = "commonized"
to_workspace = "commonized-tokyo"
to_varfile = "./variables-tokyo.tfvars"
actions = [
"mv aws_instance.web-tokyo aws_instance.web",`
]
} I think using TF_WORKSPACE or TF_CLI_ARGS_name is a very smart way to do it. But at this point, I don't know how difficult it is to implement. |
@takanakahiko Thank you for sharing your use-case! It looks like merging multiple root modules (directories) into a single root module (directory) with workspaces. I understood in such case it's useful moving a state across workspaces. When I wrote the above comment, I was looking for the best solution for supporting workspace. However, I read lots of issues around the workspace feature and reached a conclusion that there is no single solution to meet all use-cases. I can imagine someone may want to merging and splitting workspaces, at the same time others may want to renaming resource in all workspaces. We have two options, but I think they actually doesn't conflict with each other. (1) one migration file per one workspace:
(Note for me): I'm working on a migration history management feature in #2, which will allow us to apply all unapplied migrations. In your use-case, it doesn't a matter. However, If deployment pipelines and stages are separated with each workspace, we cannot apply all at once. A possible workaround is split migration file directories and history files per workspace. This means a path of setting file for tfmigrate can be passed from a command line argument. (2) one migration file per one root module
I assumed using To sum up the above, I think your proposal, that is, adding |
@takanakahiko I studied how to integrate workspace with tfmigrate and found that there are some limitations we should consider:
With the above in mind, an implementation probably looks like the following:
To do this, we probably need to implement the following:
I think it will require other minor changes, but the grand design is not so wrong. |
Thank you for your reply! |
Hey @minamijoyo, any plan to support workspaces in the foreseeable future? I see that as being the missing piece to this otherwise awesome tool. |
Hi, @Gowiem. Thank you for your feedback! I understand the workspace feature should be supported, but honestly it's not high-priority for me. I'm working on multiple projects in my spare time and other things for now. So I cannot say any ETA, sorry 🙏 For anyone who want it, Note: The internal structure has changed with refactoring since my last comment, but I think the implementation is still technically possible. Thanks! |
@minamijoyo appreciate getting back and totally get other projects taking over. Hopefully someone will pick this up one day! |
I'm only interested in support for var-file. Is there a way to pass env vars to terraform right now? I tried setting I haven't tried it yet, but maybe |
@whiskeysierra Yes, you can pass Terraform variables to terraform CLI with auto tfvar files you mentioned. Or, to pass environment variables (e.g. If you still have trouble, could you open a new issue with the minimal reproduction case to clarify what you are trying? |
I tried that, but it didn't seem to work. |
Correction, it does work. |
I have exactly this use case and I think this is how many of users use them. Of course there might be other cases when temporary workspace is created for testing stuff but I think then there is no need for run migrations. We use same configuration for multiple states.
staging/
|
I'm totally new to Go but I investigated this a bit. So final init command should look something like this:
I didn't find a way how to implement this to code right away because this my first experience with Go language. |
Thank you for creating a great tool.
This tool allows you to specify directories using
dir
,from_dir
, andto_dir
.But that doesn't work if you want to specify a workspace.
For example, this would be the case if you want to
terraform workspace select hogehuga
for each directory.It would be nice to have the option to specify
workspace
andvar_file
for each type, but what do you think?example
The text was updated successfully, but these errors were encountered: