SlideShare una empresa de Scribd logo
1 de 42
Descargar para leer sin conexión
Terraform:
Infrastructure as Code
Martin Schütte
20 August 2017
Concepts
by Rodzilla at Wikimedia Commons (CC-BY-SA-3.0)
From Servers …
Martin Schütte | Terraform | FrOSCon’17 2/39
…to Services
Martin Schütte | Terraform | FrOSCon’17 3/39
Services have APIs
• Starting servers is just a command line or function call
• Add to build process (phoenix/immutable servers)
• Replace “click paths” with source code in VCS
• Fewer “black box” setup steps, better team handovers
⇒ Infrastructure as Code
Martin Schütte | Terraform | FrOSCon’17 4/39
Services also need Configuration Management
• Lifecycle awareness, not just a setup.sh
• Multiple stages/environments
• Specification, documentation, policy enforcement
⇒ Tool support
Martin Schütte | Terraform | FrOSCon’17 5/39
TERRAFORM
Build,  Combine,  and  Launch  Infrastructure
Example: Simple Webservice (part 1)
### AWS Setup
provider ”aws” {
profile = ”${var.aws_profile}”
region = ”${var.aws_region}”
}
# Queue
resource ”aws_sqs_queue” ”importqueue” {
name = ”${var.app_name}-${var.aws_region}-importqueue”
}
# Storage
resource ”aws_s3_bucket” ”importdisk” {
bucket = ”${var.app_name}-${var.aws_region}-importdisk”
acl = ”private”
}
Martin Schütte | Terraform | FrOSCon’17 7/39
Example: Simple Webservice (part 2)
### Heroku Setup
provider ”heroku” { ... }
# Importer
resource ”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 | FrOSCon’17 8/39
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 | FrOSCon’17 9/39
Core Concepts in Terraform
• Provider: a source of resources
(usually with an API endpoint & authentication)
• Resource: every thing “that has a set of configurable
attributes and a lifecycle (create, read, update, delete)” –
implies ID and state
• Data Source: information read from provider
(e. g. lookup own account ID or AMI-ID)
• Provisioner: initialize a resource with local or
remote scripts
Martin Schütte | Terraform | FrOSCon’17 10/39
Design Choices in Terraform
• Order: directed acyclic graph of all resources
• Plan: generate an execution plan for review
before 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 | FrOSCon’17 11/39
Available services
Providers:
• AWS
• Azure
• Google Cloud
• Alicloud
• Heroku
• DNSMadeEasy
• OpenStack
• Docker
• …
Resources:
• aws_instance
• aws_vpc
• aws_iam_user
• azurerm_subnet
• azurerm_dns_zone
• azure_instance
• aws_iam_user
• heroku_app
• postgresql_schema
• …
Provisioners:
• chef
• file
• local-exec
• remote-exec
Martin Schütte | Terraform | FrOSCon’17 12/39
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 | FrOSCon’17 13/39
HCL vs. JSON
# An AMI
variable ”ami” {
description = ”custom AMI”
}
/* A multi
line comment. */
resource ”aws_instance” ”web” {
ami = ”${var.ami}”
count = 2
source_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 | FrOSCon’17 14/39
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 | FrOSCon’17 15/39
Terraform Process
*.tf override.tfModules
“source” terraform.tfvars
plan
state
get
plan
apply
destroy
Martin Schütte | Terraform | FrOSCon’17 16/39
Example: Add Provisioning
# Importer
resource ”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 master
EOT
}
}
Martin Schütte | Terraform | FrOSCon’17 17/39
Example: Add Outputs
# Storage
resource ”aws_s3_bucket” ”importdisk” { ... }
# Importer
resource ”heroku_app” ”importer” { ... }
# Outputs
output ”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 | FrOSCon’17 18/39
Example: Add Lifecycle Meta-Parameter
# Storage
resource ”aws_s3_bucket” ”importdisk” {
bucket = ”${var.app_name}-${var.aws_region}-importdisk”
acl = ”private”
lifecycle {
prevent_destroy = true
}
}
Martin Schütte | Terraform | FrOSCon’17 19/39
Demo
$ terraform init
$ terraform validate
$ terraform plan -out=my.plan
$ terraform show my.plan
$ terraform apply my.plan
$ terraform output
$ terraform output -json
$ terraform output importer_url
$ curl -s $(terraform output importer_url)
$ terraform graph | dot -Tpdf > graph.pdf && evince graph.pdf
$ terraform plan -destroy
$ terraform destroy
Martin Schütte | Terraform | FrOSCon’17 20/39
Features
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 | FrOSCon’17 21/39
Example Module
module ”database” {
source = ”github.com/terraform-community-modules/tf_aws_rds”
# DB Instance Inputs
rds_instance_identifier = ”${terraform.workspace}-${var.app}-db”
rds_allocated_storage = ”${var.database_size}”
database_name = ”${var.database_name}”
database_user = ”${var.database_user}”
database_password = ”${var.database_password}”
# DB Subnet Inputs
subnets = [”${aws_subnet.dbnet.*.id}”]
rds_vpc_id = ”${data.aws_vpc.app.id}”
tags {
Name = ”${terraform.workspace} - ${var.app} - DB”
}
}
Martin Schütte | Terraform | FrOSCon’17 22/39
terraform.tfstate
• Terraform keeps known state of resources
• Defaults to local state in terraform.tfstate
• Optional remote state with different backends
(S3, Azure Storage, Consul, Atlas, …)
• Useful to sync multiple team members
• May need additional mutex mechanism
(v0.9 added state locking for Local, S3, and Consul)
• Remote state is a data source
Martin Schütte | Terraform | FrOSCon’17 23/39
Example: Using State Import
$ terraform import azurerm_storage_account.my_storage_account 
/subscriptions/e9b2ec19-ab6e-4547-a3ec-5a58e234ce5e/resourceGroups/
demo-res-group/providers/Microsoft.Storage/storageAccounts/demostorage20170418
azurerm_storage_account.my_storage_account: Importing from ID ...
azurerm_storage_account.my_storage_account: Import complete!
Imported azurerm_storage_account (ID: ...)
azurerm_storage_account.my_storage_account: Refreshing state... (ID: ...)
Import success! The resources imported are shown above. These are
now in your Terraform state. Import does not currently generate
configuration, so you must do this next. If you do not create configuration
for the above resources, then the next ‘terraform plan‘ will mark
them for destruction.
$ terraform state list
azurerm_storage_account.my_storage_account
$ terraform state show azurerm_storage_account.my_storage_account
id = /subscriptions/e9b2ec19...
account_kind = Storage
account_type = Standard_LRS
location = westeurope
name = demostorage20170418
...
Martin Schütte | Terraform | FrOSCon’17 24/39
Example: Use Remote State (with Workspaces)
terraform {
required_version = ”>= 0.9.8”
environment = ”${terraform.workspace}”
backend ”s3” {
bucket = ”ms-terraform-state”
key = ”infra/ms-tf-demo/state”
region = ”eu-central-1”
}
}
$ terraform workspace new prod
$ terraform workspace new dev
$ aws s3 ls --recursive ”s3://ms-terraform-state/”
... 282 workspace:/dev/infra/ms-tf-demo/state
... 282 workspace:/prod/infra/ms-tf-demo/state
Martin Schütte | Terraform | FrOSCon’17 25/39
Example: Use Remote State to Chain Projects
data ”terraform_remote_state” ”infra” {
backend = ”s3”
config {
bucket = ”ms-terraform-state”
key = ”workspace:/${terraform.workspace}/infra/ ⌋
ms-tf-demo/state”→
region = ”eu-central-1”
}
}
resource ”aws_instance” ”foo” {
# use state from vpc_project
subnet_id =
”${data.terraform_remote_state.infra.app_subnet_id}”→
instance_type = ”t2.micro”
ami = ”ami-b968bad6”
}
Martin Schütte | Terraform | FrOSCon’17 26/39
Example: Using Data Source to Lookup Data
# searches for most recent tagged AMI in own account
data ”aws_ami” ”webami” {
most_recent = true
owners = [”self”]
filter {
name = ”tag:my_key”
values = [”my_value”]
}
}
# use AMI
resource ”aws_instance” ”web” {
instance_type = ”t2.micro”
ami = ”${data.aws_ami.webami.id}”
}
Martin Schütte | Terraform | FrOSCon’17 27/39
Example: “External” Data Source
data ”external” ”dyndns” {
program = [”bash”, ”${path.module}/variomedia_dyndns.sh”]
query = {
hostname = ”aws-demo.martin-schuette.de”
ipaddress = ”${aws_eip.foo.public_ip}”
}
}
Martin Schütte | Terraform | FrOSCon’17 28/39
How to Write Own Plugins
Now:
• Learn you some Golang
• Use the schema helper lib
• Adapt to model of
Provider (setup steps, authentication) and
Resources (arguments/attributes and CRUD methods)
• Start reading of simple plugins like
builtin/providers/mysql 
Future:
• interface, support for Python, Ruby, C#, Java, …
Martin Schütte | Terraform | FrOSCon’17 29/39
Usage
General Problemes for all Tools
• Testing is inherently difficult
• Provider coverage largely depends on community
• Resource model mismatches, e. g. with Heroku apps
• Ignorant of API rate limits, account ressource limits, etc.
Martin Schütte | Terraform | FrOSCon’17 30/39
Issues
Under active development,
current version 0.10.2 (August 16)
• Modules are very simple
• Lacking syntactic sugar
(e. g. aggregations, common repetitions)
• Big improvements in state management
• Large variation in provider support, new project
boundaries
Martin Schütte | Terraform | FrOSCon’17 31/39
New Features
Recent Features in 0.7–0.10
• State Import
• Data Sources
• Workspaces (previously: State Environments)
• Separate sub-projects for providers
terraform-providers 
• Support for gRPC-based plugins, i. e. providers in other
languages
Martin Schütte | Terraform | FrOSCon’17 32/39
Comparable Tools
Configuration Management Tools:
• SaltStack Salt Cloud
• Ansible modules
• Puppet modules
Vendor Tools:
• Azure Resource Manager Templates
• AWS CloudFormation
• OpenStack Heat
Martin Schütte | Terraform | FrOSCon’17 33/39
Workflow
• Avoid user credentials in Terraform code,
use e. g. profiles and assume-role wrapper scripts
• At least use separate user credentials,
know how to revoke them
• To hold credentials in VCS use PGP encryption,
e. g. with Blackbox
Martin Schütte | Terraform | FrOSCon’17 34/39
Workflow (contd.)
• Use a VCS, i. e. git
• Namespaces! – Always add some
”${var.shortname}-${var.env}”
• per project
• per region
• per account
• per provider
• Use remote state and consider access locking,
e. g. with a single build server
• Take a look at Hashicorp Atlas and its workflow
Martin Schütte | Terraform | FrOSCon’17 35/39
Hashicorp Toolset
Martin Schütte | Terraform | FrOSCon’17 36/39
Links and Resources
• Terraform.io and hashicorp/terraform 
• terraform-providers 
• terraform-community-modules 
• newcontext/kitchen-terraform 
• Terraforming – Export existing AWS resources
• Terraform: Beyond the Basics with AWS
• A Comprehensive Guide to Terraform
• Terraform, VPC, and why you want a tfstate file per env
Martin Schütte | Terraform | FrOSCon’17 37/39
Books
Hopefully, deployments will become routine and
boring–and in the world of operations, boring is
a very good thing.
— Terraform: Up & Running by Yevgeniy Brikman
Defining system infrastructure as code and
building it with tools doesn’t make the quality
any better. At worst, it can complicate things.
— Infrastructure as Code by Kief Morris
Martin Schütte | Terraform | FrOSCon’17 38/39
The End
Thank You! — Questions?
Workshop
Terraform und AWS
at 14:00 h in C 120
Martin Schütte
@m_schuett
info@martin-schuette.de
slideshare.net/mschuett/ 
tinyurl.com/froscon17-tf
Martin Schütte | Terraform | FrOSCon’17 39/39

