Bringing software development practices to your infrastructure

A presentation at HashiTalks: Canada in November 2021 in by Jenna Pederson

Slide 1

Slide 1

#HASHITALKS: CANADA Bringing software development practices to your infrastructure @jennapederson

Slide 2

Slide 2

Fun Fact I once had the phrase “automated test fanatic” on my business card. @jennapederson

Slide 3

Slide 3

The awesomeness of Infrastructure as Code @jennapederson

Slide 4

Slide 4

Infrastructure as Code IS Code @jennapederson

Slide 5

Slide 5

wo on m e s k r my ac n hi (or account or region) @jennapederson

Slide 6

Slide 6

Different Types of Testing Agenda Using the right type at the right time Using Test Driven Development Build what you need and only what you need Testing Your Infrastructure Directly Making sure it was created correctly and hasn’t drifted Using a CI/CD Pipeline Run tests in the real world and isolate issues quicker @jennapederson

Slide 7

Slide 7

Why Test Infrastructure? The cloud makes it easier and quicker to provision infrastructure, but there is complexity with that scale. @jennapederson

Slide 8

Slide 8

Slow + Expensive Manual Tests Failing Fast Balance fast and cheap tests with more expensive tests that are closer to the real infrastructure and production environment. System Tests Integration Tests Contract Tests Unit Tests Fast + Cheap

Slide 9

Slide 9

If you’re TDDing your application code, why not do the same for your infrastructure code? @jennapederson

Slide 10

Slide 10

Benefits of TDD Reduced defect rates Improve the overall design Focused on requirements Focused on small chunks Serves as documentation Confidence @jennapederson

Slide 11

Slide 11

The Flow RED REFACTOR 3. Make it better

  1. Write a failing test GREEN GREEN
  2. Write only enough code to make it pass

Slide 12

Slide 12

What is a unit test? Exercises a small part of your application, one unit, and verifies that it’s correct. Get feedback early on to shorten the feedback loop between changes Serves as documentation Can be run in your CI/CD tool Isolated from other resources and external APIs @jennapederson

Slide 13

Slide 13

Unit Testing Infrastructure Code Code. Not infrastructure. @jennapederson

Slide 14

Slide 14

A unit test checks: If a resource will be created with the correct configuration The correct number of resources will be created Dependencies between resources are correct Interpolated values are correct @jennapederson

Slide 15

Slide 15

Demo S3 + CDK + Jest @jennapederson

Slide 16

Slide 16

Slide 17

Slide 17

How do we go from code to infrastructure?

Slide 18

Slide 18

What is an Integration Test? Tests the interactions across different units or modules, or in the case of infrastructure testing, across cloud resources. Verifies your provisioned cloud resources are created and configured as you expect them to be. Gives you confidence in infrastructure at scale and at velocity. @jennapederson

Slide 19

Slide 19

Chef InSpec Open-source framework to test and audit cloud resources IN the cloud Tests are written with a DSL Can be used across teams Test resources that are managed manually or with code Ensures requirements are met at every stage of the SDLC @jennapederson

Slide 20

Slide 20

Demo EC2 + RDS + CDK + InSpec @jennapederson

Slide 21

Slide 21

Slide 22

Slide 22

Detecting Drift Use InSpec to compare the desired state with the actual state of your cloud resources. Can be used against any resources, regardless of how they are managed. @jennapederson

Slide 23

Slide 23

Without CI/CD Development Test Staging Production @jennapederson

Slide 24

Slide 24

With CI/CD Development Test Staging Production @jennapederson

Slide 25

Slide 25

Wrapping Up Infrastructure code is like any other code, treat it as such. Testing is never done, even once you reach production. It’s cheaper to detect broken code early. @jennapederson

Slide 26

Slide 26

Thank you! @jennapederson /in/jennapederson jennapederson https://jenna.link/hq7