KEMBAR78
Infrastructure as code with terraform and packer | PPTX
Infrastructure as code with
Terraform and Packer
Alex Landa
Head of DevOps and BigData technologies at Trainologic
We will talk about
•Why infrastructure as code?
•How to build images with Packer?
•Managing cloud environments with
Terraform
Story time
• Once upon a time a cool Machine Learning startup raised seed
money - lets call them CyberTemp
• They wrote in Python and used AWS
The story part ||
• They wanted use Auto-Scaling so they created an AMI
• By hand
• Took a day to configure
And then they had to update..
• They wanted to use a different base AMI that support GPUs
So what went wrong?
• No automatic way to configure the server
• Error prone
• No connection to the CI process
• No way to track changes
Infrastructure as Code
• Writing code to manage configuration, provisioning and deployment
• Scripts
• Ansible
• Docker
• Terraform
• ..
• Using software development practices:
• Source control
• Testing
• Code reviews
• Design
• 3rd libraries
• Knowledge sharing
What if CyberTemp
• Has to deploy on several cloud providers?
• It wants to create an automatic deployment?
• Want to create a VM images for on premise deployment?
Packer
• “An open source tool for creating identical machine images
for multiple platforms from a single source configuration”
• Machine image contains pre-configures OS and installed
software which is used to create and run machines
• AMI for EC2
• VMDK/VMX for VMware
• OVF for VirtualBox
• Different cloud providers
Packer template
• A JSON file that contains the build configuration
• Composed of several parts:
• Builders – Create the machines and generates the images from them
in the configured platform
• Provisioners – Install and configure on the machine
• Post-processors – Take the result of a builder or another post-
processor
• Variables – key value strings to parametrize templates
Packer Example
Builders
• Responsible for creating the machine
• Provide a way to run the Provisioners there – communicator
• SSH
• WinRM for Windows machines
• Packages the machine into a deployable image
• Every build definition maps to a single build.
Provisioners
• Use built-in and existing module to install and configure the
machine after booting
• Installing packages
• Applying patches
• Creating users and folders
• Downloading or copying application code
• The Provisioners are executed in the order they are defined
Post Processors
• Optional section to run processing on the built image
• Compression, tagging, uploading files, etc..
• Every post processor will be ran on every build image (unless
configured otherwise)
Variables
• The way to pass environment variables to packer
• Templating the build
So we have an image..
• Cloud deployments are complex
• Contain
• Permissions
• Network definitions
• Machines
• Load balancers
• Databases
• Other managed services
Now lets deploy in Europe
If only we had our infrastructure in code..
Terraform
• An open-source tool for building, updating and managing
infrastructure
• Uses declarative approach – the state that I want to achieve
instead of set of instructions
• Configuration files are written in HCL, also support JSON
• Written in Go
The Terraform work flow
• Define – create or update your configuration file
• Init – initializes the terraform environment and provider binaries
• Plan – Terraform creates an execution plan from the configuration
files and the current state
• Apply – Applies the execution plan, modify the cloud infrastructure
• Destroy (optional  ) – removes all the resources specified in the
configuration
Simple example
resource "aws_instance" "web" {
ami = "ami-679d3f1e"
instace_type = "t2.micro
}
Resource
• Everything with life cycle – machine, load balancers, network …
• Has a type – different for every resource type and cloud provider
• Has a unique name – for internal reference
• Uses attributes – configuration for the type
• Uses Interpolation – a way to reference other resources and manipulate values
Terraform plan
• Terraform creates a resource graph
• It calculates the dependencies of every resource and the action that
should happen – the result is a plan
+ aws_instance.web
id: <computed>
ami: "ami-679d3f1e"
associate_public_ip_address: <computed>
...
get_password_data: "false"
instance_state: <computed>
instance_type: "t2.micro"
...
Reading the Terraform plan
•+ a resource will be created
• - resource will be destroyed
• ~ resource will be updated in place
• -/+ resource will be destroyed and recreated
Terraform apply
• Generates an execution plan if not given
• Asks for confirmation for the changes to come
• Executes the plan
• Updates the state
State of the “Art”
• Used by Terraform to map between the “actual” cloud resources
to the configured one
• Contains resource metadata - the real dependencies between
resources
• Refreshed before actions to validate the “State of the world”
• A JSON file created in the local directory with the name
terraform.tfstate
Remote state
• Local state is an issue when a team works together
• Remote state allows Terraform to synchronize state across different
machines
• Uses “backends” – determines how state is loaded and updated:
• S3
• Consul
• ETCD
• Terraform enterprise …
Templating Terraform
• Terraform allows to template configuration by using variables
• As convention defined in variables.tf
• Support lists and maps
• Can be set:
• In a file terraform.tfvars
• As a command line flags -var ‘region=us-west-2’
• UI
• Defaults
variable "region" {}
Output
• A way to organize data back to the user
• Tells Terraform what data is important
• Can be viewed as part of the ”apply” command result,
or by using terraform output web_ip
•
output "web_ip" {
value = "${aws_instance.web.public_ip}"
}
Terraform modules
• A way to structure Terraform to reusable pieces
• Self-contained packages that are managed as a group
• Terraform registry – a public repository for popular modules
• To use a module:
• Declare in config
• Run terraform init – downloads the module
module "vpc" {
source = "terraform-aws-modules/vpc/aws"
name = "my-vpc"
cidr = "10.0.0.0/16
…
Questions?
variable "questions" {
type = "list"
description = "???"
}

Infrastructure as code with terraform and packer

