Command Line Deployment with Terraform

Published: 2021-05-10 18:22:07

Terraform is an infrastructure as code (IaC) service that greatly reduces the complexity faced by developers during deployment. Developers want great agility when choosing platforms. Typically with deployment, a platform will play nicely with other in-house tech, and less so with the services of competitors. Of course, great software shouldn’t be guided by what a platform's maker deems convenient or inconvenient.

What’s the solution? First, it would be beneficial to use a service that minimizes interactions with these platforms, and gives developers direct access to the resources themselves (an AWS EC2, for example). The service should also minimize discrepancies in how developers interact with each company's products. Many of the differences platform to platform amount to proprietary flare. Product A and B will have different terminology and UI, but ultimately the tools offered are quite similar. Whether it be Azure, AWS, or GCP, there should be no need to become too deeply rooted in one offering or another. By bringing deployment away from these platforms and to the command line, Terraform allows developers to focus their energy where it ought to be: devising the best possible deployment strategy for their application.


##A Declarative Approach to Infrastructure

The declarative nature of Terraform takes shape in the Terraform config file. Here, programmers simply list out the technologies and configurations they require.


You’ll notice there are no execution commands, simply a declaration of what to use. Terraform reduces complexity by internally formulating a plan that will bring a developer’s request to fruition. There’s no need to define step by step instructions. This is beneficial when first sketching out the framework of a project. It’s also beneficial when creating copies of frameworks for development, staging, and production.

Most importantly, It simplifies the process of modifying a framework. This is revealed in how Terraform is designed. The Terraform core accepts two arguments: the project state and the config file. If there are differences between the two, Terraform will devise a strategy to bring them into harmony. This means developers don’t need to spend time on breaking down existing infrastructure to improve configuration. It also means less code, as new strategies will simply take the place of old strategies in the config file.


##Interacting with the Terraform Core

The 5 basic commands for Terraform are as follows:
Init: initialize a directory as a Terraform project
Validate- check the contents of the config file for validity, and clarify any changes that need to be made
Plan - print the step-by-step strategy Terraform can execute to bridge the gap between the desired configuration and current program state
Apply- this is the command to execute the plan devised by the previous command.
Destroy- shut down an infrastructure in the proper order

To see these commands in action, first create a project directory and create a main.tf file to serve as your config:

```
provider "aws" {
	region = "us-east-1"
}

resource "aws_instance" "ec2main" {
	ami = "ami-example"
	instance_type = "t2.micro"
}
```

As you can see, this declarative syntax simply lists the provider of a resource (AWS, in this case), and the resource needed (an ec2 instance). By running `$terraform init`, a terraform lock file will be created, and any dependencies will be downloaded. Now, running `$terraform validate` will check the contents of the config file to determine if it can be executed. If there are no issues, run `$terraform plan` to see the outcome of executing the config file. Finally, `$terraform apply` will put the Terraform core into action. It will communicate directly with the provider (AWS) to get the resource requested, and, if successful, bring the status of your project into state.


Often with deployment, the proprietary quirks of each platform can make for some unwanted concessions. Either use a platform with which you’re already familiar, or spend time learning to navigate an unfamiliar interface. Terraform sees these platforms as what they are: providers and resources. With infrastructure as code services like Terraform, testing different strategies, trying new technologies, and tuning a deployment approach is a much smoother process.