Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(1)Terraform Basics and a Docker Deployment-(15)Null Resources and Local-Exec

2018年10月04日


Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
1. Terraform Basics and a Docker Deployment
15. Null Resources and Local-Exec

~/docker# docker ps -a

CONTAINER ID        IMAGE               COMMAND                  CREATED             STATUS                        PORTS                  NAMES
311610e3b765        a7d409c8a8af        "docker-entrypoint.s…"   30 minutes ago      Up 30 minutes                 0.0.0.0:80->2368/tcp   prod_blog
2c856cff5dc5        98c65d66926b        "docker-entrypoint.s…"   32 minutes ago      Exited (137) 17 minutes ago                          dev_blog

~/docker# docker stop 2c856cff5dc5
2c856cff5dc5

~/docker# docker rm 2c856cff5dc5
2c856cff5dc5

~/docker# vim main.tf
# Download the latest Ghost image
module "image" {
  source = "./image"
  image = lookup(var.image, var.env)
}

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

resource "null_resource" "null_id" {
  provisioner "local-exec" {
    command = "echo ${module.container.container_name}:${module.container.ip} >> container.txt"
  }
}

~/docker# terraform init
Initializing modules...

Initializing the backend...

Initializing provider plugins...
- Using previously-installed hashicorp/aws v3.9.0
- Using previously-installed terraform-providers/docker v2.7.2
- Finding latest version of hashicorp/null...
- Installing hashicorp/null v2.1.2...
- Installed hashicorp/null v2.1.2 (signed by HashiCorp)

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"
* hashicorp/null: version = "~> 2.1.2"
* 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# terraform apply
var.env
  env: dev or prod

  Enter a value: dev

module.image.docker_image.image_id: Refreshing state... [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost: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:

  # null_resource.null_id will be created
  + resource "null_resource" "null_id" {
      + id = (known after apply)
    }

  # module.container.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            = "sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5"
      + 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             = "dev_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 = 8080
          + internal = 2368
          + ip       = "0.0.0.0"
          + protocol = "tcp"
        }
    }

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

Changes to Outputs:
  + IP_Address     = (known after apply)
  + container_name = "dev_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

module.container.docker_container.container_id: Creating...
module.container.docker_container.container_id: Creation complete after 2s [id=06a04e5f5e8db77a3b60327f0386e7b6c53fe0be83e0feff8d711227dc276025]
null_resource.null_id: Creating...
null_resource.null_id: Provisioning with 'local-exec'...
null_resource.null_id (local-exec): Executing: ["/bin/sh" "-c" "echo dev_blog:172.17.0.2 >> container.txt"]
null_resource.null_id: Creation complete after 0s [id=3151921924678757368]

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

Outputs:

IP_Address = 172.17.0.2
container_name = dev_blog

~/docker# cat container.txt
dev_blog:172.17.0.2

Category: orchestration Tags: public

Upvote


Downvote