building infrastructure with terraform (google)

42
Building infrastructure with Terraform Radek Simko

Upload: radek-simko

Post on 16-Apr-2017

1.914 views

Category:

Technology


6 download

TRANSCRIPT

Page 1: Building infrastructure with Terraform (Google)

Building infrastructure with Terraform

Radek Simko

Page 2: Building infrastructure with Terraform (Google)

$ whoami

radeksimko}twitter.com/

google.com/+

linkedin.com/in/

github.com/

Page 3: Building infrastructure with Terraform (Google)
Page 4: Building infrastructure with Terraform (Google)

Provisioning of the past

● manual● Shell, Perl● extra knowledge required

○ bottleneck for team growth● pure-ops task

○ devs & ops talking over a wall

Page 5: Building infrastructure with Terraform (Google)
Page 6: Building infrastructure with Terraform (Google)

Provisioning today

● Chef, Puppet, Salt, Ansible● knowledge codified● de facto serves as documentation● faster● less error prone● brings devs and ops closer

Page 7: Building infrastructure with Terraform (Google)

Infrastructure today

Page 8: Building infrastructure with Terraform (Google)

Hashicorp

Page 9: Building infrastructure with Terraform (Google)

Vagrant

● Virtualbox● VMWare● Docker● AWS● Google Cloud● ...

Page 10: Building infrastructure with Terraform (Google)

Terraform

● AWS● Azure● Digital Ocean● Google Cloud● Docker● OpenStack● ...

Page 11: Building infrastructure with Terraform (Google)

Other solutions

● AWS CloudFormation● Google Deployment Manager● Heat (OpenStack)● Puppet Cloud Provisioner● Ansible● SaltStack● ...

Page 12: Building infrastructure with Terraform (Google)

XML

Page 13: Building infrastructure with Terraform (Google)

JSON

Page 14: Building infrastructure with Terraform (Google)

YAML- states: - QC: Quebec - ON: Ontario - BC: British Columbia - YT: Yukon Territory

[ { "states": [ { "QC": "Quebec" }, { "true": "Ontario" }, { "BC": "British Columbia" }, { "YT": "Yukon Territory" } ] }]

Page 15: Building infrastructure with Terraform (Google)

YAML#cloud-config

_discovery_url: &ETCD_DISCOVERY_URL

url: "https://discovery.etcd.io/5416cf2db"

coreos:

fleet:

<<: *FLEET_METADATA

public-ip: $private_ipv4

etcd:

<<: *ETCD_DISCOVERY_URL

addr: $private_ipv4:4001

peer-addr: $private_ipv4:7001

Page 16: Building infrastructure with Terraform (Google)
Page 17: Building infrastructure with Terraform (Google)
Page 18: Building infrastructure with Terraform (Google)

jsonnet

● 20% project of Dave Cunningham● Turing-complete language● allows building more abstraction layers● compatible w/ Terraform

Page 19: Building infrastructure with Terraform (Google)

DSL

● referencing● reusability (DRY)● human-readability

Page 21: Building infrastructure with Terraform (Google)

DSL● provider● resource (ID + reference name)● count attribute● variable● output● provisioner● Reference: ${TYPE.NAME.ATTRIBUTE}● Expansion: ${TYPE.NAME.*.ATTRIBUTE}

Page 22: Building infrastructure with Terraform (Google)

Provisioners

● local-exec● remote-exec● chef● …

Page 24: Building infrastructure with Terraform (Google)

DSL - built-in functions● file(path)● format("web-%03d", count.index+1)● formatlist("https://%s:%s/", aws_instance.

foo.*.public_dns, var.port)● lookup(map, key)● ...

Page 25: Building infrastructure with Terraform (Google)

How does it work?

Page 26: Building infrastructure with Terraform (Google)

Why Terraform? (overview)

● Provider-agnostic● DSL (yet JSON-compatible)

Page 27: Building infrastructure with Terraform (Google)

Why Terraform?● open to community

○ missing feature or bug != support ticket

Page 28: Building infrastructure with Terraform (Google)

Why Terraform?● doesn’t “own” your whole account or resource “type”

Page 29: Building infrastructure with Terraform (Google)

Why Terraform?

● core features○ aware of dependency graph between resources○ transparent state (pros/cons)

■ resources import (in the future)■ detailed plan■ maintenance & atomicity & sharing in the team

Page 30: Building infrastructure with Terraform (Google)

State..."google_compute_firewall.consul_admin_node": { "type": "google_compute_firewall", "primary": { "id": "consul-admin-firewall", "attributes": { "allow.#": "1", "allow.803338340.ports.#": "1", "allow.803338340.ports.1685985038": "22", "allow.803338340.protocol": "tcp", "id": "consul-admin-firewall", "name": "consul-admin-firewall", "network": "default", "self_link": "https://www.googleapis.com/compute/v1/projects/skilled-bee-777/global/firewalls/consul-admin-firewall", "source_tags.#": "1", "source_tags.3928264908": "consul-web-ui", "target_tags.#": "1", "target_tags.1747926572": "consul-node" } }},...

Page 31: Building infrastructure with Terraform (Google)

State

● terraform.tfstate (default)● Atlas● S3● Consul● HTTP● OpenStack’ Swift

Page 32: Building infrastructure with Terraform (Google)

State - setupterraform remote config \

-backend=consul \

-backend-config="address=demo.consul.io:80" \

-backend-config="path=tf"

Page 33: Building infrastructure with Terraform (Google)

State - referencingresource "terraform_remote_state" "network" {

backend = "atlas"

config {

name = "timeinc/network-prod"

}

}

resource "google_compute_firewall" "default" {

network = "${terraform_remote_state.network.network-name}"

...

Page 34: Building infrastructure with Terraform (Google)

Modulesmodule "consul" {

source = "github.com/hashicorp/consul/terraform/aws"

servers = 3

}

variable "servers" {

default = 2

}

$ terraform get

Page 35: Building infrastructure with Terraform (Google)

Modules./module/outputs.tf

output "ip" {

value = "${google_compute_instance.default.public_ip}"

}

./main.tf

module "consul" {

${module.consul.ip}

Page 37: Building infrastructure with Terraform (Google)

New provider?~/.terraformrc:

providers {

privatecloud = "/path/to/privatecloud"

}

Page 38: Building infrastructure with Terraform (Google)

Custom provider?package main

import (

"github.com/hashicorp/terraform/plugin"

)

func main() {

plugin.Serve(new(MyPlugin))

}

Page 39: Building infrastructure with Terraform (Google)

Under the hood - provider

● helper/schema

Page 40: Building infrastructure with Terraform (Google)

What’s next?

● Kubernetes provider● Google backend service (HTTP LB)● …

Page 41: Building infrastructure with Terraform (Google)

↓ SLIDES ↓

Page 42: Building infrastructure with Terraform (Google)

$ whoami

radeksimko}twitter.com/

google.com/+

linkedin.com/in/

github.com/

slideshare.net/