Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(1)Terraform Basics and a Docker Deployment-(6)Terraform Variables

2018年10月04日


Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
1. Terraform Basics and a Docker Deployment
6. Terraform Variables

In this post, we will separate the variables from the resource configuration blocks, so that the script is more readable and well arranged.

Input variables serve as parameters for a Terraform module, allowing aspects of the module to be customized without altering the module's own source code, and allowing modules to be shared between different configurations.
~/docker# vim main.tf

variable "image" {
  description = "image for container"
}

# Download the latest Ghost image
resource "docker_image" "image_id" {
  name = var.image
}

~/docker# terraform apply
var.image
  image for container

  Enter a value: ghost:latest


An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # docker_container.container_id will be created
  + resource "docker_container" "container_id" {
      + attach           = false
      + bridge           = (known after apply)
      + command          = (known after apply)
      + container_logs   = (known after apply)
      + dns              = (known after apply)
      + dns_opts         = (known after apply)
      + entrypoint       = (known after apply)
      + exit_code        = (known after apply)
      + gateway          = (known after apply)
      + hostname         = (known after apply)
      + id               = (known after apply)
      + image            = (known after apply)
      + ip_address       = (known after apply)
      + ip_prefix_length = (known after apply)
      + ipc_mode         = (known after apply)
      + log_driver       = (known after apply)
      + log_opts         = (known after apply)
      + logs             = false
      + must_run         = true
      + name             = "blog"
      + network_data     = (known after apply)
      + read_only        = false
      + restart          = "no"
      + rm               = false
      + shm_size         = (known after apply)
      + start            = true
      + user             = (known after apply)
      + working_dir      = (known after apply)

      + ports {
          + external = 80
          + internal = 2368
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }
    }

  # docker_image.image_id will be created
  + resource "docker_image" "image_id" {
      + id     = (known after apply)
      + latest = (known after apply)
      + name   = "ghost:latest"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + IP_Address     = (known after apply)
  + container_name = "blog"

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: ^C
Interrupt received.
Please wait for Terraform to exit or data loss may occur.
Gracefully shutting down...

Error: Error asking for approval: interrupted
When prompt for a name, type "ghost:latest".

Ctrl + C

The variable declaration can also include a default argument. If present, the variable is considered to be optional and the default value will be used if no value is set when calling the module or running Terraform. The default argument requires a literal value and cannot reference other objects in the configuration.
~/docker# vim main.tf
variable "image" {
  description = "image for container"
  default = "ghost:latest"
}

~/docker# terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  + docker_container.container_id
      id:                       <computed>
      bridge:                   <computed>
      gateway:                  <computed>
      image:                    "${docker_image.image_id.latest}"
      ip_address:               <computed>
      ip_prefix_length:         <computed>
      log_driver:               "json-file"
      must_run:                 "true"
      name:                     "blog"
      ports.#:                  "1"
      ports.580670141.external: "80"
      ports.580670141.internal: "2368"
      ports.580670141.ip:       ""
      ports.580670141.protocol: "tcp"
      restart:                  "no"

  + docker_image.image_id
      id:                       <computed>
      latest:                   <computed>
      name:                     "ghost:latest"


Plan: 2 to add, 0 to change, 0 to destroy.

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: ^C

Error: Error asking for approval: interrupted

Update the resource configuration block with variable reference.
~/docker# vim main.tf
variable "image" {
  description = "image for container"
  default = "ghost:latest"
}

variable "container_name" {
  description = "Name of Container"
  default = "blog"
}

variable "int_port" {
  description = "Internal port for container"
  default = "2368"
}

variable "ext_port" {
  description = "External port for container"
  default = "80"
}


# Download the latest Ghost image
resource "docker_image" "image_id" {
  name = var.image
}

# Start the container, reference the above "docker_image" and "image_id"
resource "docker_container" "container_id" {
  name  = var.container_name
  image = docker_image.image_id.latest
  ports {
    internal = var.int_port
    external = var.ext_port
  }
}

# Output name and ip address
output "IP Address" {
  value = docker_container.container_id.ip_address
}

output "container_name" {
  value = docker_container.container_id.name
}

~/docker# terraform apply
An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  + create

Terraform will perform the following actions:

  # docker_container.container_id will be created
  + resource "docker_container" "container_id" {
      + attach           = false
      + bridge           = (known after apply)
      + command          = (known after apply)
      + container_logs   = (known after apply)
      + dns              = (known after apply)
      + dns_opts         = (known after apply)
      + entrypoint       = (known after apply)
      + exit_code        = (known after apply)
      + gateway          = (known after apply)
      + hostname         = (known after apply)
      + id               = (known after apply)
      + image            = (known after apply)
      + ip_address       = (known after apply)
      + ip_prefix_length = (known after apply)
      + ipc_mode         = (known after apply)
      + log_driver       = (known after apply)
      + log_opts         = (known after apply)
      + logs             = false
      + must_run         = true
      + name             = "blog"
      + network_data     = (known after apply)
      + read_only        = false
      + restart          = "no"
      + rm               = false
      + shm_size         = (known after apply)
      + start            = true
      + user             = (known after apply)
      + working_dir      = (known after apply)

      + ports {
          + external = 80
          + internal = 2368
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }
    }

  # docker_image.image_id will be created
  + resource "docker_image" "image_id" {
      + id     = (known after apply)
      + latest = (known after apply)
      + name   = "ghost:latest"
    }

Plan: 2 to add, 0 to change, 0 to destroy.

Changes to Outputs:
  + IP_Address     = (known after apply)
  + container_name = "blog"

Do you want to perform these actions?
  Terraform will perform the actions described above.
  Only 'yes' will be accepted to approve.

  Enter a value: yes

docker_image.image_id: Creating...
docker_image.image_id: Still creating... [10s elapsed]
docker_image.image_id: Still creating... [20s elapsed]
docker_image.image_id: Creation complete after 27s [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest]
docker_container.container_id: Creating...
docker_container.container_id: Creation complete after 2s [id=12c3fb7eefb1c7472f0d985b977baa1518b174fd6fdb8ac493b168a2938244c0]

Apply complete! Resources: 2 added, 0 changed, 0 destroyed.

Outputs:

IP_Address = 172.17.0.2
container_name = blog

~/docker# terraform destroy
docker_image.image_id: Refreshing state... [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest]
docker_container.container_id: Refreshing state... [id=12c3fb7eefb1c7472f0d985b977baa1518b174fd6fdb8ac493b168a2938244c0]

An execution plan has been generated and is shown below.
Resource actions are indicated with the following symbols:
  - destroy

Terraform will perform the following actions:

  # docker_container.container_id will be destroyed
  - resource "docker_container" "container_id" {
      - attach            = false -> null
      - command           = [
          - "node",
          - "current/index.js",
        ] -> null
      - cpu_shares        = 0 -> null
      - dns               = [] -> null
      - dns_opts          = [] -> null
      - dns_search        = [] -> null
      - entrypoint        = [
          - "docker-entrypoint.sh",
        ] -> null
      - gateway           = "172.17.0.1" -> null
      - group_add         = [] -> null
      - hostname          = "12c3fb7eefb1" -> null
      - id                = "12c3fb7eefb1c7472f0d985b977baa1518b174fd6fdb8ac493b168a2938244c0" -> null
      - image             = "sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5" -> null
      - ip_address        = "172.17.0.2" -> null
      - ip_prefix_length  = 16 -> null
      - ipc_mode          = "private" -> null
      - links             = [] -> null
      - log_driver        = "json-file" -> null
      - log_opts          = {} -> null
      - logs              = false -> null
      - max_retry_count   = 0 -> null
      - memory            = 0 -> null
      - memory_swap       = 0 -> null
      - must_run          = true -> null
      - name              = "blog" -> null
      - network_data      = [
          - {
              - gateway          = "172.17.0.1"
              - ip_address       = "172.17.0.2"
              - ip_prefix_length = 16
              - network_name     = "bridge"
            },
        ] -> null
      - network_mode      = "default" -> null
      - privileged        = false -> null
      - publish_all_ports = false -> null
      - read_only         = false -> null
      - restart           = "no" -> null
      - rm                = false -> null
      - shm_size          = 64 -> null
      - start             = true -> null
      - sysctls           = {} -> null
      - tmpfs             = {} -> null
      - working_dir       = "/var/lib/ghost" -> null

      - ports {
          - external = 80 -> null
          - internal = 2368 -> null
          - ip       = "0.0.0.0" -> null
          - protocol = "tcp" -> null
        }
    }

  # docker_image.image_id will be destroyed
  - resource "docker_image" "image_id" {
      - id     = "sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest" -> null
      - latest = "sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5" -> null
      - name   = "ghost:latest" -> null
    }

Plan: 0 to add, 0 to change, 2 to destroy.

Changes to Outputs:
  - IP_Address     = "172.17.0.2" -> null
  - container_name = "blog" -> null

Do you really want to destroy all resources?
  Terraform will destroy all your managed infrastructure, as shown above.
  There is no undo. Only 'yes' will be accepted to confirm.

  Enter a value: yes

docker_container.container_id: Destroying... [id=12c3fb7eefb1c7472f0d985b977baa1518b174fd6fdb8ac493b168a2938244c0]
docker_container.container_id: Destruction complete after 1s
docker_image.image_id: Destroying... [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest]
docker_image.image_id: Destruction complete after 8s

Destroy complete! Resources: 2 destroyed.


References

Input Variables

Category: orchestration Tags: public

Upvote


Downvote