Using Pipelines to Manage Environments with Infrastructure as Code

One stack with all the environments

Multiple environments managed as a single stack
# STAGING ENVIRONMENTresource “aws_vpc” “staging_vpc” {  cidr_block = “10.0.0.0/16”}resource “aws_subnet” “staging_subnet” {  vpc_id = “${aws_vpc.staging_vpc.id}”  cidr_block = “10.0.1.0/24”}resource “aws_security_group” “staging_access” {  name = “staging_access”  vpc_id = “${aws_vpc.staging_vpc.id}”}resource “aws_instance” “staging_server” {  instance_type = “t2.micro”  ami = “ami-ac772edf”  vpc_security_group_ids = [“${aws_security_group.staging_access.id}”]  subnet_id = “${aws_subnet.staging_subnet.id}”}# PRODUCTION ENVIRONMENTresource “aws_vpc” “production_vpc” {  cidr_block = “10.0.0.0/16”}resource “aws_subnet” “production_subnet” {  vpc_id = “${aws_vpc.production_vpc.id}”  cidr_block = “10.0.1.0/24”}resource “aws_security_group” “production_access” {  name = “production_access”  vpc_id = “${aws_vpc.production_vpc.id}”}resource “aws_instance” “production_server” {  instance_type = “t2.micro”  ami = “ami-ac772edf”  vpc_security_group_ids = [“${aws_security_group.production_access.id}”]  subnet_id = “${aws_subnet.production_subnet.id}”}

A Separate stack definition for each environment

./our-project/staging/main.tf./our-project/production/main.tf
Each stack instance defined in its own stack definition

One stack definition managed with a pipeline

A single definition file used to create multiple stack instances in a pipeline
Basic flow of a stack definition through a pipeline

Benefits

Artefact repository, promotion, and versioning

aws s3 sync ./our-project/ s3://our-project-repository/1.0.123/
aws s3 sync — delete \  s3://our-project-repository/1.0.123/ \  s3://our-project-repository/staging/

Running the tool

Developer workflow

When is this appropriate?

Beyond pipelines

Acknowledgements

--

--

Cloud Practice Lead at ThoughtWorks. Author of “Infrastructure as Code”, from O’Reilly.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store