Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(1)Terraform Basics and a Docker Deployment-(11)Modules - The Root Module

2018年10月04日


Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
1. Terraform Basics and a Docker Deployment
11. Modules - The Root Module

~/docker/container# cd ..

~/docker# terraform destroy

Do you really want to destroy?
  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


Destroy complete! Resources: 0 destroyed.

~/docker# ls
container  image  main.tf  outputs.tf  terraform.tfstate  terraform.tfstate.backup  variables.tf

Append below blocks in the main.tf file.
~/docker# vim main.tf
# Download the latest Ghost image
module "image" {
  source = "./image"
  image = var.image
}

# Start the Container
module "container" {
  source = "./container"
  image = module.image.image_out
  name = var.container_name
  int_port = var.int_port
  ext_port = var.ext_port
}

Remove below blocks.
# 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
  }
}

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

~/docker# terraform init
Initializing modules...
- container in container
- image in image

Initializing the backend...

Initializing provider plugins...
- Using previously-installed hashicorp/aws v3.9.0
- Using previously-installed terraform-providers/docker v2.7.2

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, we recommend adding version constraints in a required_providers block
in your configuration, with the constraint strings suggested below.

* hashicorp/aws: version = "~> 3.9.0"
* terraform-providers/docker: version = "~> 2.7.2"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.

~/docker# ls
container  image  main.tf  outputs.tf  terraform.tfstate  terraform.tfstate.backup  variables.tf

~/docker# vim outputs.tf
# Output name and ip address
output "IP Address" {
  value = module.container.ip
}
output "container_name" {
  value = module.container.container_name
}

~/docker# terraform init
Initializing modules...
- module.image
- module.container

Initializing provider plugins...

The following providers do not have any version constraints in configuration,
so the latest version was installed.

To prevent automatic upgrades to new major versions that may contain breaking
changes, it is recommended to add version = "..." constraints to the
corresponding provider blocks in configuration, with the constraint strings
suggested below.

* provider.docker: version = "~> 1.0"

Terraform has been successfully initialized!

You may now begin working with Terraform. Try running "terraform plan" to see
any changes that are required for your infrastructure. All Terraform commands
should now work.

If you ever set or change modules or backend configuration for Terraform,
rerun this command to reinitialize your working directory. If you forget, other
commands will detect it and remind you to do so if necessary.
*
~/docker# cat container/outputs.tf
# Output the IP Address and name of the container
output "ip" {
  value = "${docker_container.container_id.ip_address}"
}

output "container_name" {
  value = "${docker_container.container_id.name}"
}
*
~/docker# cat outputs.tf
# Output name and ip address
output "IP Address" {
  value = "${module.container.ip}"
}
output "container_name" {
  value = "${module.container.container_name}"
}

~/docker# terraform plan
Refreshing Terraform state in-memory prior to plan...
The refreshed state will be used to calculate this plan, but will not be
persisted to local or remote state storage.


------------------------------------------------------------------------

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:

  + module.container.docker_container.container_id
      id:                       <computed>
      bridge:                   <computed>
      gateway:                  <computed>
      image:                    "${var.image}"
      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"

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


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

------------------------------------------------------------------------

Note: You didn't specify an "-out" parameter to save this plan, so Terraform
can't guarantee that exactly these actions will be performed if
"terraform apply" is subsequently run.

~/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:

  + module.container.docker_container.container_id
      id:                       <computed>
      bridge:                   <computed>
      gateway:                  <computed>
      image:                    "${var.image}"
      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"

  + module.image.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: yes

module.image.docker_image.image_id: Creating...
  latest: "" => "<computed>"
  name:   "" => "ghost:latest"
module.image.docker_image.image_id: Still creating... (10s elapsed)
module.image.docker_image.image_id: Still creating... (20s elapsed)
module.image.docker_image.image_id: Still creating... (30s elapsed)
module.image.docker_image.image_id: Creation complete after 31s (ID: sha256:5d42eda6891259afd89cab3a0f005c18...628a74d9bf3f85d17756928c73ghost:latest)
module.container.docker_container.container_id: Creating...
  bridge:                   "" => "<computed>"
  gateway:                  "" => "<computed>"
  image:                    "" => "sha256:5d42eda6891259afd89cab3a0f005c18735a17628a74d9bf3f85d17756928c73"
  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"
module.container.docker_container.container_id: Creation complete after 1s (ID: 244b89bff50e3373b967f1ffe78926e3e7e3cf586629f1e78a86dfc989a83b89)

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

Outputs:

IP Address = 172.17.0.2
container_name = blog

~/docker# terraform destroy
module.image.docker_image.image_id: Refreshing state... [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest]
module.container.docker_container.container_id: Refreshing state... [id=01c9bfc10452a976b8748b5ea89849c721c01946b9d1f26b4c00a4255b2bdff8]

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:

  # module.container.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          = "01c9bfc10452" -> null
      - id                = "01c9bfc10452a976b8748b5ea89849c721c01946b9d1f26b4c00a4255b2bdff8" -> 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
        }
    }

  # module.image.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

module.container.docker_container.container_id: Destroying... [id=01c9bfc10452a976b8748b5ea89849c721c01946b9d1f26b4c00a4255b2bdff8]
module.container.docker_container.container_id: Destruction complete after 1s
module.image.docker_image.image_id: Destroying... [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest]
module.image.docker_image.image_id: Destruction complete after 9s

Destroy complete! Resources: 2 destroyed.


Category: orchestration Tags: public

Upvote


Downvote