Multiple Deployments with Terraform Workspaces
In recent months, I've been working more and more with Terraform, deploying to multiple cloud providers and even combining it with Serverless projects (more on that on the next post).
Terraform allows for infrastructure automation in a way Ansible could only dream of doing.
It's incredibly simple, quite predictable, and works seemlessly with all the major cloud providers.
If you make any changes to your Terraform configuration, and run
terraform apply
But what if you want multiple deployments of your infrastructure, across different regions?
For this we will be using Terraform Workspaces, with a few added improvements.
Example Terraform config - EC2 Instance
Let's look at the following main.tfprovider "aws" {
region = "us-east-1"
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "main_server" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
tags {
Name = "exampleInstance-us-east-1"
}
}
Let's tweak it a bit and move the region to a variable file.
main.tf:
provider "aws" {
region = "${var.aws_region}"
}
data "aws_ami" "ubuntu" {
most_recent = true
filter {
name = "name"
values = ["ubuntu/images/hvm-ssd/ubuntu-bionic-18.04-amd64-server-*"]
}
filter {
name = "virtualization-type"
values = ["hvm"]
}
owners = ["099720109477"] # Canonical
}
resource "aws_instance" "main_server" {
ami = "${data.aws_ami.ubuntu.id}"
instance_type = "t2.micro"
tags {
Name = "exampleInstance-${var.aws_region}"
}
}
variable "aws_region" {
description = "Selected AWS Region"
}
terraform workspace create dev
If we try to run terraform apply again, a second instance will be created!
External configuration files
Once your variables start adding up, it gets pretty annoying to fill them in each time you deploy your terraform. You could add them to the command itself as such:terraform apply -var='aws_region=us-east-1'
For our example, I have the file dev.tfvars:
aws_region = "us-west-2"
terraform apply --var-file='dev.tfvars'
terraform apply --var-file=$(terraform workspace show).tfvars
I've created an example repository with all the relevant files.
My next post will review how you could connect Terraform with Serverless deployments.
Posted in
Technology