Copyright © 2017 HashiCorp
Infrastructure as Code
in Google Cloud
@radeksimko
Copyright © 2017 HashiCorp
Radek Simko
@radeksimko
✋✋ ✋
s
Copyright © 2017 HashiCorp 4
Infrastructure as Code
Copyright © 2017 HashiCorp
▪ Many different things
▪ Different
▪ Operational experience
▪ Limitations
▪ Costs
12
What is
Infrastructure
then?
s
Copyright © 2017 HashiCorp
Dedicated Ops Team
14
Delivering Infrastructure
24 hours before deadline …
Can we go live tomorrow?
Designing infrastructure well
requires knowing the app
Designing app well
requires knowing the infrastructure
s
Copyright © 2017 HashiCorp
On Your Own
21
Delivering Infrastructure
503 Service Unavailable
s
Copyright © 2017 HashiCorp 31
Delivering Software
Terminal
Delivering
Software
git commit -m "add cool feature”
git push my-feature
What if …
s
Copyright © 2017 HashiCorp 38
Infrastructure as Code
Copyright © 2017 HashiCorp
#!/bin/sh
apt-get install nginx
systemctl enable consul.service
systemctl enable nginx.service
bootstrap.sh
39
Shell Script
AWS Bitbucket CenturyLink Cloud
CloudFlare CloudStack PagerDuty
Consul Datadog DigitalOcean
DNSMadeEasy DNSimple Docker
Dyn GitHub Fastly
Google Cloud Heroku Librato
Microsoft Azure MySQL OpenStack
Packet PostgreSQL SoftLayer
UltraDNS VMware vSphere and more...
AWS Bitbucket CenturyLink Cloud
CloudFlare CloudStack PagerDuty
Consul Datadog DigitalOcean
DNSMadeEasy DNSimple Docker
Dyn GitHub Fastly
Google Cloud Heroku Librato
Microsoft Azure MySQL OpenStack
Packet PostgreSQL SoftLayer
UltraDNS VMware vSphere and more...
AWS Bitbucket CenturyLink Cloud
CloudFlare CloudStack PagerDuty
Consul Datadog DigitalOcean
DNSMadeEasy DNSimple Docker
Dyn GitHub Fastly
Google Cloud Heroku Librato
Microsoft Azure MySQL OpenStack
Packet PostgreSQL SoftLayer
UltraDNS VMware vSphere and more...
AWS Bitbucket CenturyLink Cloud
CloudFlare CloudStack PagerDuty
Consul Datadog DigitalOcean
DNSMadeEasy DNSimple Docker
Dyn GitHub Fastly
Google Cloud Heroku Librato
Microsoft Azure MySQL OpenStack
Packet PostgreSQL SoftLayer
UltraDNS VMware vSphere and more...
Copyright © 2017 HashiCorp
1.HCL
2.Efficiency
3.Full Lifecycle
4.DEMO !!!
Terraform
49
s
Copyright © 2017 HashiCorp
HashiCorp Config Language
50
1. HCL
high-level
language
language
for dataDSL
JSON

{}
YAML

