Skip to content
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

Add a atmos terraform apply-last-plan command #194

Closed
Gowiem opened this issue Sep 7, 2022 · 3 comments
Closed

Add a atmos terraform apply-last-plan command #194

Gowiem opened this issue Sep 7, 2022 · 3 comments
Assignees

Comments

@Gowiem
Copy link
Member

Gowiem commented Sep 7, 2022

Describe the Feature

Add a new terraform subcommand: apply-last-plan that will pick up the written *.planfile for the given component + stack and will execute terraform apply <planfile name>.planfile <extra args>.

Use Case

A really common workflow that I followed when working with vanilla terraform was the following:

  1. Run terraform plan -out run.plan
  2. Check and confirm the plan
  3. terraform apply run.plan

With atmos, this doesn't work. When you run a plan, it outputs a planfile, but when passing that planfile as an extra argument to apply, it causes the following error:

Executing command:
/usr/bin/terraform apply -var-file dev-ssm-parameters.terraform.tfvars.json dev-ssm-parameters.planfile

│ Error: Can't set variables when applying a saved plan
│
│ The -var and -var-file options cannot be used when applying a saved plan file, because a saved plan includes the variable values that were set when it was created.

exit status 1

That is due to the -var-file arg atmos passes obviously, which we would not pass if we implemented the apply-last-plan subcommand. It also has the added benefit that the atmos user doesn't need to specify the planfile name in the command, which is duplicate information anyway.

Alternatives Considered

N/A

Additional Context

I'll have someone from my team or myself work on this if there is interest to implement!

@aknysh
Copy link
Member

aknysh commented Sep 8, 2022

@Gowiem we have the flag already --from-plan

Run

atmos terraform --help
'atmos terraform apply' and 'atmos terraform deploy' commands support '--from-plan' flag. If the flag is specified, the commands will use the previously generated 'planfile' instead of generating a new 'varfile'
'atmos' supports all native 'terraform' commands.
In addition, 'component' and 'stack' are required in order to generate variables for the component in the stack.
atmos terraform <command> <component> -s <stack> [options]
atmos terraform <command> <component> --stack <stack> [options]

Additions and differences from native terraform:
 - before executing other 'terraform' commands, 'atmos' calls 'terraform init'
 - 'atmos terraform deploy' command executes 'terraform plan' and then 'terraform apply'
 - 'atmos terraform deploy' command supports '--deploy-run-init=true/false' flag to enable/disable running 'terraform init' before executing the command
 - 'atmos terraform deploy' command sets '-auto-approve' flag before running 'terraform apply'
 - 'atmos terraform apply' and 'atmos terraform deploy' commands support '--from-plan' flag. If the flag is specified, the commands will use the previously generated 'planfile' instead of generating a new 'varfile'
 - 'atmos terraform clean' command deletes the '.terraform' folder, '.terraform.lock.hcl' lock file, and the previously generated 'planfile' and 'varfile' for the specified component and stack
 - 'atmos terraform workspace' command first calls 'terraform init -reconfigure', then 'terraform workspace select', and if the workspace was not created before, it then calls 'terraform workspace new'
 - 'atmos terraform import' command searches for 'region' in the variables for the specified component and stack, and if it finds it, sets 'AWS_REGION=<region>' ENV var before executing the command
 - 'atmos terraform generate backend' command generates the backend file for the component in the stack
 - 'atmos terraform generate varfile' command generates a varfile for the component in the stack
 - 'atmos terraform shell' command configures an environment for the component in the stack and starts a new shell allowing executing all native terraform commands

@aknysh
Copy link
Member

aknysh commented Sep 8, 2022

Please add your new --skip-init flag from the other PR to the help "Additions and differences from native terraform:", right after "- before executing other 'terraform' commands, 'atmos' calls 'terraform init'"

@Gowiem
Copy link
Member Author

Gowiem commented Sep 9, 2022

@aknysh ah damn -- I missed --from-plan. Great to know about, thanks for the heads up! I'll close this out.

And re: #193 -- Will do!

@Gowiem Gowiem closed this as completed Sep 9, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants