Top Banner
Terraform: Configuration Management for Cloud Services Martin Schütte 27 April 2016
34

Terraform: Configuration Management for Cloud Services

Jan 26, 2017

Download

Technology

Martin Schütte
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Terraform: Configuration Management for Cloud Services

Terraform:Configuration Management for Cloud Services

Martin Schütte27 April 2016

Page 2: Terraform: Configuration Management for Cloud Services

TERRAFORMBuild,  Combine,  and  Launch  Infrastructure

Page 3: Terraform: Configuration Management for Cloud Services

Concepts

Page 4: Terraform: Configuration Management for Cloud Services

by Rodzilla at Wikimedia Commons (CC-BY-SA-3.0)

From Servers …

Martin Schütte | Terraform | OSDC’16 3/29

Page 5: Terraform: Configuration Management for Cloud Services

…to Services

Martin Schütte | Terraform | OSDC’16 4/29

Page 6: Terraform: Configuration Management for Cloud Services

Services also need Configuration Management

• Replace “click paths” with source code in VCS• Lifecycle awareness, not just a setup.sh• Reproducible environments• Specification, documentation, policy enforcement

Martin Schütte | Terraform | OSDC’16 5/29

Page 7: Terraform: Configuration Management for Cloud Services

Core Ideas in Terraform

• Simple model of resource entities with attributes• Stateful lifecycle with CRUD operations• Declarative configuration• Dependencies by inference• Parallel execution

Martin Schütte | Terraform | OSDC’16 6/29

Page 8: Terraform: Configuration Management for Cloud Services

Core Concepts in Terraform

• Provider: a source of resources(usually with an API endpoint & authentication)

• Resource: every thing “that has a set of configurableattributes and a lifecycle (create, read, update, delete)” –implies ID and state

• Provisioner: initialize a resource with local orremote scripts

Martin Schütte | Terraform | OSDC’16 7/29

Page 9: Terraform: Configuration Management for Cloud Services

Core Concepts in Terraform

• Order: directed acyclic graph of all resources• Plan: generate an execution plan for reviewbefore applying a configuration

• State: execution result is kept in state file(local or remote)

• Lightweight: little provider knowledge, no error handling

Martin Schütte | Terraform | OSDC’16 8/29

Page 10: Terraform: Configuration Management for Cloud Services

Available services

Providers:• AWS• Azure• Google Cloud• Heroku• DNSMadeEasy• OpenStack• Docker• …

Resources:• aws_instance• aws_vpc• aws_elb• aws_iam_user• azure_instance• heroku_app• …

Provisioners:• chef• file• local-exec• remote-exec

Martin Schütte | Terraform | OSDC’16 9/29

Page 11: Terraform: Configuration Management for Cloud Services

DSL Syntax

• Hashicorp Configuration Language (HCL),think “JSON-like but human-friendly”

• Variables• Interpolation, e. g.”number ${count.index + 1}”

• Attribute access with resource_type.resource_name• Few build-in functions, e. g.base64encode(string), format(format, args…)

Martin Schütte | Terraform | OSDC’16 10/29

Page 12: Terraform: Configuration Management for Cloud Services

HCL vs. JSON

# An AMIvariable ”ami” {description = ”custom AMI”

}

/* A multiline comment. */

resource ”aws_instance” ”web” {ami = ”${var.ami}”count = 2source_dest_check = false

connection {user = ”root”

}}

{”variable”: {

”ami”: {”description”: ”custom AMI”

}},”resource”: {

”aws_instance”: {”web”: {

”ami”: ”${var.ami}”,”count”: 2,”source_dest_check”: false,

”connection”: {”user”: ”root”

}}

}}

}Martin Schütte | Terraform | OSDC’16 11/29

Page 13: Terraform: Configuration Management for Cloud Services

Example: Simple Webservice

Page 14: Terraform: Configuration Management for Cloud Services

Example: Simple Webservice (part 1)

### AWS Setupprovider ”aws” {access_key = ”${var.aws_access_key}”secret_key = ”${var.aws_secret_key}”region = ”${var.aws_region}”

}

# Queueresource ”aws_sqs_queue” ”importqueue” {name = ”${var.app_name}-${var.aws_region}-importqueue”

}

# Storageresource ”aws_s3_bucket” ”importdisk” {bucket = ”${var.app_name}-${var.aws_region}-importdisk”acl = ”private”

}

Martin Schütte | Terraform | OSDC’16 12/29

Page 15: Terraform: Configuration Management for Cloud Services

Example: Simple Webservice (part 2)

### Heroku Setupprovider ”heroku” { ... }

# Importerresource ”heroku_app” ”importer” {name = ”${var.app_name}-${var.aws_region}-import”region = ”eu”config_vars {

SQS_QUEUE_URL = ”${aws_sqs_queue.importqueue.id}”S3_BUCKET = ”${aws_s3_bucket.importdisk.id}”

}}

resource ”heroku_addon” ”mongolab” {app = ”${heroku_app.importer.name}”plan = ”mongolab:sandbox”

}

Martin Schütte | Terraform | OSDC’16 13/29

Page 16: Terraform: Configuration Management for Cloud Services

terraform graph | dot -Tpdf

aws_s3_bucket.importdisk

provider.aws

aws_sqs_queue.importqueue

heroku_addon.mongolab

heroku_app.importer

provider.heroku

Martin Schütte | Terraform | OSDC’16 14/29

Page 17: Terraform: Configuration Management for Cloud Services

Terraform Process

*.tf override.tfModules

“source” terraform.tfvars

plan

state

get

plan

apply

destroy

Martin Schütte | Terraform | OSDC’16 15/29