-- :
Copyright © 2017 HashiCorp
▪ github.com/hashicorp/hcl
▪ Used in various HashiCorp projects
▪ Consul
▪ Vault
▪ Nomad
▪ Terraform
▪ JSON compatible
▪ Useful for generated code
HCL
52
github.com/alphagov/govuk-dns
s
Copyright © 2017 HashiCorp 54
2. Efficiency
Copyright © 2017 HashiCorp
resource	"google_compute_instance"	"frontend"	{	
		name									=	"devfest-frontend"	
		machine_type	=	"n1-standard-1"	
		zone									=	"europe-west2-a"	
		boot_disk	{	
				initialize_params	{	
						image	=	"debian-cloud/debian-8"	
				}	
		}	
		network_interface	{	
				access_config	{	}	
		}	
}
compute.tf
55
Compute Instance
Terraform Config
Copyright © 2017 HashiCorp
resource	"google_dns_managed_zone"	"devfest"	{	
		name	=	"devfest"	
		dns_name	=	"devfest.org.ua"	
}	
resource	"google_dns_record_set"	"www"	{	
		managed_zone	=	"${google_dns_managed_zone.devfest.name}"	
		name	=	"www.devfest.org.ua"	
		type	=	"A"	
		ttl		=	120	
		rrdatas	=	[	
				“${google_compute_instance.….assigned_nat_ip}"	
		]	
}
dns.tf
56
DNS zone + record
Terraform Config
Terminal
Planning
Terraform
in Action
$ terraform plan
+ google_compute_instance.frontend
boot_disk.0.initialize_params.0.image: "debian-cloud/debian-8"
boot_disk.0.source: <computed>
can_ip_forward: "false"
machine_type: "n1-standard-1"
name: "devfest-frontend"
zone: “europe-west2-a"
...
+ google_dns_managed_zone.devfest
dns_name: "devfest.org.ua"
name: "devfest"
name_servers.#: <computed>
+ google_dns_record_set.www
managed_zone: "devfest"
name: "www.devfest.org.ua"
rrdatas.#: <computed>
ttl: "120"
type: "A"
Plan: 3 to add, 0 to change, 0 to destroy.
Terminal
Applying
Terraform
in Action
$ terraform apply
google_dns_managed_zone.devfest: Creating...
...
google_compute_instance.frontend: Creating...
...
google_dns_managed_zone.devfest: Still creating... (10s elapsed)
google_compute_instance.frontend: Still creating... (10s elapsed)
google_dns_managed_zone.devfest: Creation complete after 11s (ID: …)
google_compute_instance.frontend: Still creating... (20s elapsed)
google_compute_instance.frontend: Creation complete after 27s (ID: …)
google_dns_record_set.www: Creating...
...
google_dns_record_set.www: Still creating... (10s elapsed)
google_dns_record_set.www: Creation complete after 14s (ID: 1)
Apply complete! Resources: 3 added, 0 changed, 0 destroyed.
Copyright © 2017 HashiCorp 59
Graph
Behind the Scenes
Copyright © 2017 HashiCorp 60
Explained
Behind the Scenes
1.
2.
Terminal
Terraform Graph
$ terraform graph | dot -Tpng > graph.png
$
https://www.youtube.com/watch?v=Ce3RNfRbdZ0
s
Copyright © 2017 HashiCorp 63
3. Full Lifecycle
Terminal
Creating
resources
#!/bin/sh
gcloud compute instances create ...
gsutil mb gs://my-bucket
gcloud dns record-sets transaction add ...
Terminal
Replacing instance
Updating
resources
#!/bin/sh
gcloud compute create <new-name>
gcloud dns record-sets transaction add <new-ip>
gcloud dns record-sets transaction remove <old-ip>
gcloud compute delete <old-name>
Terminal
Deleting
Resources
#!/bin/sh
gcloud dns record-sets transaction remove <name> ...
gcloud compute instances delete <name> ...
gsutil rm gs://<name>
Copyright © 2017 HashiCorp
"identifier": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ConflictsWith: []string{"identifier_prefix"},
ValidateFunc: validateIdentifier,
},
"username": { …

"password": { …
67
Resource Schema
Copyright © 2017 HashiCorp
stateConf := &resource.StateChangeConf{
Pending: []string{"creating", "backing-up", "modifying",
"resetting-master-credentials", "maintenance",
"renaming", "rebooting", "upgrading"},
Target: []string{"available"},
Refresh: refreshFunc(d, meta),
Timeout: 40 * time.Minute,
MinTimeout: 10 * time.Second,
Delay: 30 * time.Second,
}
68
Waiters
Terminal
Mutable Field
Plan
$ terraform plan
~ google_compute_instance.frontend
metadata.%: "0" => "1"
metadata.Role: "" => "production"
Plan: 0 to add, 1 to change, 0 to destroy.
Terminal
Immutable Field
Plan
$ terraform plan
-/+ google_compute_instance.frontend
machine_type: "n1-standard-1" => "n1-standard-1"
name: "devfest" => “devfest-17" (forces new resource)
can_ip_forward: "false" => “false"
...
Plan: 1 to add, 0 to change, 1 to destroy.
s
Copyright © 2017 HashiCorp 71
4. Demo! OMG !!!
devfest-network
private
public
server
server
server
worker
worker
worker
bastion
Copyright © 2017 HashiCorp
▪ Think about what you need
▪ Operational experience
▪ Limitations
▪ Costs
▪ Treat your infrastructure as code
▪ Share your infrastructure
Summary
73
Thank you.
@radeksimko
https://www.flickr.com/photos/mhx/16419218393
https://www.pexels.com/photo/assorted-color-rj45-cables-46218/
https://www.flickr.com/photos/stars6/4381851322
https://www.flickr.com/photos/29069717@N02/32502689900
https://www.flickr.com/photos/29069717@N02/32502688260

Infrastructure as Code in Google Cloud