Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to override remote backend with local backend in terraform?

Tags:

terraform

I have been looking at the terraform docs and a udemy course for the answer to this question, but cannot find the answer. I have a jenkins pipeline that is building AWS infrastructure with terraform. This is using a remote backend which is configured via a

terraform {
  backend "s3" {}
}

block. I want to override this for local development so that a local state file is generated with terraform init. I have tried running terraform init -backend=false but I realize this is not what i want because it doesnt create a local backend either. I have seen terraform init -backend=<file> is an option, but if i use that then I dont know what to put in the file to indicate default local backend config. I found this article override files but it doesnt lead me to believe that this functionality exists in terraform for this particular use case. I want to make sure I do this in the correct way. How does one override the remote backend config with a default local backend config in terraform? Thanks.

like image 832
Dan Avatar asked Jan 16 '20 18:01

Dan


1 Answers

The Terraform override concept you mentioned does work for this use case.

You can create files with an _override.tf added to the end elements of the override will take precedence over the original. e.g. <my_existing_file>_override.tf.

You can use this to override your existing backend config override the existing backend infrastructure so that you can init a local state file for testing/dev purposes.

I would create a script and alias it to the following value:

echo "terraform {" > backend_override.tf
echo "  backend \"local\" {" >> backend_override.tf
echo "    path = \"./\"" >> backend_override.tf
echo "  }" >> backend_override.tf
echo "}" >> backend_override.tf

Remember to add *.override.tf to your .gitignore files so that you don't accidentally break your CI.

like image 77
Jake Nelson Avatar answered Nov 14 '22 04:11

Jake Nelson