Más contenido relacionado

La actualidad más candente

Building infrastructure as code using Terraform - DevOps Krakow
Building infrastructure as code using Terraform - DevOps KrakowBuilding infrastructure as code using Terraform - DevOps Krakow
Building infrastructure as code using Terraform - DevOps KrakowAnton Babenko
 
Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018Anton Babenko
 
Terraform: An Overview & Introduction
Terraform: An Overview & IntroductionTerraform: An Overview & Introduction
Terraform: An Overview & IntroductionLee Trout
 
An introduction to terraform
An introduction to terraformAn introduction to terraform
An introduction to terraformJulien Pivotto
 
Comprehensive Terraform Training
Comprehensive Terraform TrainingComprehensive Terraform Training
Comprehensive Terraform TrainingYevgeniy Brikman
 
Getting Started with Infrastructure as Code
Getting Started with Infrastructure as CodeGetting Started with Infrastructure as Code
Getting Started with Infrastructure as CodeWinWire Technologies Inc
 
Terraform modules and (some of) best practices
Terraform modules and (some of) best practicesTerraform modules and (some of) best practices
Terraform modules and (some of) best practicesAnton Babenko
 
Terraform introduction
Terraform introductionTerraform introduction
Terraform introductionJason Vance
 
Infrastructure-as-Code (IaC) using Terraform
Infrastructure-as-Code (IaC) using TerraformInfrastructure-as-Code (IaC) using Terraform
Infrastructure-as-Code (IaC) using TerraformAdin Ermie
 
