You can use terraform to setup resources, modify, delete them. You just need to write the terraform file/code/template which is used to manage resources. This Terraform code help us to manage resources
e.g. You can use Terraform Template/File/Code to create EC2 instance resource on AWS Cloud, modify it and delete when not needed. Here, AWS is called as a provider.
Terraform support a lot of providers.
A basic template would like as follows.
This template will create a VPC resource on AWS provider.
variables.tf
variable "access_key" {
description = "Access key to AWS console"
}
variable "secret_key" {
description = "Secret key to AWS console"
}
variable "region" {
description = "Region of AWS VPC"
}
terraform.tfvars
region = "eu-west-3"
access_key = "YOUR-ACCESS-KEY"
secret_key = "YOUR-SECRET-KEY"
main.tf
provider "aws" {
region = "${var.region}"
access_key = "${var.access_key}"
secret_key = "${var.secret_key}"
}
resource "aws_vpc" "vpc" {
cidr_block = "10.0.0.0/16"
}
resource "aws_internet_gateway" "gateway" {
vpc_id = "${aws_vpc.vpc.id}"
}
resource "aws_route" "route" {
route_table_id = "${aws_vpc.vpc.main_route_table_id}"
destination_cidr_block = "0.0.0.0/0"
gateway_id = "${aws_internet_gateway.gateway.id}"
}
data "aws_availability_zones" "available" {}
resource "aws_subnet" "main" {
count = "${length(data.aws_availability_zones.available.names)}"
vpc_id = "${aws_vpc.vpc.id}"
cidr_block = "10.0.${count.index}.0/24"
map_public_ip_on_launch = true
availability_zone = "${element(data.aws_availability_zones.available.names, count.index)}"
}
Commands:
terraform init
terraform plan
terraform apply
I would recommend to go through Terraform's Official Site. They have good documentations available on their site.
Infrastructure as Code
, so basically terraform allows you to create infrastructure (not configure it) in a testable, reliable, reproducible and automatic way. After applying terraform you still probably want to run aConfiguration Management
tool like Puppet, Chef or Salt. – Trunks