Page 18: Terraform: Configuration Management for Cloud Services

Example: Add Provisioning

# Importerresource ”heroku_app” ”importer” {name = ”${var.app_name}-${var.aws_region}-import”region = ”eu”

config_vars { ... }

provisioner ”local-exec” {command = <<EOT

cd ~/projects/go-testserver &&git remote add heroku ${heroku_app.importer.git_url} &&git push heroku masterEOT}

}

Martin Schütte | Terraform | OSDC’16 16/29

Page 19: Terraform: Configuration Management for Cloud Services

Example: Add Outputs

# Storageresource ”aws_s3_bucket” ”importdisk” { ... }

# Importerresource ”heroku_app” ”importer” { ... }

# Outputsoutput ”importer_bucket_arn” {value = ”${aws_s3_bucket.importdisk.arn}”

}

output ”importer_url” {value = ”${heroku_app.importer.web_url}”

}

output ”importer_gitrepo” {value = ”${heroku_app.importer.git_url}”

}

Martin Schütte | Terraform | OSDC’16 17/29

Page 20: Terraform: Configuration Management for Cloud Services

Modules

Page 21: Terraform: Configuration Management for Cloud Services

Modules

“Plain terraform code” lacks structure and reusability

Modules

• are subdirectories with self-contained terraform code• may be sourced from Git, Mercurial, HTTPS locations• use variables and outputs to pass data

Martin Schütte | Terraform | OSDC’16 18/29

Page 22: Terraform: Configuration Management for Cloud Services

Module Example

Every Terraform directory may be used as a module.

Here I use the previous webservice example.

Martin Schütte | Terraform | OSDC’16 19/29

Page 23: Terraform: Configuration Management for Cloud Services

Using a Module Example (part 1)

module ”importer_west” {source = ”../simple”aws_region = ”eu-west-1”

app_name = ”${var.app_name}”aws_access_key = ”${var.aws_access_key}”aws_secret_key = ”${var.aws_secret_key}”heroku_login_email = ”${var.heroku_login_email}”heroku_login_api_key = ”${var.heroku_login_api_key}”

}

module ”importer_central” {source = ”../simple”aws_region = ”eu-central-1”

# ...}

Martin Schütte | Terraform | OSDC’16 20/29

Page 24: Terraform: Configuration Management for Cloud Services

Using a Module Example (part 2)

# Main App, using modulesresource ”heroku_app” ”main” {name = ”${var.app_name}-main”region = ”eu”

config_vars {IMPORTER_URL_LIST = <<EOT

[ ”${module.importer_west.importer_url}”,”${module.importer_central.importer_url}” ]

EOT}

}

output ”main_url” {value = ”${heroku_app.main.web_url}”

}

Martin Schütte | Terraform | OSDC’16 21/29

Page 25: Terraform: Configuration Management for Cloud Services

Plugins

Page 26: Terraform: Configuration Management for Cloud Services

How to Write Own Plugins

• Learn you some Golang• Use the schema helper lib• Adapt to model ofProvider (setup steps, authentication) andResources (arguments/attributes and CRUD methods)

Martin Schütte | Terraform | OSDC’16 22/29

Page 27: Terraform: Configuration Management for Cloud Services

Plugin Example

Simple Plugin: MySQL

Implements provider mysql with resource mysql_database.

Code at builtin/providers/mysql

Martin Schütte | Terraform | OSDC’16 23/29

Page 28: Terraform: Configuration Management for Cloud Services

Usage

Page 29: Terraform: Configuration Management for Cloud Services

Issues

Under active development, current version 0.6.15 (April 22)

• Still a few bugs, e. g. losing state info• Modules are very simple• Lacking syntactic sugar(e. g. aggregations, common repetitions)

General problems for this kind of tool

• Testing is inherently difficult• Provider coverage• Resource model mismatch, e. g. with Heroku apps• Ignorant of API rate limits, account ressource limits, etc.

Martin Schütte | Terraform | OSDC’16 24/29

Page 30: Terraform: Configuration Management for Cloud Services

Comparable Tools

Tools:

• AWS CloudFormation (with generator tools)• OpenStack Heat• Azure Resource Manager Templates

Configuration Management:

• SaltStack Salt Cloud• Ansible v2.0 includes cloud modules

Libraries:

• fog, Ruby cloud abstraction library• boto, Python AWS library

Martin Schütte | Terraform | OSDC’16 25/29

Page 31: Terraform: Configuration Management for Cloud Services

Workflow

• Use a VCS, i. e. git• Use PGP to encrypt sensitive data, e. g. with Blackbox• Use separate user credentials, know how to revoke them• Take a look at Hashicorp Atlas and its workflow

Martin Schütte | Terraform | OSDC’16 26/29

Page 32: Terraform: Configuration Management for Cloud Services

Hashicorp Workflow

image by Hashicorp Atlas: Artifact Pipeline and Image Deploys with Packer and Terraform

Martin Schütte | Terraform | OSDC’16 27/29

Page 33: Terraform: Configuration Management for Cloud Services

Links and Resources

Defining system infrastructure as code andbuilding it with tools doesn’t make the quality anybetter. At worst, it can complicate things.— Infrastructure as Code by Kief Morris

• Terraform• hashicorp/terraform

• StackExchange/blackbox• Terraforming – Export existing AWS resources

• Terraform: Beyond the Basics with AWS• Terraform, VPC, and why you want a tfstate file per env

Martin Schütte | Terraform | OSDC’16 28/29

Page 34: Terraform: Configuration Management for Cloud Services

The End

Thank You!

Questions?

Martin Schü[email protected]

slideshare.net/mschuett/

Martin Schütte | Terraform | OSDC’16 29/29