Marius Stanca.

A practical introduction to Terraform

2018-03-17

What is Terraform?

Terraform is a tool for building, changing, and versioning infrastructure safely and efficiently. Terraform can manage existing and popular service providers as well as custom in-house solutions.

The key features of Terraform are:

  • Infrastructure as Code
  • Execution Plans
  • Resource Graph
  • Change Automation

More information here.

I will talk more about key features in another post. Now let's show a practical introduction example to Terraform.
Example scenario: I want to create a server (ARM) in scaleway cloud provider, 50GB SSD, Paris region and Debian Jessie as operating system. This server must have a public IP and 80, 443 opened ports. Also, after finished creating server I want to know what is my public IP.

Steps
  1. Install Terraform: link
  2. Create three files, for example main.tf, variables.tf and output.tf in a folder with name scaleway (you can put what name of files you want)
  3. Type terraform init, be sure you are in the same location where *.tf files are. In our case, scaleway
  4. You need few data from scaleway account, organization and token. When you got them just export them:

    export SCALEWAY_ORGANIZATION=<your_organization_name>

    export SCALEWAY_TOKEN=<your_taken_key>

  5. Open and write the following code in variables.tf:

    variable "region" {
      default = "par1" # Paris region
    }
    
    variable "project" {
      default = "your_project"
    }
    
    variable "server_type" {
      default = "C1" # Type of server
    }
    
    variable "storage" {
      default = 50
    }
    
    variable "volume_type" {
      default = "l_ssd"
    }
    
    variable "image_architecture" {
      default = "arm"
    }
    
    variable "image_name" {
      default = "Debian Jessie"
    }
    
  6. Open and write the following code in main.tf:

    provider "scaleway" {
      #organization = "${var.organization_key}"
      #token        = "${var.token}"
      region       = "${var.region}"
    }
    
    # Give an IP
    resource "scaleway_ip" "ip" {
      server = "${scaleway_server.m.id}"
    }
    
    # Architecture and image name
    data "scaleway_image" "os" {
      architecture = "${var.image_architecture}"
      name         = "${var.image_name}"
    }
    
    # Create machine
    resource "scaleway_server" "m" {
      name  = "${var.project}"
      image = "${data.scaleway_image.os.id}"
      type  = "${var.server_type}"
    }
    
    # Setup volume
    resource "scaleway_volume" "v" {
      name       = "${var.project}"
      size_in_gb = "${var.storage}"
      type       = "${var.volume_type}"
    }
    
    # Attach volume
    resource "scaleway_volume_attachment" "va" {
      server = "${scaleway_server.m.id}"
      volume = "${scaleway_volume.v.id}"
    }
    
    # Security group
    resource "scaleway_security_group" "default" {
      name        = "${var.project}"
      description = "Default security group"
    }
    
    # Security group rule
    resource "scaleway_security_group_rule" "http" {
      security_group = "${scaleway_security_group.default.id}"
    
      action    = "accept"
      direction = "inbound"
      ip_range  = "0.0.0.0/0"
      protocol  = "TCP"
      port      = 80
    }
    
    resource "scaleway_security_group_rule" "https" {
      security_group = "${scaleway_security_group.default.id}"
    
      action    = "accept"
      direction = "inbound"
      ip_range  = "0.0.0.0/0"
      protocol  = "TCP"
      port      = 443
    }
    
  7. Same for output.tf:

    output "public-ip" {
      value = "${scaleway_ip.ip.id}" # This will print the public IP
    }
    
  8. Type terraform plan. This checks if there is any resources created. If there is, this will update, otherwise will create (step 9.)

  9. Finnaly type terraform apply. This create or update (depends of step 8) resources in scaleway cloud.

terraform output will print the output, in my case i will have as output public IP address.

Note

The code is here.


© 2018. mariuss.me. Build with Python and Skeleton.