hashidays london 2017 - evolving your infrastructure with terraform by nicki watt

101
EVOLVING YOUR INFRASTRUCTURE WITH TERRAFORM Nicki Watt - CTO @techiewatt 12-06-2017

Upload: opencredo

Post on 16-Mar-2018

1.390 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

EVOLVING YOUR INFRASTRUCTURE WITH TERRAFORM

Nicki Watt - CTO@techiewatt

12-06-2017

Page 2: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

ABOUT ME / OPENCREDO

▸OpenCredo CTO

▸Premiere HashiCorp partner

▸Hands on software development consultancy

▸Cloud, Data Engineering, DevSecOps

2

Page 3: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

AGENDA

▸Evolving your Terraform

▸Orchestrating your Terraform

▸Conclusion

3

Page 4: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

4

Evolving your Terraform (a journey from a client’s perspective)

Page 5: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

6

Example: E-Commerce System in AWS(delivered as a Micro-services architecture)

Page 6: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

7

Sample System

Simple

Kubernetes (K8S)

Environment

Page 7: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

8public DMZ & Bastion Box

k8s clusterSample System

Simple

Kubernetes (K8S)

Environment

database (RDS)

Page 8: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

9

Pass #1 -

In the beginning …

Page 9: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

10

https://github.com/mycompany/myproject terraform.tf

## Test VPCresource "aws_vpc" "test" { cidr_block = "10.0.0.0/21" enable_dns_support = true enable_dns_hostnames = true}

## Staging Bastionresource "aws_instance" “test_bastion" { ami = "ami-7abd5555" instance_type = "t2.large" . . .}

- terraform-prod.tf - terraform.tf - terraform.tfvars - terraform.tfstate

Page 10: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

11

We must to go to production this week …

Page 11: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

terraform.tf

## Test VPCresource "aws_vpc" "test" { cidr_block = "10.0.0.0/21" enable_dns_support = true enable_dns_hostnames = true}

## Staging Bastionresource "aws_instance" “test_bastion" { ami = "ami-7abd5555" instance_type = "t2.large" . . .}

## Prod VPCresource "aws_vpc" "prod" { cidr_block = "172.16.0.0/21" enable_dns_support = true enable_dns_hostnames = true}

12

https://github.com/mycompany/myproject

- terraform-prod.tfbkp - terraform.tf - terraform.tfvars - terraform.tfstate

Page 12: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

terraform-test.tf

## Test VPCresource "aws_vpc" "test" { cidr_block = "10.0.0.0/21" enable_dns_support = true enable_dns_hostnames = true}

## Staging Bast-ionresource "aws_instance" “test_bastion" { ami = "ami-7abd5555" instance_type = "t2.large" . . .}

## Prod VPCresource "aws_vpc" "prod" { cidr_block = "10.0.0.3/24" enable_dns_support = true enable_dns_hostnames = true}

13

https://github.com/mycompany/myproject

terraform-prod.tf

## Prod VPCresource "aws_vpc" "prod" { cidr_block = "172.16.0.0/21" enable_dns_support = true enable_dns_hostnames = true}

## Staging Bastionresource "aws_instance" “prod_bastion" { ami = "ami-7abd5555" instance_type = "t2.large" . . .

- terraform-prod.tf - terraform-test.tf - terraform.tfvars - terraform.tfstate

Page 13: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

14

Need an upgraded CIDR range in TEST …

Page 14: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

15

- terraform-prod.tfbkp - terraform-test.tf - terraform.tfvars - terraform.tfstate

https://github.com/mycompany/myproject terraform-test.tf

## Test VPCresource "aws_vpc" "test" { cidr_block = "10.0.0.0/21" enable_dns_support = true enable_dns_hostnames = true}

## Staging Bast-ionresource "aws_instance" “test_bastion" { ami = "ami-7abd5555" instance_type = "t2.large" . . .}

## Prod VPCresource "aws_vpc" "prod" { cidr_block = "10.0.0.3/24" enable_dns_support = true enable_dns_hostnames = true}

terraform-prod.tf