Microsoft Azure IaaS and Terraform
Microsoft Azure IaaS and TerraformMicrosoft Azure IaaS and Terraform
Microsoft Azure IaaS and TerraformAlex Mags
 
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)Adin Ermie
 
Terraform modules restructured
Terraform modules restructuredTerraform modules restructured
Terraform modules restructuredAmi Mahloof
 
Infrastructure as Code with Terraform and Ansible
Infrastructure as Code with Terraform and AnsibleInfrastructure as Code with Terraform and Ansible
Infrastructure as Code with Terraform and AnsibleDevOps Meetup Bern
 

La actualidad más candente (20)

Building infrastructure as code using Terraform - DevOps Krakow
Building infrastructure as code using Terraform - DevOps KrakowBuilding infrastructure as code using Terraform - DevOps Krakow
Building infrastructure as code using Terraform - DevOps Krakow
 
Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018Terraform modules and best-practices - September 2018
Terraform modules and best-practices - September 2018
 
Terraform on Azure
Terraform on AzureTerraform on Azure
Terraform on Azure
 
Terraform: An Overview & Introduction
Terraform: An Overview & IntroductionTerraform: An Overview & Introduction
Terraform: An Overview & Introduction
 
Terraform
TerraformTerraform
Terraform
 
An introduction to terraform
An introduction to terraformAn introduction to terraform
An introduction to terraform
 
