How to create a Ghost Blog Terraform Module

How to create a Ghost Blog Terraform Module
Written by Abhishek JalanApril 18, 2020
4 min read
DevOps
4 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 4 VIEWS SHARE
Abhishek Jalan

Founder of Wikiance, DevOps Engineer

Let's create Ghost Blog using Terraform Modules on CentOS

Create the ghost module

Create the ghost directory:

mkdir ghost
cd ghost

Create main.tf:

vi main.tf
main.tf:
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}"
  }
}

Create variables.tf:

vi variables.tf
variables.tf:
#Define variables
variable "image_name" {}
variable "container_name" {}
variable "ext_port" {}

Create outputs.tf:

vi outputs.tf
outputs.tf:
#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}"
}

Create the root module

Create main.tf: Change back to the cloud_user directory.

cd ~/
vi main.tf
main.tf:
module "ghost" {
  source             = "./ghost"
  image_name         = "${var.image_name}"
  container_name     = "${var.container_name}"
  ext_port           = "${var.ext_port}"
}

Create variables.tf:

vi variables.tf
variables.tf:
#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"
}
Create outputs.tf:
vi outputs.tf
outputs.tf:
#Output the IP Address of the Container
output "ip" {
  value = "${module.ghost.ip}"
}
output "container_name" {
  value = "${module.ghost.container_name}"
}

Deploy the infrastructure

Initialize Terraform

terraform init

Generate a Terraform plan:

terraform plan -out=tfplan -var image_name=ghost:alpine -var ext_port=8080
terraform apply tfplan

terraform
devops
centos
4 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 4 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block
Abhishek Jalan
Founder of Wikiance, DevOps Engineer
14 Blog Posts
4 Discussion Threads
Trending Categories
37
Software1
DevOps15
Frontend Development4
Backend Development2
Server Administration1
Linux Administration5
Data Center0
Sentry0
Terraform2
Ansible0
Docker3
Penetration Testing0
Kubernetes0
NGINX0
JenkinsX0
Jenkins2
SSL0
Ethical-Hacking1
Python0
NodeJs1
RedHat0
Github0
AngularJs0
Google Cloud Platform (GCP)0
SonarQube0
Amazon Web Service (AWS)0
VMware0
Blockchain0
Snipe-IT1
MongoDB0