## Prod VPCresource "aws_vpc" "prod" { cidr_block = "172.16.0.0/21" enable_dns_support = true enable_dns_hostnames = true}

## Staging Bastionresource "aws_instance" “prod_bastion" { ami = "ami-7abd5555" instance_type = "t2.large" . . . 15

Page 15: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

16

Help!

I seem to have deleted production

Page 16: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

17“terralith"

https://sites.google.com/site/laurenmcnanyspln/magnetic-fields

Page 17: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Single state file

▸Single definition file

▸Hard coded config

▸Local state

Terralith: Characteristics

18

Page 18: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Can’t manage environments separately

▸Config not that intuitive (big ball of mud)

▸Maintenance challenge: Duplicate Defs (not DRY)

Terralith - Pain points

19

Page 19: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

20

Pass #2

Page 20: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

21“multi terralith"

https://sites.google.com/site/laurenmcnanyspln/magnetic-fields

Page 21: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Envs - Separate State Management

▸Multiple Terraform Definition Files

▸Better Use of Variables

Multi Terralith: Characteristics

22

Page 22: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ test - networks.tf - vms.tf - terraform.tfvars - terraform.tfstate

23

https://github.com/mycompany/myproject

networks.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = true enable_dns_hostnames = true}

vms.tf

resource "aws_instance" "node" { count = "${var.node_count}" ami = "ami-7abd5555" instance_type = “${var.vm_type}” . . .}

+ prod - networks.tf - vms.tf - terraform.tfvars - terraform.tfstate

Page 23: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

24

https://github.com/mycompany/myproject

networks.tf

resource "aws_vpc" "core" { cidr_block = “${var.cidr}” enable_dns_support = true enable_dns_hostnames = true}

vms.tf

resource "aws_instance" "node" { count = "${var.node_count}" ami = "ami-7abd5555" instance_type = “${var.vm_type}” . . .}

+ test - networks.tf - vms.tf - terraform.tfvars - terraform.tfstate

+ prod - networks.tf - vms.tf - terraform.tfvars - terraform.tfstate

Page 24: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ test - networks.tf - vms.tf - terraform.tfvars - terraform.tfstate

25

https://github.com/mycompany/myproject

networks.tf

resource "aws_vpc" "core" { cidr_block = “${var.cidr}” enable_dns_support = true enable_dns_hostnames = true}

vms.tf

resource "aws_instance" "node" { count = "${var.node_count}" ami = "ami-7abd5555" instance_type = “${var.vm_type}” . . .}

+ prod - networks.tf - vms.tf - terraform.tfvars - terraform.tfstate

Page 25: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

Terralith - (recap)

26

▸Can’t manage environments separately

▸Config not that intuitive (big ball of mud)

▸Maintenance challenge: Duplicate Defs (not DRY)

Page 26: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

Multi Terralith

27

▸Manage environment separately (separate state files per env)

▸More intuitive configuration(multiple files)

▸Maintenance challenge: Duplicate Defs (not DRY)

"

Page 27: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

28

Pass #3

Page 28: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

29

“terramod"

Alan Chia (https://commons.wikimedia.org/wiki/File:Lego_Color_Bricks.jpg)

Page 29: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Reusable modules

▸Envs compose themselves from modules

▸Restructuring of repo

30

Terramod: Characteristics

Page 30: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

31

database

core

k8s-cluster

Page 31: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

32

database

core

k8s-cluster

- VPC- All Subnets- Core Routing & Gateways- Bastion Host (OpenVPN server)

- Instances- Security Groups

- Amazon RDS- DB Subnet Group

Page 32: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

33

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

https://github.com/mycompany/myproject

separate env management & module defs

Page 33: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

34

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

https://github.com/mycompany/myproject

define logical components as re-usable modules

Page 34: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

35

https://github.com/mycompany/myproject core.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = "${var.dns}" enable_dns_hostnames = "${var.dnsh}"}

resource "aws_subnet" "dmz" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.dmz_cidr}" map_public_ip_on_launch = 1 ... }

resource "aws_subnet" "private" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.priv_cidr}" ... }

Page 35: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

36

https://github.com/mycompany/myproject core.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = "${var.dns}" enable_dns_hostnames = "${var.dnsh}"}

