When it comes to managing cloud-based resources, it’s hard to find a better tool than Hashicorp’s Terraform. Terraform is an ‘infrastructure as code’ application, marrying configuration files with backing APIs to provide a nearly seamless layer over your various cloud environments. It allows you to declaratively define your environments and their resources through a process that is structured, controlled, and collaborative.
One key advantage Terraform provides over other tools (like AWS CloudFormation) is having a rapid development and release cycle fueled by the open source community. This has some major benefits: features and bug fixes are readily available, new products from resource providers are quickly incorporated, and you’re able to submit your own changes to fulfill your own requirements.
Hashicorp recently released v0.10.0 of Terraform, introducing some fundamental changes in the application’s architecture and functionality. We’ll review the three most notable of these changes and how to incorporate them into your existing Terraform projects when migrating to Terraform v.0.10.x.
- Terraform Providers are no longer distributed as part of the main Terraform distribution
- New
auto-approve
flag forterraform apply
- Existing
terraform env
commands replaced byterraform workspace
A brief note on Terraform versions:
Even though Terraform uses a style of semantic versioning, their ‘minor’ versions should be treated as ‘major’ versions.
1. Terraform Providers are no longer distributed as part of the main Terraform distribution
The biggest change in this version is the removal of provider code from the core Terraform application.
Terraform Providers are responsible for understanding API interactions and exposing resources for a particular platform (AWS, Azure, etc). They know how to initialize and call their applications or CLIs, handle authentication and errors, and convert HCL into the appropriate underlying API calls.
It was a logical move to split the providers out into their own distributions. The core Terraform application can now add features and release bug fixes at a faster pace, new providers can be added without affecting the existing core application, and new features can be incorporated and released to existing providers without as much effort. Having split providers also allows you to update your provider distribution and access new resources without necessarily needing to update Terraform itself. One downside of this change is that you have to keep up to date with features, issues, and releases of more projects.
The provider repos can be accessed via the Terraform Providers organization in GitHub. For example, the AWS provider can be found here.
Custom Providers
An extremely valuable side-effect of having separate Terraform Providers is the ability to create your own, custom providers. A custom provider allows you to specify new or modified attributes for existing resources in existing providers, add new or unsupported resources in existing providers, or generate your own resources for your own platform or application.
You can find more information on creating a custom provider from the Terraform Provider Plugin documentation.
1.1 Configuration
The nicest part of this change is that it doesn’t really require any additional modifications to your existing Terraform code if you were already using a Provider block.
If you don’t already have a provider block defined, you can find their configurations from the Terraform Providers documentation.
You simply need to call the terraform init
command before you can perform any other action. If you fail to do so, you’ll receive an error informing you of the required actions (img 1a).
After successfully reinitializing your project, you will be provided with the list of providers that were installed as well as the versions requested (img 1b).
You’ll notice that Terraform suggests versions for the providers we are using – this is because we did not specify any specific versions of our providers in code. Since providers are now independently released entities, we have to tell Terraform what code it should download and use to run our project.
(Image 1a: Notice of required reinitialization)
(Image 1b: Response from successful reinitialization)
Providers are released separately from Terraform itself, and maintain their own version numbers.
You can specify the version(s) you want to target in your existing provider blocks by adding the version
property (code block 1). These versions should follow the semantic versioning specification (similar to node’s package.json or python’s requirements.txt).
For production use, it is recommended to limit the acceptable provider versions to ensure that new versions with breaking changes are not automatically installed.
(Code Block 1: Provider Config)
provider "aws" {
version = "0.1.4"
allowed_account_ids = ["1234567890"]
region = "us-west-2"
}
(Image 1c: Currently defined provider configuration)
2. New auto-approve flag for terraform apply
In previous versions, running terraform apply
would immediately apply any changes between your project and saved state.
Your normal workflow would likely be:
run terraform plan
followed by terraform apply
and hope nothing changed in between.
This version introduced a new auto-approve
flag which will control the behavior of terraform apply
.
Deprecation Notice
This flag is set to
true
to maintain backwards compatibility, but will quickly change tofalse
in the near future.
2.1 auto-approve=true (current default)
When set to true, terraform apply
will work like it has in previous versions.
If you want to maintain this functionality, you should upgrade your scripts, build systems, etc now as this default value will change in a future Terraform release.
(Code Block 2: Apply with default behavior)
# Apply changes immediately without plan file
terraform apply --auto-approve=true
2.2 auto-approve=false
When set to false
, Terraform will present the user with the execution plan and pause for interactive confirmation (img 2a).
If the user provides any response other than yes
, terraform will exit without applying any changes.
If the user confirms the execution plan with a yes
response, Terraform will then apply the planned changes (and only those changes).
If you are trying to automate your Terraform scripts, you might want to consider producing a plan file for review, then providing explicit approval to apply the changes from the plan file.
(Code Block 3: Apply plan with explicit approval)
# Create Plan
terraform plan -out=tfplan
# Apply approved plan
terraform apply tfplan --auto-approve=true
(Image 2a: Terraform apply with execution plan)
3. Existing terraform env commands replaced by terraform workspace
The terraform env
family of commands were replaced with terraform workspace
to help alleviate some confusion in functionality. Workspaces are very useful, and can do much more than just split up environment state (which they aren’t necessarily used for). I recommend checking them out and seeing if they can improve your projects.
There is not much to do here other than switch the command invocation, but the previous commands still currently work for now (but are deprecated).
License Warning
You are using an UNLICENSED copy of Scroll Office.
Do you find Scroll Office useful?
Consider purchasing it today: https://www.k15t.com/software/scroll-office
— Steve Byerly, Principal SDE (IV), Cloud, 2nd Watch