terraform introduction

34
Terraform Sonia Hamilton 1 / 34

Upload: soniasnowfrog

Post on 15-Jul-2015

993 views

Category:

Software


6 download

TRANSCRIPT

Page 1: Terraform Introduction

Terraform

Sonia Hamilton

1 / 34

Page 2: Terraform Introduction

Provisioninginstances

storage

dns

2 / 34

Page 3: Terraform Introduction

In Relation To...Puppet, Salt

Boto, Fog

CloudFormation

Vagrant?

3 / 34

Page 4: Terraform Introduction

Featuresexecution plans

dependencies

state

4 / 34

Page 5: Terraform Introduction

Example ProvidersAWS

Heroku

Google Cloud

DNSimple

no VCloud, yet...

5 / 34

Page 6: Terraform Introduction

Terraform and GoTerraform is written in Go (golang)

dev version

http://blog.snowfrog.net/2014/12/04/building-the-development-version-of-terraform/

http://bit.ly/1vNjuIU

docs -- grep

6 / 34

Page 7: Terraform Introduction

Using a Providerprovider "aws" {

access_key = "ACCESS_KEY_HERE"

secret_key = "SECRET_KEY_HERE"

region = "us-east-1"

}

resource "aws_instance" "example" {

ami = "ami-408c7f28"

instance_type = "t1.micro"}

7 / 34

Page 8: Terraform Introduction

Terraform Plan$ terraform plan...

+ aws_instance.example ami: "" => "ami-408c7f28" availability_zone: "" => "<computed>" instance_type: "" => "t1.micro" key_name: "" => "<computed>" private_dns: "" => "<computed>" private_ip: "" => "<computed>" public_dns: "" => "<computed>" public_ip: "" => "<computed>" security_groups: "" => "<computed>" subnet_id: "" => "<computed>"

8 / 34

Page 9: Terraform Introduction

Terraform Apply$ terraform applyaws_instance.example: Creating... ami: "" => "ami-408c7f28" instance_type: "" => "t1.micro"

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

...

9 / 34

Page 10: Terraform Introduction

terraform.tfstateJSON file

Terraform Show

$ terraform showaws_instance.example: id = i-e60900cd ami = ami-408c7f28 availability_zone = us-east-1c instance_type = t1.micro key_name = private_dns = domU-12-31-39-12-38-AB.compute-1.internal private_ip = 10.200.59.89 public_dns = ec2-54-81-21-192.compute-1.amazonaws.com public_ip = 54.81.21.192 security_groups.# = 1 security_groups.0 = default subnet_id =

10 / 34

Page 11: Terraform Introduction

Plan/Apply - Videoprovider "aws" {

access_key = "AKIA................"

secret_key = "puOt...................................."

region = "ap-southeast-2"

}

resource "aws_instance" "example" {

key_name = "sonia" # AWS keypair name

ami = "ami-9b0b62a1" # Ubuntu 14.04

instance_type = "t1.micro"}

demo01

11 / 34

Page 12: Terraform Introduction

Changing Infrastructure

Edit