resource "aws_subnet" "dmz" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.dmz_cidr}" map_public_ip_on_launch = 1 ... }

resource "aws_subnet" "private" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.priv_cidr}" ... }

input.tf

variable "cidr" {}variable "dns” {}variable "dnsh" {}variable "dmz_cidr" {}variable "priv_cidr" {}

...

Page 36: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

37

https://github.com/mycompany/myproject core.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = "${var.dns}" enable_dns_hostnames = "${var.dnsh}"}

resource "aws_subnet" "dmz" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.dmz_cidr}" map_public_ip_on_launch = 1 ... }

resource "aws_subnet" "private" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.priv_cidr}" ... }

input.tf

variable "cidr" {}variable "dns” {}variable "dnsh" {}variable "dmz_cidr" {}variable "priv_cidr" {}

...

output.tf

output "priv_subnet_id" { value ="${aws_subnet.private.id}"}

...

Page 37: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

38

https://github.com/mycompany/myproject

defines the contract of the module

Page 38: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

39

https://github.com/mycompany/myproject terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}"

}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" priv_subnet = "${module.core.priv_subnet_id}" }

Page 39: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

40

https://github.com/mycompany/myproject terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}"

}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" priv_subnet = "${module.core.priv_subnet_id}" }

Page 40: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/[test|prod] - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

41

https://github.com/mycompany/myproject terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}"

}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" priv_subnet = "${module.core.priv_subnet_id}" }

Page 41: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Manage environment separately (separate state files per env)

▸More intuitive configuration(multiple files)

▸Maintenance challenge: Duplicate Defs (not DRY)

Multi Terralith

42

"

Page 42: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Manage environment separately (separate state files per env)

▸ Intuitive configuration(reusable modules)

▸Reduced Duplicate Definitions (DRYer)

Terramod

43

"

Page 43: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

44

Pass #4

Page 44: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

45

Alan Chia (https://commons.wikimedia.org/wiki/File:Lego_Color_Bricks.jpg)

terramod

Marcos Leal (https://commons.wikimedia.org/wiki/File:Army_(2995294027).jpg)

n

Page 45: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Nested modules

▸base modules(low level infrastructure specific)

▸ logical modules(system specific)

▸Sometimes dedicated module repo46

Terramod : Characteristics n

Page 46: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

47

https://github.com/mycompany/myproject

+ envs + modules + project + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - k8s.tf - output.tf

logical (system specific) modules

Page 47: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

48

https://github.com/mycompany/myproject

+ envs + modules + common + aws + network + vpc + pub_subnet + priv_subnet + comps + instance + db-instance

+ envs + modules + project + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - k8s.tf - output.tf

logical (system specific) modules base (infra specific) modules

Page 48: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

49

https://github.com/mycompany/myproject

+ envs + modules + common + aws + network + vpc + pub_subnet + priv_subnet + comps + instance + db-instance

+ envs + modules + project + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - k8s.tf - output.tf

modules/project/core/core.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = "${var.dns}" enable_dns_hostnames = "${var.dnsh}"}

resource "aws_subnet" "dmz" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.dmz_cidr}" map_public_ip_on_launch = 1 ... }

Page 49: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

modules/project/core/core.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = "${var.dns}" enable_dns_hostnames = "${var.dnsh}"}

resource "aws_subnet" "dmz" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.dmz_cidr}" map_public_ip_on_launch = 1 ... }

+ envs + modules + project + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - k8s.tf - output.tf

50

https://github.com/mycompany/myproject

Page 50: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

modules/project/core/core.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = "${var.dns}" enable_dns_hostnames = "${var.dnsh}"}

resource "aws_subnet" "dmz" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.dmz_cidr}" map_public_ip_on_launch = 1 ... }

+ envs + modules + common + aws + network + vpc + pub_subnet + priv_subnet + comps + instance + db-instance

50

https://github.com/mycompany/myproject

modules/project/core/core.tf

module "vpc" { source = "../../common/aws/net/vpc" cidr = "${var.vpc_cidr}"}

