terraform - the simple guide

just a simple guide for getting started with terraform

setup

Download Terraform

initialize

You can initialize the working directory using
terraform init
This is the first command that should be run after writing a new Terraform configuration

validate

Validate that the configuration is valid by running the command
terraform validate
Requires the working directory to be initialized

plan

Create an execution plan of changes that will be applied by executing
terraform plan
The existing infrastructure will not be modified

apply

Deploy and apply the defined changes by running
terraform apply
This will provision the resources in the data center infrastructure

destroy

To destroy all the Terraform-managed infrastructure, use
terrafrom destroy

modules

Terraform modules are files that contain blocks and the .tf ending

providers

Providers include resources that are used to define the infrastructure that will be created

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

View all providers

blocks

Blocks are used to define the configuration of an object, like a resource

Blocks have a block type, can have zero or more labels, and have a body that contains any number of arguments and nested blocks

<block type> "<label>" "<label>" { <arguments> }

The ordering of blocks and the modules they are organized into are generally not significant

block types

Block types define the behavior of a block and how many labels must follow the type keyword

Some of the most common block types are resource, intput variable, output varaible, data source, etc

resources

Resouces define the infrastructure that will be created given what arguments and other attributes the resource supports

resource "aws_instance" "webserver" {
ami = "ami-a1b2c3d4"
instance_type = var.instanceType }

variables

Input variables can be used as parameters

variable "instanceType" { default = "t2.micro" }

outputs

Output variables define values that will be printed when changes to the infrastructure are applied

output "webserver_public_ip" {
value = aws_instance.webserver.public_ip }

You can also query outputs using

terraform output

data

Data sources allow data to be fetched from outside of a Terrafrom configuration

data "local_file" "input" {
filename = "localfile.txt" }

Providers may offer data sources that allow you to fetch information from the cloud