Terraform Introduction
-
Upload
soniasnowfrog -
Category
Software
-
view
993 -
download
6
Transcript of Terraform Introduction
Terraform
Sonia Hamilton
1 / 34
Provisioninginstances
storage
dns
2 / 34
In Relation To...Puppet, Salt
Boto, Fog
CloudFormation
Vagrant?
3 / 34
Featuresexecution plans
dependencies
state
4 / 34
Example ProvidersAWS
Heroku
Google Cloud
DNSimple
no VCloud, yet...
5 / 34
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
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
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
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
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
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
Changing Infrastructure
Edit
resource "aws_instance" "example" { ami = "ami-aa7ab6c2" # NEW AMI instance_type = "t1.micro"}
12 / 34
Changing Infrastructure
Plan
$ terraform plan...
-/+ aws_instance.example ami: "ami-408c7f28" => "ami-aa7ab6c2" (forces new resource) availability_zone: "us-east-1c" => "<computed>"...
13 / 34
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
Destroy Infrastructure
Plan
$ terraform plan -destroy...
- aws_instance.example
15 / 34
Destroy$ terraform destroyaws_instance.example: Destroying...
Apply complete! Resources: 0 added, 0 changed, 1 destroyed.
...
16 / 34
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
Resource Dependenciesmultiple providers
multiple resource types
do x before y, and y needs x's ip address
18 / 34
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
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
Explicit Dependenciesresource "aws_eip" "ip" { instance = "${aws_instance.example.id}" depends_on = ["aws_instance.example"]}
terraform graph
21 / 34
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
Provisionersconnection
file
local-exec
remote-exec
23 / 34
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
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
'Advanced'input, output variables
modules
26 / 34
Input VariablesCommonly named variables.tf, uses all *.tf files.
variable "access_key" {}variable "secret_key" {}variable "region" { default = "us-east-1"}
27 / 34
Using Variablesprovider "aws" { access_key = "${var.access_key}" secret_key = "${var.secret_key}" region = "${var.region}"}
28 / 34
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
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
Modules
Create
$ mkdir child$ touch child/main.tf
Use
module "child" { source = "./child"}
31 / 34
Module Parameters - Input
Caller
module "child" { source = "./child" memory = "1G"}
Child
variable "memory" {}resource "aws_db_instance" "default" { ... allocated_storage = "${var.memory}" ...}
32 / 34
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