module "dmz-subnet" { source = "../../common/aws/net/pub-subnet" vpc_id = "${module.vpc.vpc_id}" subnet_cidrs = [ “${var.dmz_cidr}” ]}

module "priv-subnet" { source = "../../common/aws/net/priv-subnet" vpc_id = "${module.vpc.vpc_id}" subnet_cidrs = [ “${var.priv_cidr}” ]

Page 51: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs + modules + common + aws + network + vpc + pub_subnet + priv_subnet + comps + instance + db-instance

modules/project/core/core.tf

resource "aws_vpc" "core" { cidr_block = "${var.cidr}" enable_dns_support = "${var.dns}" enable_dns_hostnames = "${var.dnsh}"}

resource "aws_subnet" "dmz" { vpc_id = "${aws_vpc.core.id}" cidr_block = "${var.dmz_cidr}" map_public_ip_on_launch = 1 ... }

51

https://github.com/mycompany/myproject

modules/project/core/core.tf

module "vpc" { source = "../../common/aws/net/vpc" cidr = "${var.vpc_cidr}"}

module "dmz-subnet" { source = "../../common/aws/net/pub-subnet" vpc_id = "${module.vpc.vpc_id}" subnet_cidrs = [ “${var.dmz_cidr}” ]}

module "priv-subnet" { source = "../../common/aws/net/priv-subnet" vpc_id = "${module.vpc.vpc_id}" subnet_cidrs = [ “${var.priv_cidr}” ]

BUT …Issue #953 - Support the count parameter for modules

Page 52: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Manage environment separately (separate state files per env)

▸ Intuitive configuration(reusable modules)

▸Reduced Duplicate Definitions (DRYer)

Terramod (recap)

52

"

Page 53: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Manage environment separately (separate state files per env)

▸ Intuitive configuration(reusable modules)

▸Reduced Duplicate Definitions further (as DRY as possible given restrictions)

Terramod

53

n

"

Page 54: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

54

Time goes on …

Page 55: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

55

Maintenance required … - Make bastion box smaller -

Page 56: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/prod - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

56

terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}" bastion_flav = "${var.bastion_flav}"}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" node_flavour = "${var.bastion_flav}" }

terraform.tfvars

vpc_cidr = “10.0.0.0/21”bastion_flav = “r4.large”node_flavour = “m4.4xlarge”

Page 57: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/prod - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

57

terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}" bastion_flav = "${var.bastion_flav}"}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" node_flavour = "${var.bastion_flav}" }

terraform.tfvars

vpc_cidr = “10.0.0.0/21”bastion_flav = “m4.large”node_flavour = “m4.4xlarge”

Page 58: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/prod - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

58

terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}" bastion_flav = "${var.bastion_flav}"}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" node_flavour = "${var.bastion_flav}" }

terraform.tfvars

vpc_cidr = “10.0.0.0/21”bastion_flav = “m4.large”node_flavour = “m4.4xlarge”

Page 59: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

59

Help!

I seem to be rebuilding the K8S cluster!

Page 60: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs/prod - config.tf - terraform.tf - terraform.tfvars - terraform.tfstate+ modules + core - input.tf - core.tf - output.tf + k8s-cluster - input.tf - dns.tf - vms.tf - output.tf

60

terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}" bastion_flav = "${var.bastion_flav}"}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" node_flavour = "${var.bastion_flav}" }

terraform.tfvars

vpc_cidr = “10.0.0.0/21”bastion_flav = “m4.large”node_flavour = “m4.4xlarge”

OOPS! Typo

Page 61: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸Can’t manage logical parts of our infrastructure independently

Next set of pain!

61

Page 62: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

62

Pass #5

Page 63: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

63

“terraservices"

https://commons.wikimedia.org/wiki/File:Caffeine_Molecule.png

Page 64: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

▸ Independent management of logical comps

▸ Isolates & Reduces Risk

▸ Aids with Multi Team Setups

▸Distributed (Remote State)

▸Requires additional orchestration effort

Terraservices - Characteristics

64

Page 65: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

65

database

core

k8s-cluster

- VPC- All Subnets- Core Routing & Gateways- Bastion Host (OpenVPN server)

