Introduction to IaC with - Event Schedule & Agenda...

57
Introduction to IaC with

Transcript of Introduction to IaC with - Event Schedule & Agenda...

Page 1: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Introduction to IaC with

Page 2: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Who is this workshop for?

2

Everyone whom deploy infrastructure in-house or cloud based environments. This is a beginner’s workshop

You will need to have an AWS account set up already with Terraform v0.9.3 installed. You will also need to have git install to download the workshop material.

https://www.terraform.iohttps://github.com/jasonvance/terraform-introductionhttps://aws.amazon.com/account/

Page 3: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Who am I?I am Jason Vance, Sr. Site Reliability Engineer for Accela, Inc.Graphic Designer turned System Administrator turned Engineer.

You can find me at @jasonsvance

3

Page 4: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

What is Infrastructure as Code (IaC)?

IaC grew as a response to the difficulty posed from two pieces of disruptive technology – utility computing and second-generation web frameworks.

4

Page 5: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

IaC isn't just automation

IaC is a CORE DevOps practice

5

Page 6: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

What IaC enables you to do:

■ Manage infrastructure via source control

■ Apply testing to infrastructure

■ Avoid written documentation of infrastructure

■ Enable collaboration

6

Page 7: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Mutable Infrastructure

vs.Immutable

Infrastructure

7

Page 8: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Configuration Drift...

8

Page 9: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

9

Page 10: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Procedural vs.

Declarative

10

Page 11: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“Declarative knowledge involves knowing THAT something is the case.

Procedural knowledge involves knowing HOW to do

something.

11

Page 12: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Client/Server Architecture

vs. Client-Only Architecture

12

Page 13: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Idempotence

13

Page 15: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Terraform syntax, internals,

and patterns

15

Page 16: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

HCLThe HashiCorp configuration language.

https://github.com/hashicorp/hcl

16

Page 17: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

The Terraform State File

17

Page 18: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Purpose of Terraform State

Mapping to the Real WorldTerraform requires some sort of database to map Terraform config to the real world.

MetadataTerraform needs to store more than just resource mappings. Terraform must keep track of metadata such as dependencies.

PerformanceIn addition to basic mapping, Terraform stores a cache of the attribute values for all resources in the state. This is the most optional feature of Terraform state and is done only as a performance improvement.

SyncingThe primary motivation people have for using remote state files is in an attempt to improve using Terraform with teams. State files can easily result in conflicts when two people modify infrastructure at the same time.

18

Page 19: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Json (Not me)

19

Page 20: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

20

Page 21: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Interpolation Syntax

VariablesStrings

Maps

Lists

ConditionalsThe support operators are:

Equality: == and !=Numerical comparison: >, <, >=, <=Boolean logic: &&, ||, unary !

FunctionsExamples:

concat(list1, list2, ...)

length(list)

log(x, base)

Math"${2 * 4 + 3 * 3}" # computes to 17"${3 * 3 + 2 * 4}" # computes to 17"${2 * (4 + 3) * 3}" # computes to 42.

21

Page 22: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

22

Page 23: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

AWS Account Setup

23

Page 24: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

24

Page 25: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Install Terraform

25

Page 26: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

26

Page 27: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

TerraformCommands

27

Page 28: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Single Server

28

Page 29: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Set up AWS Provider (main.tf)

provider "aws" { region = "us-east-1" access_key = "${var.access_key}" secret_key = "${var.secret_key}"}

29

Page 30: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Set up your key pair (main.tf)

resource "aws_key_pair" "site_key" { key_name = "id_rsa_slcdevopsdays" public_key = "${var.public_key}" lifecycle { create_before_destroy = false }}

30

Page 31: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Set up aws_instance (main.tf)

resource "aws_instance" "single_server" { count = 1 ami = "ami-500d8546" instance_type = "t2.micro"

tags { Name = "Hello-Word-${count.index}" }}

31

Page 32: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Add variables (vars.tf)

variable "access_key" {default = ""}variable "secret_key" {default = ""}variable "public_key" {default = ""}

32

