Slide 1

Slide 1 text

1 Using TDD For Infrastructure as Code Confidence when moving to the cloud Alina Murphy & Pat Dale

Slide 2

Slide 2 text

2 Who are we?!

Slide 3

Slide 3 text

WHO ARE WE?! 3 Pat Dale Consultant at ThoughtWorks Pairs well with whiskey - Likes climbing rocks - San Franciscan Alina Murphy Consultant at ThoughtWorks Sweets monster - General bear expert - New Yorker

Slide 4

Slide 4 text

WHAT BROUGHT US HERE? A client case study… 4

Slide 5

Slide 5 text

5 Tribal knowledge

Slide 6

Slide 6 text

6 Deconstructed pipelines

Slide 7

Slide 7 text

7 No tests :(

Slide 8

Slide 8 text

HOW DID WE HELP SOLVE THIS? A test driven approach to infrastructure as code 8

Slide 9

Slide 9 text

A TEST DRIVEN APPROACH TO INFRA AS CODE What do we mean by infrastructure as code? 9 To sum it up in two words: “Automate everything” We want to treat our computing, networking, and infrastructure code the same as any other piece of software. This will allow us to more easily test, build, and deploy changes at a faster and more incremental pace as well as keep our existing services readily available, even during deployments or upgrades.

Slide 10

Slide 10 text

TDD AND INFRASTRUCTURE AS CODE What is Test Driven Development? (TDD) 10 The mantra goes, “Red, Green, Refactor” TDD helps us write production code incrementally which in turn reduces the complexity of our systems. Our tests also help describe the intent of our production code — you can think of them as living and breathing documentation. And last but not least, it ensures all of our production code is sufficiently tested.

Slide 11

Slide 11 text

WHAT DOES THIS LOOK LIKE IN PRACTICE? An example of testing driven infrastructure as code 11

Slide 12

Slide 12 text

AN EXAMPLE OF TESTING INFRA AS CODE Securing a build pipeline 12 Story: As a developer I want to ensure that my build server can only be accessed internally So that my company continues to practice proper security standards

Slide 13

Slide 13 text

AN EXAMPLE OF TESTING INFRA AS CODE Securing a build pipeline 13 Our source code repository is modeled in a similar way to any application source code repo: we have a production code folder, terraform/concourse, and a test code folder, tests/concourse. Our tests use a tool called aws_spec which dictates the structure of our test folder

Slide 14

Slide 14 text

AN EXAMPLE OF TESTING INFRA AS CODE Securing a build pipeline 14 Our main.tf file currently provisions a Concourse CI (pipeline) instance as well as a Postgres database to store our build history. As we can see, we currently do not force our pipeline to be a part of any security group so we could be open to vulnerabilities.

Slide 15

Slide 15 text

AN EXAMPLE OF TESTING INFRA AS CODE Securing a build pipeline 15 Luckily we have teammates who have introduced a testing harness already. W00t! Our aws_spec.rb file describes how any piece of infrastructure that we provision will behave. Let’s begin our story following the TDD pattern of Red, Green, Refactor.

Slide 16

Slide 16 text

16 STEP 1 Write a failing test Let’s write a failing test to introduce a specific security group to our build pipeline server. The aws_spec testing framework gives us a nice interface to describe our infrastructure, and easily add tests to new or existing pieces of infrastructure. Our new test dictates that our build pipeline server should belong to a specific security group.

Slide 17

Slide 17 text

17 STEP 2 Make the test pass We now need to provision our build pipeline server with a specific security group in order for our test to pass. Easy enough…

Slide 18

Slide 18 text

18 STEP 3 Refactor Our teammate who we are working with points out that we can improve our code by making it more modular.

Slide 19

Slide 19 text

HOW DOES THIS HELP US ACHIEVE OUR ORIGINAL GOALS? 19

Slide 20

Slide 20 text

20 Tests dictate the intent of our infrastructure, and will protect this intent when modifying our IaC in the future

Slide 21

Slide 21 text

21 No more snowflake environments!

Slide 22

Slide 22 text

22 IaC reduces tribal knowledge and enables wholistic ownership over app environments

Slide 23

Slide 23 text

23 THANK YOU! Questions? Alina Murphy [email protected] Pat Dale [email protected]