Comprehensive Terraform Training
Comprehensive Terraform TrainingComprehensive Terraform Training
Comprehensive Terraform Training
 
Terraform
TerraformTerraform
Terraform
 
Terraform
TerraformTerraform
Terraform
 
Getting Started with Infrastructure as Code
Getting Started with Infrastructure as CodeGetting Started with Infrastructure as Code
Getting Started with Infrastructure as Code
 
Terraform modules and (some of) best practices
Terraform modules and (some of) best practicesTerraform modules and (some of) best practices
Terraform modules and (some of) best practices
 
Terraform
TerraformTerraform
Terraform
 
Effective terraform
Effective terraformEffective terraform
Effective terraform
 
Terraform introduction
Terraform introductionTerraform introduction
Terraform introduction
 
Infrastructure as Code
Infrastructure as CodeInfrastructure as Code
Infrastructure as Code
 
Infrastructure-as-Code (IaC) using Terraform
Infrastructure-as-Code (IaC) using TerraformInfrastructure-as-Code (IaC) using Terraform
Infrastructure-as-Code (IaC) using Terraform
 
Microsoft Azure IaaS and Terraform
Microsoft Azure IaaS and TerraformMicrosoft Azure IaaS and Terraform
Microsoft Azure IaaS and Terraform
 
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
Infrastructure-as-Code (IaC) Using Terraform (Advanced Edition)
 
Terraform modules restructured
Terraform modules restructuredTerraform modules restructured
Terraform modules restructured
 
Infrastructure as Code with Terraform and Ansible
Infrastructure as Code with Terraform and AnsibleInfrastructure as Code with Terraform and Ansible
Infrastructure as Code with Terraform and Ansible
 

Similar a Terraform -- Infrastructure as Code