Page 33: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform plan”

33

Page 34: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform apply”

34

Page 35: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Deploy a single web server

35

Page 36: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Deploy a web server

resource "aws_instance" "web_server" { ami = "ami-2d39803a" count = 1 instance_type = "t2.micro" user_data = <<-EOF

#!/bin/bash echo "Hello, Salt Lake City DevOps Days!" > index.html nohup busybox httpd -f -p 80 &

EOF

tags {

Name = "single-webserver"

}

}

36

Page 37: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Let’s open a Security Group

resource "aws_security_group" "web_server_sg" { name = "web_server_sg" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] } egress {

protocol = -1 from_port = 0 to_port = 0 cidr_blocks = ["0.0.0.0/0"] }}

37

Page 38: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Get the Public IP Address

output "public_ip" { value = "${aws_instance.web_server.public_ip}"}

38

Page 39: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform plan”

39

Page 40: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform apply”

40

Page 41: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Deploy a cluster of servers

41

Page 42: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Create a Launch Configuration

resource "aws_launch_configuration" "web_server_lc" { image_id = "ami-2d39803a" instance_type = "t2.micro" security_groups = ["${aws_security_group.web_server_sg.name}"] user_data = <<-EOF #!/bin/bash echo "Hello, Salt Lake City DevOps Days!" > index.html nohup busybox httpd -f -p 80 & EOF lifecycle { create_before_destroy = true }}

42

Page 43: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Add create_before_destry to the Security Group

resource "aws_security_group" "web_server_sg" { name = "web_server_sg" ingress { from_port = 80 to_port = 80 protocol = "tcp" cidr_blocks = ["0.0.0.0/0"] }

egress { protocol = -1 from_port = 0 to_port = 0 cidr_blocks = ["0.0.0.0/0"] } lifecycle { create_before_destroy = true }}

43

Page 44: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Create the Auto Scaling Group

resource "aws_autoscaling_group" "web_server_asg" { launch_configuration = "${aws_launch_configuration.web_server_lc.id}" availability_zones = ["${data.aws_availability_zones.all.names}"] min_size = 2 max_size = 10 tag { key = "Name" value = "terraform-asg-example" propagate_at_launch = true }}

data "aws_availability_zones" "all" {}

44

Page 45: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform plan”

45

Page 46: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform apply”

46

Page 47: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Deploy a load balancer

47

Page 48: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Add an ELB

resource "aws_elb" "web_server_elb" { name = "terraform-elb-example" security_groups = ["${aws_security_group.web_server_sg.id}"] availability_zones = ["${data.aws_availability_zones.all.names}"] health_check { healthy_threshold = 2 unhealthy_threshold = 2 timeout = 3 interval = 30 target = "HTTP:80/" } listener { lb_port = 80 lb_protocol = "http" instance_port = "80" instance_protocol = "http" }}

48

Page 49: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Update ASG

resource "aws_autoscaling_group" "web_server_asg" { launch_configuration = "${aws_launch_configuration.web_server_lc.id}" availability_zones = ["${data.aws_availability_zones.all.names}"]

load_balancers = ["${aws_elb.web_server_elb.name}"] health_check_type = "ELB"

min_size = 2 max_size = 10

tag { key = "Name" value = "terraform-asg-example" propagate_at_launch = true }}

49

Page 50: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Output ELB DNS Name

output "elb_dns_name" { value = "${aws_elb.web_server_elb.dns_name}"}

50

Page 51: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform plan”

51

Page 52: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

“terraform apply”

52

Page 53: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

(Bonus Time Permitting)Deploy Public/Private VPC with Bastion

53

Page 54: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Let’s Walk Through the Code:

54

Page 55: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Route 53 Management

55

Page 56: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

Let’s Walk Through the Code:

56

Page 57: Introduction to IaC with - Event Schedule & Agenda …schd.ws/hosted_files/slcdevopsdays2017/0d/Terraform...Code (IaC)? IaC grew as a response to the difficulty posed from two pieces

57

Thanks!Any questions?

Find me at @[email protected]