This is an old revision of the document!
Terraform
General Information
“Terraform enables you to safely and predictably create, change, and improve infrastructure. It is an open source tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.”
Site
- Official Site: https://www.terraform.io/
- Downloads: https://www.terraform.io/downloads.html
- Getting started: https://www.terraform.io/intro/getting-started/install.html
Checklist
- AWS Account
Install Terraform
Installing Terraform on Linux.
- Visit downloads page: https://www.terraform.io/downloads.html
- Copy download link
- On Linux server, wget the link to download (example link)
wget https://releases.hashicorp.com/terraform/0.11.7/terraform_0.11.7_linux_amd64.zip
- Unzip single binary, move into /usr/local/bin
unzip terraform_0.11.7_linux_amd64.zip mv terraform /usr/local/bin/
- Verify
terraform --version
Configure AWS Credentials for Use
- Login to your AWS account, create access keys for CLI use and download the file.
- Create an AWS credentials file in your home directory
vim ~/.aws/credentials # AWS Credentials [default] aws_access_key_id = "access-key-id-here" aws_secret_access_key = "secret-key-here"
- The profile name is “default” and can now be referenced in the terraform config files.
- Lock down permissions
chmod 600 ~/.aws/credentials
Terraform Example: 2 Tier VPC
Creating a 2-tier VPC (public and private subnets), utilizing 3 availability zones in US-West (Oregon).
This will create the all of the virtual infrastructure to start creating services inside of.
File Structure
Files can be named anything, as long as it ends in a “.tf”. Terraform will load all files in a directory structure below it that end in that.
Example Structure
├── main.tf # Terraform root config (does not have to be 'main.tf') ├── outputs.tf # Output data that can be displayed ├── site # A local defined module called "site" │ ├── nat_gateway.tf │ ├── outputs.tf │ ├── routes.tf │ ├── security_groups.tf │ ├── subnets.tf │ ├── variables.tf │ └── vpc.tf ├── terraform.tfstate # Terrform creates this file to keep track of resource state ├── terraform.tfstate.backup # And this is just a backup of state └── variables.tf # Variables defined here that can be used or passed into modules