  • 1.
    Infrastructure as codewith Terraform and Packer Alex Landa Head of DevOps and BigData technologies at Trainologic
  • 2.
    We will talkabout •Why infrastructure as code? •How to build images with Packer? •Managing cloud environments with Terraform
  • 3.
    Story time • Onceupon a time a cool Machine Learning startup raised seed money - lets call them CyberTemp • They wrote in Python and used AWS
  • 4.
    The story part|| • They wanted use Auto-Scaling so they created an AMI • By hand • Took a day to configure
  • 5.
    And then theyhad to update.. • They wanted to use a different base AMI that support GPUs
  • 6.
    So what wentwrong? • No automatic way to configure the server • Error prone • No connection to the CI process • No way to track changes
  • 7.
    Infrastructure as Code •Writing code to manage configuration, provisioning and deployment • Scripts • Ansible • Docker • Terraform • .. • Using software development practices: • Source control • Testing • Code reviews • Design • 3rd libraries • Knowledge sharing
  • 8.
    What if CyberTemp •Has to deploy on several cloud providers? • It wants to create an automatic deployment? • Want to create a VM images for on premise deployment?
  • 9.
    Packer • “An opensource tool for creating identical machine images for multiple platforms from a single source configuration” • Machine image contains pre-configures OS and installed software which is used to create and run machines • AMI for EC2 • VMDK/VMX for VMware • OVF for VirtualBox • Different cloud providers
  • 10.
    Packer template • AJSON file that contains the build configuration • Composed of several parts: • Builders – Create the machines and generates the images from them in the configured platform • Provisioners – Install and configure on the machine • Post-processors – Take the result of a builder or another post- processor • Variables – key value strings to parametrize templates
  • 11.
  • 12.
    Builders • Responsible forcreating the machine • Provide a way to run the Provisioners there – communicator • SSH • WinRM for Windows machines • Packages the machine into a deployable image • Every build definition maps to a single build.
  • 13.
    Provisioners • Use built-inand existing module to install and configure the machine after booting • Installing packages • Applying patches • Creating users and folders • Downloading or copying application code • The Provisioners are executed in the order they are defined
  • 14.
    Post Processors • Optionalsection to run processing on the built image • Compression, tagging, uploading files, etc.. • Every post processor will be ran on every build image (unless configured otherwise)
  • 15.
    Variables • The wayto pass environment variables to packer • Templating the build
  • 16.
    So we havean image.. • Cloud deployments are complex • Contain • Permissions • Network definitions • Machines • Load balancers • Databases • Other managed services
  • 17.
    Now lets deployin Europe If only we had our infrastructure in code..
  • 18.
    Terraform • An open-sourcetool for building, updating and managing infrastructure • Uses declarative approach – the state that I want to achieve instead of set of instructions • Configuration files are written in HCL, also support JSON • Written in Go
  • 19.
    The Terraform workflow • Define – create or update your configuration file • Init – initializes the terraform environment and provider binaries • Plan – Terraform creates an execution plan from the configuration files and the current state • Apply – Applies the execution plan, modify the cloud infrastructure • Destroy (optional  ) – removes all the resources specified in the configuration
  • 20.
    Simple example resource "aws_instance""web" { ami = "ami-679d3f1e" instace_type = "t2.micro }
  • 21.
    Resource • Everything withlife cycle – machine, load balancers, network … • Has a type – different for every resource type and cloud provider • Has a unique name – for internal reference • Uses attributes – configuration for the type • Uses Interpolation – a way to reference other resources and manipulate values
  • 22.
    Terraform plan • Terraformcreates a resource graph • It calculates the dependencies of every resource and the action that should happen – the result is a plan + aws_instance.web id: <computed> ami: "ami-679d3f1e" associate_public_ip_address: <computed> ... get_password_data: "false" instance_state: <computed> instance_type: "t2.micro" ...
  • 23.
    Reading the Terraformplan •+ a resource will be created • - resource will be destroyed • ~ resource will be updated in place • -/+ resource will be destroyed and recreated
  • 24.
    Terraform apply • Generatesan execution plan if not given • Asks for confirmation for the changes to come • Executes the plan • Updates the state
  • 25.
    State of the“Art” • Used by Terraform to map between the “actual” cloud resources to the configured one • Contains resource metadata - the real dependencies between resources • Refreshed before actions to validate the “State of the world” • A JSON file created in the local directory with the name terraform.tfstate
  • 26.
    Remote state • Localstate is an issue when a team works together • Remote state allows Terraform to synchronize state across different machines • Uses “backends” – determines how state is loaded and updated: • S3 • Consul • ETCD • Terraform enterprise …
  • 27.
    Templating Terraform • Terraformallows to template configuration by using variables • As convention defined in variables.tf • Support lists and maps • Can be set: • In a file terraform.tfvars • As a command line flags -var ‘region=us-west-2’ • UI • Defaults variable "region" {}
  • 28.
    Output • A wayto organize data back to the user • Tells Terraform what data is important • Can be viewed as part of the ”apply” command result, or by using terraform output web_ip • output "web_ip" { value = "${aws_instance.web.public_ip}" }
  • 29.
    Terraform modules • Away to structure Terraform to reusable pieces • Self-contained packages that are managed as a group • Terraform registry – a public repository for popular modules • To use a module: • Declare in config • Run terraform init – downloads the module module "vpc" { source = "terraform-aws-modules/vpc/aws" name = "my-vpc" cidr = "10.0.0.0/16 …
  • 30.
    Questions? variable "questions" { type= "list" description = "???" }

Editor's Notes

  • #28 https://www.terraform.io/intro/getting-started/variables.html
  • #29 https://www.terraform.io/intro/getting-started/variables.html
  • #30 https://www.terraform.io/intro/getting-started/modules.html