Introduction to Terraform
Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions. Managing existing resources on aws will require the importation of the resources to Terraform and be managed by Terraform, which will allow other resources to be built around it.
Infrastructure as Code
Infrastructure is described using a high-level configuration syntax. This allows a blueprint of your datacenter to be versioned and treated as you would any other code. Additionally, infrastructure can be shared and re-used.
Terraform has a "planning" step where it generates an execution plan. The execution plan shows what Terraform will do when you call
terraform apply. This lets you avoid any surprises when Terraform manipulates infrastructure.
terraform plan + aws_instance.example.11 ami: "ami-v1" instance_type: "t2.micro" + aws_instance.example.12 ami: "ami-v1" instance_type: "t2.micro" + aws_instance.example.13 ami: "ami-v1" instance_type: "t2.micro" + aws_instance.example.14 ami: "ami-v1" instance_type: "t2.micro" + aws_instance.example.15 ami: "ami-v1" instance_type: "t2.micro" Plan: 5 to add, 0 to change, 0 to destroy.
The above shows what will be added before the changes will be made on the infrastructure.
- The use of variables
- resources importation
- infrastructure state file
- infrastructure diagram generator based on the state
- custom output variables
- Comparison of Infrastructure As Code tools
- Infrastructure versioning
Setup AWS Account
Login to your aws account, go to your IAM console, go to "Users", click "Add user" to generate an access key and a secret key. Under access type, check Programmatic access, Click the "Create user" button on the last step and you will be able to see the security credentials for that user, which consist of Access Key ID and a Secret Access Key. You should save these keys immediately, as the Secret Access Key will never be shown again.
In order for terraform to be able to make changes to our aws infrastructure, we need to set AWS credential for the user. On your terminal:
export AWS_ACCESS_KEY_ID=(your access key id) export AWS_SECRET_ACCESS_KEY=(your secret access key) $ env |grep AWS AWS_ACCESS_KEY_ID=XXXXXXXXXXXXXXXXXXXXXXX AWS_SECRET_ACCESS_KEY=XXXXXXXXXXXXXXXXXXX
To install Terraform, find the appropriate package for your system and download it. After downloading, unzip the package. Terraform runs as a single binary named terraform. Any other files in the package can be safely removed and Terraform will still function.
The final step is to make sure that the terraform binary is available on the PATH. See this page for instruction on setting the PATH on Linux and Mac. This page contains instructions for setting the PATH on Windows.
Download terraform for Linux
$ wget https://releases.hashicorp.com/terraform/0.xx.x/terraform_0.xx.x_linux_amd64.zip $ unzip terraform_0.xx.x_linux_amd64.zip set path $ sudo mv terraform /usr/local/bin
Using brew install is the quickest way brew install terraform. If you dont have homebre, install it.
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" < /dev/null 2> /dev/null install ruby brew install terraform
- Download terraform for windows
Note: Terraform is packaged as a zip archive, so after downloading Terraform, unzip the package. Terraform runs as a single binary named terraform. Any other files in the package can be safely removed and Terraform will still function
Copy files from the zip to c:\terraform for example. That's our terraform PATH.
The final step is to make sure that the terraform binary is available on the PATH.
Set the path in your system utility in control panel.
Verifying the Installation
After installing Terraform, verify the installation worked by opening a new terminal session and checking that terraform is available. By executing terraform you should see help output similar to this:
$ terraform Usage: terraform [--version] [--help] <command> [args] The available commands for execution are listed below. The most common, useful commands are shown first, followed by less common or more advanced commands. If you're just getting started with Terraform, stick with the common commands. For the other commands, please read the help and docs before usage. Common commands: apply Builds or changes infrastructure console Interactive console for Terraform interpolations
If you get an error that terraform could not be found, your PATH environment variable was not set up properly. Please go back and ensure that your PATH variable contains the directory where Terraform was installed.
That's all folks. Feel free to point out any mistake, make some corrections, and contribute to this post in the comment section.