linux_wiki:terraform

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

Checklist

  • AWS Account

Install Terraform

Installing Terraform on Linux.

  • 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.

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

Contents of the above config files.

Files in the top level directory. Ordered in a way that is easier to follow.

main.tf
# Title: main.tf
# Description: Main terraform config file. Define provider and load other modules
#              AWS Credentials auto loaded from ~/.aws/credentials
 
## AWS Provider and Region
provider "aws" {
  region = "${var.region}"
  profile = "default"
}
 
## Module: Site Infrastructure Setup
module "site" {
  source = "./site"
  availability_zones = "${var.availability_zones}"
  public_subnet01_cidr = "${var.public_subnet01_cidr}"
  public_subnet02_cidr = "${var.public_subnet02_cidr}"
  public_subnet03_cidr = "${var.public_subnet03_cidr}"
  private_subnet01_cidr = "${var.private_subnet01_cidr}"
  private_subnet02_cidr = "${var.private_subnet02_cidr}"
  private_subnet03_cidr = "${var.private_subnet03_cidr}"
  vpc_cidr = "${var.vpc_cidr}"
}
variables.tf
# Title: variables.tf                                                                                                
# Description: Root defined variables                                                                                
 
####-- Global Variables --####                                                                                       
 
# AWS Region To Use                                                                                                  
variable "region" {                                                                                                  
  default = "us-west-2"                                                                                              
}                                                                                                                    
 
# Availability Zones To Use                                                                                          
variable "availability_zones" {                                                                                      
  type = "list"                                                                                                      
  default = [ "us-west-2a" , "us-west-2b" , "us-west-2c" ]                                                           
}                                                                                                                    
 
####-- VPC Variables --####                                                                                          
 
# VPC Network                                                                                                        
variable "vpc_cidr" {                                                                                                
  description = "CIDR for the whole VPC"                                                                             
  # /21 = 2046 IPs, 10.0.0.1 - 10.0.7.254                                                                            
  default = "10.0.0.0/21"                                                                                            
}                                                                                                                    
 
# Public Subnet 01 (with IGW)                                                                                        
variable "public_subnet01_cidr" {                                                                                    
  description = "CIDR for the Public Subnet"                                                                         
  # /25 = 126 IPs, 10.0.0.1 - 10.0.0.126
  default = "10.0.0.0/25"
}
 
# Public Subnet 02 (with IGW)
variable "public_subnet02_cidr" {
  description = "CIDR for the Public Subnet"
  # /25 = 126 IPs, 10.0.0.129 - 10.0.0.254
  default = "10.0.0.128/25"
}
 
# Public Subnet 03 (with IGW)
variable "public_subnet03_cidr" {
  description = "CIDR for the Public Subnet"
  # /25 = 126 IPs, 10.0.1.1 - 10.0.1.126
  default = "10.0.1.0/25"
}
 
# Private Subnet 01 (no IGW)
variable "private_subnet01_cidr" {
  description = "CIDR for the Private Subnet"
  # /23 = 510 IPs, 10.0.2.1 - 10.0.3.254
  default = "10.0.2.0/23"
}
 
# Private Subnet 02 (no IGW)
variable "private_subnet02_cidr" {
  description = "CIDR for the Private Subnet"
  # /23 = 510 IPs, 10.0.4.1 - 10.0.5.254
  default = "10.0.4.0/23"
}
 
# Private Subnet 03 (no IGW)
variable "private_subnet03_cidr" {
  description = "CIDR for the Private Subnet"
  # /23 = 510 IPs, 10.0.6.1 - 10.0.7.254
  default = "10.0.6.0/23"
}
outputs.tf
# Title: outputs.tf
# Description: Outputs from resources saved as variables
 
# Pull the VPC ID from the site module
output "vpc_id" {
  value = "${module.site.vpc_id}"
}

Files in the site/ module directory. Ordered in a way that is easier to follow.

variables.tf
# Title: site/variables.tf
# Description: Required input variables from the parent file (../main.tf)
 
# Availability Zones: Pass in from main variables
variable "availability_zones" { type = "list" }
 
# VPC CIDR: Pass in from main variables
variable "vpc_cidr" {}
 
# Public Subnets (with IGW): Pass in from main
variable "public_subnet01_cidr" {}
variable "public_subnet02_cidr" {}
variable "public_subnet03_cidr" {}
 
# Private Subnets (no IGW): Pass in from main
variable "private_subnet01_cidr" {}
variable "private_subnet02_cidr" {}
variable "private_subnet03_cidr" {}
vpc.tf
# Title: vpc.tf
# Description: Create a VPC and attach an internet gateway
 
####-- VPC --####
 
# VPC: Creation
resource "aws_vpc" "myvpc" {
  cidr_block = "${var.vpc_cidr}"
  enable_dns_hostnames = true
  tags {
    Name = "myvpc"
  }
}
 
# VPC: Internet Gateway
resource "aws_internet_gateway" "myigw" {
  vpc_id =  "${aws_vpc.myvpc.id}"
  tags {
    Name = "myigw"
  }
}
nat_gateway.tf
 
outputs.tf
 
routes.tf
 
security_groups.tf
 
subnets.tf
 

  • linux_wiki/terraform.1529029429.txt.gz
  • Last modified: 2019/05/25 23:50
  • (external edit)