Skip to content

Commit

Permalink
Support atmos terraform apply --from-plan with additional flags (#684)
Browse files Browse the repository at this point in the history
Right now, when atmos generates the plan-file arg for `terraform apply`,
it puts it first, and then appends any additional args or flags that may
be specified by the user. Terraform is very particular about the order
of args and flags, and crashes with an error if flags follow the
plan-file args. Instead, put the plan-file arg at the end of the
command, after any additional flags or args.

For example, with this change `atmos terraform apply -s $STACK
$COMPONENT planfile -- -parallelism=1` will call `terraform apply
-parallelism=1 planfile` instead of `terraform apply planfile
-parallelism=1`.
  • Loading branch information
duncanaf authored Sep 1, 2024
1 parent fec62aa commit 61389b2
Showing 1 changed file with 14 additions and 9 deletions.
23 changes: 14 additions & 9 deletions internal/exec/terraform.go
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,7 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {
case "refresh":
allArgsAndFlags = append(allArgsAndFlags, []string{varFileFlag, varFile}...)
case "apply":
if info.UseTerraformPlan {
if info.PlanFile != "" {
// If the planfile name was passed on the command line, use it
allArgsAndFlags = append(allArgsAndFlags, []string{info.PlanFile}...)
} else {
// Otherwise, use the planfile name what is autogenerated by Atmos
allArgsAndFlags = append(allArgsAndFlags, []string{planFile}...)
}
} else {
if !info.UseTerraformPlan {
allArgsAndFlags = append(allArgsAndFlags, []string{varFileFlag, varFile}...)
}
case "init":
Expand All @@ -370,6 +362,19 @@ func ExecuteTerraform(info schema.ConfigAndStacksInfo) error {

allArgsAndFlags = append(allArgsAndFlags, info.AdditionalArgsAndFlags...)

// Add any args we're generating -- terraform is picky about ordering flags
// and args, so these args need to go after any flags, including those
// specified in AdditionalArgsAndFlags.
if info.SubCommand == "apply" && info.UseTerraformPlan {
if info.PlanFile != "" {
// If the planfile name was passed on the command line, use it
allArgsAndFlags = append(allArgsAndFlags, []string{info.PlanFile}...)
} else {
// Otherwise, use the planfile name what is autogenerated by Atmos
allArgsAndFlags = append(allArgsAndFlags, []string{planFile}...)
}
}

// Run `terraform workspace` before executing other terraform commands
// only if the `TF_WORKSPACE` environment variable is not set by the caller
if info.SubCommand != "init" && !(info.SubCommand == "workspace" && info.SubCommand2 != "") {
Expand Down

0 comments on commit 61389b2

Please sign in to comment.