Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform-(1)Terraform Basics and a Docker Deployment-(9)Modules - The Image Module
2018年10月04日
~/docker# cd image
~/docker/image# ls
~/docker/image# vim main.tf
~/docker/image# vim variables.tf
~/docker/image# vim outputs.tf
~/docker/image# cd ..
~/docker# terraform destroy
~/docker# cd image
~/docker/image# terraform init
~/docker/image# terraform plan
~/docker/image# terraform apply
~/docker/image# terraform destroy
Managing Applications and Infrastructure with Terraform-Deploying Infrastructure with Terraform
1. Terraform Basics and a Docker Deployment
9. Modules - The Image Module
In the last post, we have built the directory structure to hold the Terraform modules. In this post, we will setup the "image" module.
~/docker# ls
container image main.tf outputs.tf terraform.tfstate terraform.tfstate.backup variables.tf
~/docker# cd image
~/docker/image# ls
main.tf outputs.tf variables.tf
~/docker/image# vim main.tf
terraform { required_providers { aws = { source = "hashicorp/aws" } docker = { source = "terraform-providers/docker" } } required_version = ">= 0.13" } # Download the Docker image resource "docker_image" "image_id" { name = var.image }
~/docker/image# vim variables.tf
variable "image" { description = "name of the image" }
~/docker/image# vim outputs.tf
output "image_out" { value = docker_image.image_id.latest }
~/docker/image# cd ..
~/docker# terraform destroy
docker_image.image_id: Refreshing state... [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest] docker_container.container_id: Refreshing state... [id=ffb0afe6004c7ecb4b205a5b7bcc903debf22aa654b5381f22180b06ad357206] 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 = "ffb0afe6004c" -> null - id = "ffb0afe6004c7ecb4b205a5b7bcc903debf22aa654b5381f22180b06ad357206" -> 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=ffb0afe6004c7ecb4b205a5b7bcc903debf22aa654b5381f22180b06ad357206] docker_container.container_id: Destruction complete after 1s docker_image.image_id: Destroying... [id=sha256:98c65d66926b2da9fbb696d43aadfaf3fee847b7185e132e199532bc549aeba5ghost:latest] docker_image.image_id: Destruction complete after 7s Destroy complete! Resources: 2 destroyed.
~/docker# cd image
~/docker/image# terraform init
Initializing the backend... Initializing provider plugins... - Finding latest version of hashicorp/aws... - Finding latest version of terraform-providers/docker... - Installing hashicorp/aws v3.9.0... - Installed hashicorp/aws v3.9.0 (signed by HashiCorp) - Installing terraform-providers/docker v2.7.2... - Installed terraform-providers/docker v2.7.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" * 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/image# terraform plan
var.image name of the image Enter a value: ghost:alpine 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: # docker_image.image_id will be created + resource "docker_image" "image_id" { + id = (known after apply) + latest = (known after apply) + name = "ghost:alpine" } Plan: 1 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.Enter "ghost:alpine" when prompted.
~/docker/image# terraform apply
var.image name of the image Enter a value: ghost:alpine 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_image.image_id will be created + resource "docker_image" "image_id" { + id = (known after apply) + latest = (known after apply) + name = "ghost:alpine" } Plan: 1 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 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 21s [id=sha256:a7d409c8a8af50ce6df5cc5cb94600b657acc9cfd82216d89a83730467bdd439ghost:alpine] Apply complete! Resources: 1 added, 0 changed, 0 destroyed. Outputs: image_out = sha256:a7d409c8a8af50ce6df5cc5cb94600b657acc9cfd82216d89a83730467bdd439Enter "ghost:alpine" when prompted.
~/docker/image# terraform destroy
var.image name of the image Enter a value: yes docker_image.image_id: Refreshing state... (ID: sha256:dddc4523848b1834b333dcd5e00b5234...f260c9a7d6082d1ecf832d2fa5ghost:alpine) 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_image.image_id Plan: 0 to add, 0 to change, 1 to 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 docker_image.image_id: Destroying... (ID: sha256:dddc4523848b1834b333dcd5e00b5234...f260c9a7d6082d1ecf832d2fa5ghost:alpine) docker_image.image_id: Destruction complete after 1s Destroy complete! Resources: 1 destroyed.Enter "ghost:alpine" when prompted.