resource "aws_instance" "example" { ami = "ami-aa7ab6c2" # NEW AMI instance_type = "t1.micro"}

12 / 34

Page 13: Terraform Introduction

Changing Infrastructure

Plan

$ terraform plan...

-/+ aws_instance.example ami: "ami-408c7f28" => "ami-aa7ab6c2" (forces new resource) availability_zone: "us-east-1c" => "<computed>"...

13 / 34

Page 14: Terraform Introduction

Changing Infrastructure

Apply

$ terraform applyaws_instance.example: Destroying...aws_instance.example: Modifying... ami: "ami-408c7f28" => "ami-aa7ab6c2"

Apply complete! Resources: 0 added, 1 changed, 1 destroyed....

14 / 34

Page 15: Terraform Introduction

Destroy Infrastructure

Plan

$ terraform plan -destroy...

- aws_instance.example

15 / 34

Page 16: Terraform Introduction

Destroy$ terraform destroyaws_instance.example: Destroying...

Apply complete! Resources: 0 added, 0 changed, 1 destroyed.

...

16 / 34

Page 17: Terraform Introduction

Changing Infrastructure - Videoprovider "aws" { access_key = "AKIA................" secret_key = "puOt...................................." region = "ap-southeast-2"}

resource "aws_instance" "example" { key_name = "sonia" # AWS keypair name ami = "ami-950b62af" # Ubuntu 14.04 instance_type = "t2.small" # CHANGED}

demo02

17 / 34

Page 18: Terraform Introduction

Resource Dependenciesmultiple providers

multiple resource types

do x before y, and y needs x's ip address

18 / 34

Page 19: Terraform Introduction

Resource Dependenciesresource "aws_eip" "ip" { instance = "${aws_instance.example.id}" # IMPLICIT}

Plan

$ terraform plan...+ aws_eip.ip instance: "" => "${aws_instance.example.id}" private_ip: "" => "<computed>" public_ip: "" => "<computed>"

+ aws_instance.example ami: "" => "ami-aa7ab6c2"...

19 / 34

Page 20: Terraform Introduction

Resource Dependencies

Apply

aws_instance.example: Creating... ami: "" => "ami-aa7ab6c2" instance_type: "" => "t1.micro"aws_eip.ip: Creating... instance: "" => "i-0e737b25"

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Run order is different from output order.

20 / 34

Page 21: Terraform Introduction

Explicit Dependenciesresource "aws_eip" "ip" { instance = "${aws_instance.example.id}" depends_on = ["aws_instance.example"]}

terraform graph

21 / 34

Page 22: Terraform Introduction

Dependencies - Videoprovider "aws" {

access_key = "AKIA................"

secret_key = "puOt...................................."

region = "ap-southeast-2"

}

resource "aws_instance" "example" {

key_name = "sonia" # AWS keypair name

ami = "ami-9b0b62a1" # Ubuntu 14.04

instance_type = "t1.micro"}

resource "aws_eip" "example_eip" {

instance = "${aws_instance.example.id}"}

demo03

22 / 34

Page 23: Terraform Introduction

Provisionersconnection

file

local-exec

remote-exec

23 / 34

Page 24: Terraform Introduction

Connection/File Provisioner - Videoprovider "aws" {

access_key = "AKIA................"

secret_key = "puOt...................................."

region = "ap-southeast-2"

}

resource "aws_instance" "example" {

key_name = "sonia" # AWS keypair name

ami = "ami-9b0b62a1" # Ubuntu 14.04

instance_type = "t1.micro"

connection {

user = "ubuntu"

host = "${aws_instance.example.public_ip}" key_file = "../include/sonia.pem"

}

provisioner "file" {

source = "bootstrap" destination = "/var/tmp"

}

}

demo04

24 / 34

Page 25: Terraform Introduction

Exec Provisioners - Videoprovider "aws" {

access_key = "AKIA................"

secret_key = "puOt...................................."

region = "ap-southeast-2"

}

resource "aws_instance" "example" {

key_name = "sonia" # AWS keypair name

ami = "ami-9b0b62a1" # Ubuntu 14.04

instance_type = "t1.micro" connection {

user = "ubuntu"

host = "${aws_instance.example.public_ip}" key_file = "../include/sonia.pem"

}

provisioner "local-exec" {

command = "echo ${aws_instance.example.public_ip} > ip.txt" }

provisioner "remote-exec" {

inline = [ "sudo locale-gen en_AU.UTF-8" ]

}

provisioner "remote-exec" {

script = "script1.sh"

}

}

demo0525 / 34

Page 26: Terraform Introduction

'Advanced'input, output variables

modules

26 / 34

Page 27: Terraform Introduction

Input VariablesCommonly named variables.tf, uses all *.tf files.

variable "access_key" {}variable "secret_key" {}variable "region" { default = "us-east-1"}

27 / 34

Page 28: Terraform Introduction

Using Variablesprovider "aws" { access_key = "${var.access_key}" secret_key = "${var.secret_key}" region = "${var.region}"}

28 / 34

Page 29: Terraform Introduction

Assigning Variables

Command Line

$ terraform apply \ -var 'access_key=foo' \ -var 'secret_key=bar'

File

terraform.tfvars or use -var-file

access_key = "foo"secret_key = "bar"

29 / 34

Page 30: Terraform Introduction

Outputs Variables

Defining

output "ip" {

value = "${aws_eip.ip.public_ip}"}

Viewing

$ terraform apply

...

Apply complete! Resources: 0 added, 0 changed, 0 destroyed.

Outputs:

ip = 50.17.232.209

30 / 34

Page 31: Terraform Introduction

Modules

Create

$ mkdir child$ touch child/main.tf

Use

module "child" { source = "./child"}

31 / 34

Page 32: Terraform Introduction

Module Parameters - Input

Caller

module "child" { source = "./child" memory = "1G"}

Child

variable "memory" {}resource "aws_db_instance" "default" { ... allocated_storage = "${var.memory}" ...}

32 / 34

Page 33: Terraform Introduction

Module Parameters - Output

Child

output "result" { value = "${foo.region}-${bar.city}"}

Caller

module "child" { ... }resource "aws_instance" "web" { ... tags { Location = "${module.child.result}" } ...}

33 / 34

Page 34: Terraform Introduction

Questions

Sonia Hamilton

[email protected]

34 / 34