- Instances- Security Groups

- Amazon RDS- DB Subnet Group

Page 66: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs + test - ... - ... - ... + k8s-cluster - ... + prod + core - ... - ... - ... + k8s-cluster - ...

66

- terraform.tfstate - terraform.tfvars - xxx.tf

Terraservices - Repo Structure

From

Page 67: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

67

- terraform.tfstate - terraform.tfvars - xxx.tf

To

+ envs + test + core - ... + database - ... + k8s-cluster - ... + prod + core - ... + database - ... + k8s-cluster - ...

Terraservices - Repo Structure

Page 68: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

+ envs + test - ... - ... - ... + k8s-cluster - ... + prod + core - ... - ... - ... + k8s-cluster - ...

68

envs/test/terraform.tf

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}" dmz_cidr = "${var.dmz_cidr}" priv_cidr = "${var.priv_cidr}"

}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" priv_subnet = "${module.core.priv_subnet_id}" }

Terramod - Connecting (recap)

From

Page 69: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

69

+ envs + test + core - ... + database - ... + k8s-cluster - ... + prod + core - ... + database - ... + k8s-cluster - ...

envs/test/core/terraform.tf

# Optional but explicit! (Needs 0.9+) terraform { backend "local" { path = "terraform.tfstate" }}

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}"

envs/test/core/outputs.tf

output "priv_subnet_id" { value ="${module.core.priv_subnet_id}"}

Terraservices - Connecting

To

Page 70: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

70

+ envs + test + core - ... + database - ... + k8s-cluster - ... + prod + core - ... + database - ... + k8s-cluster - ...

envs/test/core/terraform.tf

# Optional but explicit! (Needs 0.9+) terraform { backend "local" { path = "terraform.tfstate" }}

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}"

envs/test/core/outputs.tf

output "priv_subnet_id" { value ="${aws_subnet.private.id}"}

envs/test/k8s-cluster/terraform.tf

