visit
The Terraform language uses configuration files that are named with
.tf
file extension. There is also a JSON-based variant of the language that is named with the .tf.json
file extension.We will start by creating a very simple Terraform file that will pull down the image from Docker Hub and start the container. In addition, we will use input variables. They serve as parameters for a Terraform file. A variable block configures a single input variable for a Terraform module. Each block declares a single variable. And we will create a map to specify different environment variables based on conditions. This allows us to dynamically deploy infrastructure configurations based on information we pass to the deployment.To start with we need to set up the environment:mkdir terraform/
cd terraform/
vi variables.tf
# Define variables
variable "environment" {
description = "env: production or development"
}
variable "image_name" {
type = "map"
description = "Image name for container"
default = {
prod = "ghost:alpine"
dev = "ghost:default"
}
}
variable "container_name" {
type = "map"
description = "Name of the container"
default = {
prod = "container_production"
dev = "container_development"
}
}
variable "internal_port" {
description = "Internal port for container"
default = "2368"
}
variable "external_port" {
type = "map"
description = "External port for container"
default {
prod = "80"
dev = "8081"
}
}
vi main.tf
# Download the latest Ghost image
resource "docker_image" "image_id" {
name = "${lookup(var.image_name)}"
}
# Start the Container
resource "docker_container" "container_id" {
name = "${lookup(var.container_name)}"
image = "${docker_image.image_id.latest}"
ports {
internal = "${var.internal_port}"
external = "${lookup(var.external_port)}"
}
}
terraform init
terraform validate
Then we need to generate and see
dev
execution plan:terraform plan -out=tfdevelopment_plan -var env=development
Here are some useful flags for
plan
:-out=path
: Writes a plan file to the given path. This can be used as input to the apply
command.-var foo=bar
: This sets a variable in the Terraform configuration. It can be set multiple times.If everything looks good you can build (or change)
dev
infrastructure according to the plan:terraform apply tfdevelopment_plan
-auto-approve
: This skips interactive approval of plan before applying.-var foo=bar
: This sets a variable in the Terraform configuration. It can be set multiple times.Confirm your apply by typing
yes
. The apply
will take a bit to complete.Now you can list the Docker images or inspect Terraform state or plan by running following commands:docker images ls
terraform show
docker container ls
This article was originally published on .