Deploying Your First Terraform Configuration
Overview
- What is Terraform
- Workflow
- Installation
- ‘Globomantics’ scenario
- Walk through ‘Terraform: Hello world’ demonstration
What is Terraform
- Infrastructure automation tool
- infra meaning a layer of technology that a developer consumes with having to deploy and manage it i.e.networking, virtual machines and containers
- the core is an open-source project maintained by Hashicorp, there are paid version
- it is also vendor agnostic meaning it has no preference of any particular cloud or service, works with most
- The single binary is compiled from Go available for multiple OS
- Uses a declarative syntax,
- describe how you want the env, TF does the rest
- Config files are written in Hashicorp Configuarion Language (HCL) or JSON
- Push based deployment
Core components
- Executable
- single binary file you call to run Terraform, contains all core functionality
- HCL Configuration files
- the config you are deploying is contained in HCL Configuration files, with extension .tf usually
- when there are multiple .tf files in a directory, Terraform stitches them together
- Provider Plugins
- executables invoked by Terraform to interact with a service’s API, ie AWS
- most common plugins are hosted on the public terraform registry
- State data
- how Terraform keeps track of what’s been deployed
- state data contains current info about deployment
- a mapping of whats defined in config to what exists in target env.
- Terraform, when deploying:
- compares updated config files to state data file
- calculates changes needed to make them match,
- makes changes
- then updates state data
Installing Terraform
- Download executable for platform
- Add to your PATH environment variable
- Terraform is available in common packet managers and even as a docker container.
You can find the installer info for Terraform here:
https://www.terraform.io/downloads.htm
NOTE
- I had trouble using brew because the xcode install tool was going to take a long time to download
- so I bypassed and downloaded mac binary directly and unzipped into /opt/terraform directory
- and added the following to my ~/.bash_profile using nano
PATH="/opt/terraform:${PATH}"
source:
https://www.coachdevops.com/2020/04/how-to-install-terraform-on-mac-os.html
- be sure to curl latest version of binary, address available on terraform downloads page
Using the CLI
Usage:
- terraform [global options] <subcommand> [args]
- terraform -help
- Show this help output, or the help for a specified subcommand
- terraform -version
- An alias for the "version" subcommand.
- Will accept double -- dash or single - dash for arguments, even if they are more than single characters
- Single is preferred syntax
Terraform Object Types
- Providers
- define information about provider you want to use.
- i.e. AWS, which account, which region
- Resources
- things to create in target env ie Ec2, network, database
- bulk of what you write
- each resource is associated with a provider and usually requires additional config information
- Data Sources
- a way to query information from provider
- not creating anything
- just asking for data may need to use in config
- also linked to a provider
- e.g. ami for an ec2 instance, list of Availability Zones in a region
General Block Syntax
- uses block syntax
- easier to read
- permits inline comments
- simplified version of JSON
- a sample block looks like
block_type “label” “name_label” {
key = “value”
nested_block {
key = “value”
}
}
- each lines begins with block_type
- which specifies type of object being described
- next are a series of labels which depend on what object we are dealing with
- last label is usually the name label, provides a way to refer back to object in config
- next in block we have one or more key value pairs which make use of available arguments for object type
- each key is a string and value can be any of Terraform’s data types
- can also have nested blocks inside main block
- inside will be more key value pairs
Example
- HCL has a defined syntax for referring to other objects
- if you want the whole resource, skip the attribute option
- e.g from the above example.
aws_instance.web_server.name
- which will equal “web-server”
- e.g from the above example.
Base configuration file
- Comments are supported in HCL by using hash symbol #
- comments can break file into:
- Providers
- identify provider, with key value pairs storing AWS account details and how we will access, and also specifying region
- Data
- lists the data sources
- in example, data source type is an AWS Service Manager parameter with the name label ami
- within config block we have a key called name and the value is the path of a parameter, which in this case grabs the latest Amazon Linux AMI ID for region we’re in
- this value will be used later when we create our AWS Ec2 instance
- lists the data sources
- Resources
- creates each individual resource
- in example we start with Networking,
- creating a VPC with specified Cidr block and dns hostnames setting
- creating an internet gateway linked to vpc using vpc.id
- creating a subnet in vpc with a specfied CIDR block and public IP address
- and more… such as a route table, in which we can create within a nested block a particular route to populate the table
- To find out what arguments and attributes are available for a resource i.e. .vpc.id
- read the documentation
Terraform Workflow
- terraform makes use of provider plugins as noted
- before it uses plugins it gets them
- this is done by in initialisation process
$> terraform init
- init looks for config files inside current working directory
- examines them to find any needed plugins
- try and download those plugins from public Terraform Registry
- unless an alternate location is specified
- also prepares the back end of the state data file,
- if not specified Terraform creates this in cd
- once complete, ready to deploy infra
- next step is plan out deployment with terraform plan
$> terraform plan
- in this step terraform looks at current config, state date contents, determine differences, and make plan to update target env to match desired config
- terraform prints out plan so you can verify changes to be made
- not absolutely necessary to run, but pretty useful
- you can save the changes of the plan to a file and then feed into terraform for next step
- terraform indicates what will be created with a green + sign
- Next step is making changes in target env, done with terraform apply
$> terraform apply
- executes changes saved in plan using provider plugins
- resources are created or modified
- state data is then updated
- if no changes are needed, terraform notifies us and makes none
- One last command, when done with env is terraform destroy
$> terraform destroy
- a dangerous command!
- the red minus sign indicates what resources will be destroyed
Summary
- Terraform is infrastructure automation
- Terraform is a single binary which can be used on almost any OS
- Configurations used by Terraform is HCL or JSON
- basic workflow is: init, plan and apply (and maybe destroy)
- creates each individual resource