data "terraform_remote_state" "core" { backend = "local" config { path = “../core/terraform.tfstate" }}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" priv_subnet = “${data.terraform_remote_ state.core.priv_subnet_id}" } }

Terraservices - Connecting

To

Page 71: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

Terraservices - Characteristics

71

▸ Independent management of logical comps

▸ Isolates & Reduces Risk

▸ Aids with Multi Team Setups

▸Distributed (Remote State)

▸Requires additional orchestration effort

Page 72: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

72

+ envs + test + core - ... + database - ... + k8s-cluster - ... + prod + core - ... + database - ... + k8s-cluster - ...

envs/test/core/terraform.tf

# Optional but explicit! (Needs 0.9+) terraform { backend "local" { path = "terraform.tfstate" }}

module "core" { source = "../../modules/core" cidr = "${var.vpc_cidr}"

envs/test/core/outputs.tf

output "priv_subnet_id" { value ="${module.core.priv_subnet_id}"}

Terraservices - Distributed (Remote State)

From

Page 73: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

73

+ envs + test + core - ... + database - ... + k8s-cluster - ... + prod + core - ... + database - ... + k8s-cluster - ...

envs/test/core/terraform.tf

# Optional but explicit! (Needs 0.9+) terraform { backend "s3" { region = "eu-west-1" bucket = "myco/myproj/test" key = "core/terraform.tfstate" encrypt = "true" }}

envs/test/core/outputs.tf

output "priv_subnet_id" { value ="${module.core.priv_subnet_id}"}

Terraservices - Distributed (Remote State)

To

Page 74: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

74

+ envs + test + core - ... + database - ... + k8s-cluster - ... + prod + core - ... + database - ... + k8s-cluster - ...

envs/test/core/terraform.tf

# Optional but explicit! (Needs 0.9+) terraform { backend "s3" { region = "eu-west-1" bucket = "myco/myproj/test" key = "core/terraform.tfstate" encrypt = "true" }}

envs/test/core/outputs.tf

output "priv_subnet_id" { value ="${module.core.priv_subnet_id}"}

Terraservices - Distributed (Remote State)

To

envs/test/k8s-cluster/terraform.tf

data "terraform_remote_state" "core" { backend = "s3" config { region = "eu-west-1" bucket = "myco/myproj/test" key = "core/terraform.tfstate" encrypt = "true" }}

module "k8s-cluster" { source = "../../modules/k8s-cluster" num_nodes = "${var.k8s_nodes}" priv_subnet = “${data.terraform_remote_ state.core.priv_subnet_id}"

Page 75: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

75

+ envs + test|prod + core - ... + database - ... + k8s-cluster - ... + modules + common + aws + network + vpc + ...

Terraservices - Repo Isolation (Optional)

https://github.com/myco/myproj From

Page 76: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

76

+ envs + test|prod + core - ... + database - ... + k8s-cluster - ... + modules + common + aws + network + vpc + ...

Terraservices - Repo Isolation (Optional)

https://github.com/myco/myproj

https://github.com/myco/myproj-core

https://github.com/myco/myproj-db

https://github.com/myco/myproj-k8s

https://github.com/myco/tf-modcomm

To

Page 77: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

Terraservices - Characteristics

77

▸ Independent management of logical comps

▸ Isolates & Reduces Risk

▸ Aids with Multi Team Setups

▸Distributed (Remote State)

▸Requires additional orchestration effort

Page 78: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

78

Orchestrating your Terraform

Page 79: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

79

database

core

k8s-cluster

Page 80: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

80

database

core

k8s-cluster

Page 81: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

81

Orchestration System

Page 82: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

82

Orchestration System

Laptop, Local State & READMEs

Page 83: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

83

Orchestration System

Laptop, Local State & READMEs

Page 84: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

84

Orchestration System

Laptops, Local State & READMEs

Page 85: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

85

Orchestration System

Laptops, Local State & READMEs

Remote State

Page 86: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

86

Orchestration System

Laptops, Local State & READMEs

Remote State

Page 87: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

87

Orchestration System

Laptops, Remote State, Shared Services,

& READMEs

Page 88: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

88

Orchestration System

Who builds the infrastructure

that builds the infrastructure ?

Page 89: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

89

Orchestration System

Jenkins, Remote State,Custom Scripts, Shared Services,

& READMEs

Page 90: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

90

Orchestration System

Custom Systems & Tooling

Page 91: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

91

Orchestration System

SaaS Offerings (HashiCorp Enterprise

Products)

Page 92: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

92

It’s not just about the structure of the code …

You also need to evolve your supporting orchestration system &

processes

Page 93: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

93

Conclusion

Page 94: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

94

Evolving Terraform Setup

Page 95: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

95

EVOLVING YOUR TERRAFORM SETUP

▸Terralith

▸Multi Terralith - Envs: Independent management

▸Terramod

▸Terramod - Modules : Maintainability & Reuse

▸Terraservices - Logical Components: Independent management

n

Page 96: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

96

EVOLVING YOUR TERRAFORM SETUP

▸Terralith

▸Multi Terralith - Envs: Independent management

▸Terramod

▸Terramod - Modules : Maintainability & Reuse

▸Terraservices - Logical Components: Independent management

n

Page 97: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

97

EVOLVING YOUR TERRAFORM SETUP

▸Terralith

▸Multi Terralith - Envs: Independent management

▸Terramod

▸Terramod - Modules : Maintainability & Reuse

▸Terraservices - Logical Components: Independent management

n

Page 98: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

98

EVOLVING YOUR TERRAFORM SETUP

▸Terralith

▸Multi Terralith - Envs: Independent management

▸Terramod

▸Terramod - Modules : Maintainability & Reuse

▸Terraservices - Logical Components: Independent management

n

Page 99: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

99

EVOLVING YOUR TERRAFORM SETUP

▸Terralith

▸Multi Terralith - Envs: Independent management

▸Terramod

▸Terramod - Modules : Maintainability & Reuse

▸Terraservices - Logical Components: Independent management

n

Page 100: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

100

Also need to consider how to evolve the management & orchestration of Terraform

Page 101: Hashidays London 2017 - Evolving your Infrastructure with Terraform By Nicki Watt

101

Thanks!

@techiewatt