Terraform – Infrastructure as Code (Kielux'18)
Terraform – Infrastructure as Code (Kielux'18)Terraform – Infrastructure as Code (Kielux'18)
Terraform – Infrastructure as Code (Kielux'18)Martin Schütte
 
Terraform: Cloud Configuration Management (WTC/IPC'16)
Terraform: Cloud Configuration Management (WTC/IPC'16)Terraform: Cloud Configuration Management (WTC/IPC'16)
Terraform: Cloud Configuration Management (WTC/IPC'16)Martin Schütte
 
"Continuously delivering infrastructure using Terraform and Packer" training ...
"Continuously delivering infrastructure using Terraform and Packer" training ..."Continuously delivering infrastructure using Terraform and Packer" training ...
"Continuously delivering infrastructure using Terraform and Packer" training ...Anton Babenko
 
Terraform day 1
Terraform day 1Terraform day 1
Terraform day 1Kalkey
 
OSDC 2016 - Configuration Management for Cloud Services by Martin Schütte
OSDC 2016 - Configuration Management for Cloud Services by Martin SchütteOSDC 2016 - Configuration Management for Cloud Services by Martin Schütte
OSDC 2016 - Configuration Management for Cloud Services by Martin SchütteNETWAYS
 
Terraform: Configuration Management for Cloud Services
Terraform: Configuration Management for Cloud ServicesTerraform: Configuration Management for Cloud Services
Terraform: Configuration Management for Cloud ServicesMartin Schütte
 
Debasihish da final.ppt
Debasihish da final.pptDebasihish da final.ppt
Debasihish da final.pptKalkey
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
Terraform infraestructura como código
Terraform infraestructura como códigoTerraform infraestructura como código
Terraform infraestructura como códigoVictor Adsuar
 
Terraform on Oracle Cloud Infrastructure: A Primer for Database Administrators
Terraform on Oracle Cloud Infrastructure: A Primer for Database AdministratorsTerraform on Oracle Cloud Infrastructure: A Primer for Database Administrators
Terraform on Oracle Cloud Infrastructure: A Primer for Database AdministratorsSean Scott
 
Configuration management II - Terraform
Configuration management II - TerraformConfiguration management II - Terraform
Configuration management II - TerraformXavier Serrat Bordas
 
Terraform in deployment pipeline
Terraform in deployment pipelineTerraform in deployment pipeline
Terraform in deployment pipelineAnton Babenko
 
leboncoin DataEngineering / Terraform - beginner to advanced
leboncoin DataEngineering / Terraform - beginner to advancedleboncoin DataEngineering / Terraform - beginner to advanced
leboncoin DataEngineering / Terraform - beginner to advancedleboncoin engineering
 
Misadventures With Terraform
Misadventures With TerraformMisadventures With Terraform
Misadventures With TerraformMatt Revell
 
Hashicorp Terraform with Microsoft Azure
Hashicorp Terraform with Microsoft AzureHashicorp Terraform with Microsoft Azure
Hashicorp Terraform with Microsoft AzureAlan Chen
 
Deploy resources on Azure using IaC (Azure Terraform)
Deploy  resources on Azure using IaC (Azure Terraform)Deploy  resources on Azure using IaC (Azure Terraform)
Deploy resources on Azure using IaC (Azure Terraform)George Grammatikos
 

Similar a Terraform -- Infrastructure as Code (20)

Terraform – Infrastructure as Code (Kielux'18)
Terraform – Infrastructure as Code (Kielux'18)Terraform – Infrastructure as Code (Kielux'18)
Terraform – Infrastructure as Code (Kielux'18)
 
Terraform: Cloud Configuration Management (WTC/IPC'16)
Terraform: Cloud Configuration Management (WTC/IPC'16)Terraform: Cloud Configuration Management (WTC/IPC'16)
Terraform: Cloud Configuration Management (WTC/IPC'16)
 
London HUG 12/4
London HUG 12/4London HUG 12/4
London HUG 12/4
 
"Continuously delivering infrastructure using Terraform and Packer" training ...
"Continuously delivering infrastructure using Terraform and Packer" training ..."Continuously delivering infrastructure using Terraform and Packer" training ...
"Continuously delivering infrastructure using Terraform and Packer" training ...
 
Terraform day 1
Terraform day 1Terraform day 1
Terraform day 1
 
OSDC 2016 - Configuration Management for Cloud Services by Martin Schütte
OSDC 2016 - Configuration Management for Cloud Services by Martin SchütteOSDC 2016 - Configuration Management for Cloud Services by Martin Schütte
OSDC 2016 - Configuration Management for Cloud Services by Martin Schütte
 
Terraform: Configuration Management for Cloud Services
Terraform: Configuration Management for Cloud ServicesTerraform: Configuration Management for Cloud Services
Terraform: Configuration Management for Cloud Services
 
Debasihish da final.ppt
Debasihish da final.pptDebasihish da final.ppt
Debasihish da final.ppt
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
Terraform infraestructura como código
Terraform infraestructura como códigoTerraform infraestructura como código
Terraform infraestructura como código
 
Final terraform
Final terraformFinal terraform
Final terraform
 
Terraform on Oracle Cloud Infrastructure: A Primer for Database Administrators
Terraform on Oracle Cloud Infrastructure: A Primer for Database AdministratorsTerraform on Oracle Cloud Infrastructure: A Primer for Database Administrators
Terraform on Oracle Cloud Infrastructure: A Primer for Database Administrators
 
Configuration management II - Terraform
Configuration management II - TerraformConfiguration management II - Terraform
Configuration management II - Terraform
 
Terraform in deployment pipeline
Terraform in deployment pipelineTerraform in deployment pipeline
Terraform in deployment pipeline
 
leboncoin DataEngineering / Terraform - beginner to advanced
leboncoin DataEngineering / Terraform - beginner to advancedleboncoin DataEngineering / Terraform - beginner to advanced
leboncoin DataEngineering / Terraform - beginner to advanced
 
Terraform 101
Terraform 101Terraform 101
Terraform 101
 
Misadventures With Terraform
Misadventures With TerraformMisadventures With Terraform
Misadventures With Terraform
 
Hashicorp Terraform with Microsoft Azure
Hashicorp Terraform with Microsoft AzureHashicorp Terraform with Microsoft Azure
Hashicorp Terraform with Microsoft Azure
 
Deploy resources on Azure using IaC (Azure Terraform)
Deploy  resources on Azure using IaC (Azure Terraform)Deploy  resources on Azure using IaC (Azure Terraform)
Deploy resources on Azure using IaC (Azure Terraform)
 
Terraform training 🎒 - Basic
Terraform training 🎒 - BasicTerraform training 🎒 - Basic
Terraform training 🎒 - Basic
 

Más de Martin Schütte

Writing Ansible Modules (DENOG11)
Writing Ansible Modules (DENOG11)Writing Ansible Modules (DENOG11)
Writing Ansible Modules (DENOG11)Martin Schütte
 
Writing Ansible Modules (CLT'19)
Writing Ansible Modules (CLT'19)Writing Ansible Modules (CLT'19)
Writing Ansible Modules (CLT'19)Martin Schütte
 
The IPv6 Snort Plugin (at DeepSec 2014)
The IPv6 Snort Plugin (at DeepSec 2014)The IPv6 Snort Plugin (at DeepSec 2014)
The IPv6 Snort Plugin (at DeepSec 2014)Martin Schütte
 
The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)
The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)
The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)Martin Schütte
 
Short Introduction to IPv6
Short Introduction to IPv6Short Introduction to IPv6
Short Introduction to IPv6Martin Schütte
 
Software Testing on the Web
Software Testing on the WebSoftware Testing on the Web
Software Testing on the WebMartin Schütte
 
NetBSD syslogd with IETF Syslog Protocols
NetBSD syslogd with IETF Syslog ProtocolsNetBSD syslogd with IETF Syslog Protocols
NetBSD syslogd with IETF Syslog ProtocolsMartin Schütte
 
Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...
Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...
Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...Martin Schütte
 

Más de Martin Schütte (10)

Writing Ansible Modules (DENOG11)
Writing Ansible Modules (DENOG11)Writing Ansible Modules (DENOG11)
Writing Ansible Modules (DENOG11)
 
Writing Ansible Modules (CLT'19)
Writing Ansible Modules (CLT'19)Writing Ansible Modules (CLT'19)
Writing Ansible Modules (CLT'19)
 
The IPv6 Snort Plugin (at DeepSec 2014)
The IPv6 Snort Plugin (at DeepSec 2014)The IPv6 Snort Plugin (at DeepSec 2014)
The IPv6 Snort Plugin (at DeepSec 2014)
 
The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)
The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)
The IPv6 Snort Plugin (at Troopers 14 IPv6 Security Summit)
 
Short Introduction to IPv6
Short Introduction to IPv6Short Introduction to IPv6
Short Introduction to IPv6
 
Software Testing on the Web
Software Testing on the WebSoftware Testing on the Web
Software Testing on the Web
 
NetBSD syslogd with IETF Syslog Protocols
NetBSD syslogd with IETF Syslog ProtocolsNetBSD syslogd with IETF Syslog Protocols
NetBSD syslogd with IETF Syslog Protocols
 
PGP/GPG Einführung
PGP/GPG EinführungPGP/GPG Einführung
PGP/GPG Einführung
 
Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...
Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...
Design and Implementation of an IPv6 Plugin for the Snort Intrusion Detection...
 
Syslog Protocols
Syslog ProtocolsSyslog Protocols
Syslog Protocols
 

Último

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsNanddeep Nachan
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Jeffrey Haguewood
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesrafiqahmad00786416
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxRustici Software
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...DianaGray10
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWERMadyBayot
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Bhuvaneswari Subramani
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Angeliki Cooney
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Victor Rentea
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfsudhanshuwaghmare1
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobeapidays
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 

Último (20)

MS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectorsMS Copilot expands with MS Graph connectors
MS Copilot expands with MS Graph connectors
 
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
Web Form Automation for Bonterra Impact Management (fka Social Solutions Apri...
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
Corporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptxCorporate and higher education May webinar.pptx
Corporate and higher education May webinar.pptx
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
Connector Corner: Accelerate revenue generation using UiPath API-centric busi...
 
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWEREMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
EMPOWERMENT TECHNOLOGY GRADE 11 QUARTER 2 REVIEWER
 
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​Elevate Developer Efficiency & build GenAI Application with Amazon Q​
Elevate Developer Efficiency & build GenAI Application with Amazon Q​
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
Boost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdfBoost Fertility New Invention Ups Success Rates.pdf
Boost Fertility New Invention Ups Success Rates.pdf
 
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, AdobeApidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
Apidays New York 2024 - Scaling API-first by Ian Reasor and Radu Cotescu, Adobe
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 

Terraform -- Infrastructure as Code

  • 3. by Rodzilla at Wikimedia Commons (CC-BY-SA-3.0) From Servers … Martin Schütte | Terraform | FrOSCon’17 2/39
  • 4. …to Services Martin Schütte | Terraform | FrOSCon’17 3/39
  • 5. Services have APIs • Starting servers is just a command line or function call • Add to build process (phoenix/immutable servers) • Replace “click paths” with source code in VCS • Fewer “black box” setup steps, better team handovers ⇒ Infrastructure as Code Martin Schütte | Terraform | FrOSCon’17 4/39
  • 6. Services also need Configuration Management • Lifecycle awareness, not just a setup.sh • Multiple stages/environments • Specification, documentation, policy enforcement ⇒ Tool support Martin Schütte | Terraform | FrOSCon’17 5/39
  • 7. TERRAFORM Build,  Combine,  and  Launch  Infrastructure
  • 8. Example: Simple Webservice (part 1) ### AWS Setup provider ”aws” { profile = ”${var.aws_profile}” region = ”${var.aws_region}” } # Queue resource ”aws_sqs_queue” ”importqueue” { name = ”${var.app_name}-${var.aws_region}-importqueue” } # Storage resource ”aws_s3_bucket” ”importdisk” { bucket = ”${var.app_name}-${var.aws_region}-importdisk” acl = ”private” } Martin Schütte | Terraform | FrOSCon’17 7/39
  • 9. Example: Simple Webservice (part 2) ### Heroku Setup provider ”heroku” { ... } # Importer resource ”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 | FrOSCon’17 8/39
  • 10. 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 | FrOSCon’17 9/39
  • 11. Core Concepts in Terraform • Provider: a source of resources (usually with an API endpoint & authentication) • Resource: every thing “that has a set of configurable attributes and a lifecycle (create, read, update, delete)” – implies ID and state • Data Source: information read from provider (e. g. lookup own account ID or AMI-ID) • Provisioner: initialize a resource with local or remote scripts Martin Schütte | Terraform | FrOSCon’17 10/39
  • 12. Design Choices in Terraform • Order: directed acyclic graph of all resources • Plan: generate an execution plan for review before 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 | FrOSCon’17 11/39
  • 13. Available services Providers: • AWS • Azure • Google Cloud • Alicloud • Heroku • DNSMadeEasy • OpenStack • Docker • … Resources: • aws_instance • aws_vpc • aws_iam_user • azurerm_subnet • azurerm_dns_zone • azure_instance • aws_iam_user • heroku_app • postgresql_schema • … Provisioners: • chef • file • local-exec • remote-exec Martin Schütte | Terraform | FrOSCon’17 12/39
  • 14. 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 | FrOSCon’17 13/39
  • 15. HCL vs. JSON # An AMI variable ”ami” { description = ”custom AMI” } /* A multi line comment. */ resource ”aws_instance” ”web” { ami = ”${var.ami}” count = 2 source_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 | FrOSCon’17 14/39
  • 16. 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 | FrOSCon’17 15/39
  • 17. Terraform Process *.tf override.tfModules “source” terraform.tfvars plan state get plan apply destroy Martin Schütte | Terraform | FrOSCon’17 16/39
  • 18. Example: Add Provisioning # Importer resource ”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 master EOT } } Martin Schütte | Terraform | FrOSCon’17 17/39
  • 19. Example: Add Outputs # Storage resource ”aws_s3_bucket” ”importdisk” { ... } # Importer resource ”heroku_app” ”importer” { ... } # Outputs output ”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 | FrOSCon’17 18/39
  • 20. Example: Add Lifecycle Meta-Parameter # Storage resource ”aws_s3_bucket” ”importdisk” { bucket = ”${var.app_name}-${var.aws_region}-importdisk” acl = ”private” lifecycle { prevent_destroy = true } } Martin Schütte | Terraform | FrOSCon’17 19/39
  • 21. Demo $ terraform init $ terraform validate $ terraform plan -out=my.plan $ terraform show my.plan $ terraform apply my.plan $ terraform output $ terraform output -json $ terraform output importer_url $ curl -s $(terraform output importer_url) $ terraform graph | dot -Tpdf > graph.pdf && evince graph.pdf $ terraform plan -destroy $ terraform destroy Martin Schütte | Terraform | FrOSCon’17 20/39
  • 23. 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 | FrOSCon’17 21/39
  • 24. Example Module module ”database” { source = ”github.com/terraform-community-modules/tf_aws_rds” # DB Instance Inputs rds_instance_identifier = ”${terraform.workspace}-${var.app}-db” rds_allocated_storage = ”${var.database_size}” database_name = ”${var.database_name}” database_user = ”${var.database_user}” database_password = ”${var.database_password}” # DB Subnet Inputs subnets = [”${aws_subnet.dbnet.*.id}”] rds_vpc_id = ”${data.aws_vpc.app.id}” tags { Name = ”${terraform.workspace} - ${var.app} - DB” } } Martin Schütte | Terraform | FrOSCon’17 22/39
  • 25. terraform.tfstate • Terraform keeps known state of resources • Defaults to local state in terraform.tfstate • Optional remote state with different backends (S3, Azure Storage, Consul, Atlas, …) • Useful to sync multiple team members • May need additional mutex mechanism (v0.9 added state locking for Local, S3, and Consul) • Remote state is a data source Martin Schütte | Terraform | FrOSCon’17 23/39
  • 26. Example: Using State Import $ terraform import azurerm_storage_account.my_storage_account /subscriptions/e9b2ec19-ab6e-4547-a3ec-5a58e234ce5e/resourceGroups/ demo-res-group/providers/Microsoft.Storage/storageAccounts/demostorage20170418 azurerm_storage_account.my_storage_account: Importing from ID ... azurerm_storage_account.my_storage_account: Import complete! Imported azurerm_storage_account (ID: ...) azurerm_storage_account.my_storage_account: Refreshing state... (ID: ...) Import success! The resources imported are shown above. These are now in your Terraform state. Import does not currently generate configuration, so you must do this next. If you do not create configuration for the above resources, then the next ‘terraform plan‘ will mark them for destruction. $ terraform state list azurerm_storage_account.my_storage_account $ terraform state show azurerm_storage_account.my_storage_account id = /subscriptions/e9b2ec19... account_kind = Storage account_type = Standard_LRS location = westeurope name = demostorage20170418 ... Martin Schütte | Terraform | FrOSCon’17 24/39
  • 27. Example: Use Remote State (with Workspaces) terraform { required_version = ”>= 0.9.8” environment = ”${terraform.workspace}” backend ”s3” { bucket = ”ms-terraform-state” key = ”infra/ms-tf-demo/state” region = ”eu-central-1” } } $ terraform workspace new prod $ terraform workspace new dev $ aws s3 ls --recursive ”s3://ms-terraform-state/” ... 282 workspace:/dev/infra/ms-tf-demo/state ... 282 workspace:/prod/infra/ms-tf-demo/state Martin Schütte | Terraform | FrOSCon’17 25/39
  • 28. Example: Use Remote State to Chain Projects data ”terraform_remote_state” ”infra” { backend = ”s3” config { bucket = ”ms-terraform-state” key = ”workspace:/${terraform.workspace}/infra/ ⌋ ms-tf-demo/state”→ region = ”eu-central-1” } } resource ”aws_instance” ”foo” { # use state from vpc_project subnet_id = ”${data.terraform_remote_state.infra.app_subnet_id}”→ instance_type = ”t2.micro” ami = ”ami-b968bad6” } Martin Schütte | Terraform | FrOSCon’17 26/39
  • 29. Example: Using Data Source to Lookup Data # searches for most recent tagged AMI in own account data ”aws_ami” ”webami” { most_recent = true owners = [”self”] filter { name = ”tag:my_key” values = [”my_value”] } } # use AMI resource ”aws_instance” ”web” { instance_type = ”t2.micro” ami = ”${data.aws_ami.webami.id}” } Martin Schütte | Terraform | FrOSCon’17 27/39
  • 30. Example: “External” Data Source data ”external” ”dyndns” { program = [”bash”, ”${path.module}/variomedia_dyndns.sh”] query = { hostname = ”aws-demo.martin-schuette.de” ipaddress = ”${aws_eip.foo.public_ip}” } } Martin Schütte | Terraform | FrOSCon’17 28/39
  • 31. How to Write Own Plugins Now: • Learn you some Golang • Use the schema helper lib • Adapt to model of Provider (setup steps, authentication) and Resources (arguments/attributes and CRUD methods) • Start reading of simple plugins like builtin/providers/mysql  Future: • interface, support for Python, Ruby, C#, Java, … Martin Schütte | Terraform | FrOSCon’17 29/39
  • 32. Usage
  • 33. General Problemes for all Tools • Testing is inherently difficult • Provider coverage largely depends on community • Resource model mismatches, e. g. with Heroku apps • Ignorant of API rate limits, account ressource limits, etc. Martin Schütte | Terraform | FrOSCon’17 30/39
  • 34. Issues Under active development, current version 0.10.2 (August 16) • Modules are very simple • Lacking syntactic sugar (e. g. aggregations, common repetitions) • Big improvements in state management • Large variation in provider support, new project boundaries Martin Schütte | Terraform | FrOSCon’17 31/39
  • 35. New Features Recent Features in 0.7–0.10 • State Import • Data Sources • Workspaces (previously: State Environments) • Separate sub-projects for providers terraform-providers  • Support for gRPC-based plugins, i. e. providers in other languages Martin Schütte | Terraform | FrOSCon’17 32/39
  • 36. Comparable Tools Configuration Management Tools: • SaltStack Salt Cloud • Ansible modules • Puppet modules Vendor Tools: • Azure Resource Manager Templates • AWS CloudFormation • OpenStack Heat Martin Schütte | Terraform | FrOSCon’17 33/39
  • 37. Workflow • Avoid user credentials in Terraform code, use e. g. profiles and assume-role wrapper scripts • At least use separate user credentials, know how to revoke them • To hold credentials in VCS use PGP encryption, e. g. with Blackbox Martin Schütte | Terraform | FrOSCon’17 34/39
  • 38. Workflow (contd.) • Use a VCS, i. e. git • Namespaces! – Always add some ”${var.shortname}-${var.env}” • per project • per region • per account • per provider • Use remote state and consider access locking, e. g. with a single build server • Take a look at Hashicorp Atlas and its workflow Martin Schütte | Terraform | FrOSCon’17 35/39
  • 39. Hashicorp Toolset Martin Schütte | Terraform | FrOSCon’17 36/39
  • 40. Links and Resources • Terraform.io and hashicorp/terraform  • terraform-providers  • terraform-community-modules  • newcontext/kitchen-terraform  • Terraforming – Export existing AWS resources • Terraform: Beyond the Basics with AWS • A Comprehensive Guide to Terraform • Terraform, VPC, and why you want a tfstate file per env Martin Schütte | Terraform | FrOSCon’17 37/39
  • 41. Books Hopefully, deployments will become routine and boring–and in the world of operations, boring is a very good thing. — Terraform: Up & Running by Yevgeniy Brikman Defining system infrastructure as code and building it with tools doesn’t make the quality any better. At worst, it can complicate things. — Infrastructure as Code by Kief Morris Martin Schütte | Terraform | FrOSCon’17 38/39
  • 42. The End Thank You! — Questions? Workshop Terraform und AWS at 14:00 h in C 120 Martin Schütte @m_schuett info@martin-schuette.de slideshare.net/mschuett/  tinyurl.com/froscon17-tf Martin Schütte | Terraform | FrOSCon’17 39/39