Creating a Ghost Blog Terraform Module

Hands-On Lab

 

Photo of Travis Thomsen

Travis Thomsen

Course Development Director in Content

Length

01:00:00

Difficulty

Intermediate

After helping your friend deploying their Ghost blog using Terraform, you’ve experienced an increase in demand for the product. To make deploy Ghost blog easier, you’ve decided to convert your existing Terraform files into a module.

What are Hands-On Labs?

Hands-On Labs are scenario-based learning environments where learners can practice without consequences. Don't compromise a system or waste money on expensive downloads. Practice real-world skills without the real-world risk, no assembly required.

Creating a Ghost Blog Terraform Module

Introduction

After helping your friend deploying their Ghost blog using Terraform, you’ve experienced an increase in demand for the product. To make deploy Ghost blog easier, you’ve decided to convert your existing Terraform files into a module.

Solution

  1. Begin by logging in to the lab server using the credentials provided on the hands-on lab page.

    ssh cloud_user@PUBLIC_IP_ADDRESS

Create the Ghost Module

  1. Create the ghost directory and transfer into it.

    mkdir ghost
    cd ghost/
  2. Create and edit main.tf.

    vi main.tf
  3. Add the following text to the file.

    # Download the Ghost Image
    resource "docker_image" "ghost_image" {
        name = "${var.image_name}"
    }
    
    # Start the Ghost Container
    resource "docker_container" "ghost_container" {
        name  = "${var.container_name}"
        image = "${docker_image.ghost_image.latest}"
        ports {
            internal = "2368"
            external = "${var.ext_port}"
        }
    }
  4. Save the changes and exit the editor.

  5. Create and edit variables.tf.

    vi variables.tf
  6. Add the following text to the file.

    #Define variables
    variable "image_name" {}
    variable "container_name" {}
    variable "ext_port" {}
  7. Save the changes and exit the editor.

  8. Create and edit outputs.tf.

    vi outputs.tf
  9. Add the following text to the file.

    #Output the IP Address of the Container
    output "ip" {
        value = "${docker_container.ghost_container.ip_address}"
    }
    
    output "container_name" {
        value = "${docker_container.ghost_container.name}"
    }
  1. Save the changes and exit the editor.

Create the Root Module

  1. Transfer to a higher directory.

    cd ../
  2. Create and edit main.tf.

    vi main.tf
  3. Add the following text to the file.

    
    module "ghost" {
        source          = "./ghost"
        image_name      = "${var.image_name}"
        container_name  = "${var.container_name}"
        ext_port        = "${var.ext_port}"
    }
  4. Save the changes and exit the editor.

  5. Create and edit variables.tf.

    vi variables.tf
  6. Add the following text to the file.

    #Define variables
    variable "image_name" {
        description = "Image for container."
        default     = "ghost:latest"
    }
    
    variable "container_name" {
        description = "Name of the container."
        default     = "blog"
    }
    
    variable "ext_port" {
        description = "External port for container."
        default     = "80"
    }
  7. Save the changes and exit the editor.

  8. Create and edit outputs.tf.

    vi outputs.tf
  9. Add the following text to the file.

    #Output the IP Address of the Container
    output "ip" {
        value = "${module.ghost.ip}"
    }
    
    output "container_name" {
        value = "${module.ghost.container_name}"
    }
  1. Save the changes and exit the editor.

Deploy the Infrastructure

  1. Initialize Terraform.

    terraform init
  2. Generate a Terraform plan.

    terraform plan -out=tfplan -var image_name=ghost:alpine -var ext_port=8080
  3. Deploy the infrastructure using the plan file.

    terraform apply tfplan
  4. Open a browser and navigate to the public IP address on port 8080.

    PUBLIC_IP_ADDRESS:8080
  5. Verify the ghost blog.

Conclusion

Congratulations — you've completed this hands-on lab!