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
5 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 5 VIEWS SHARE
Abhishek Jalan

DevSecOps 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
5 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 5 VIEWS SHARE
Was this blog helpful?
You must be Logged in to comment
Code Block
Abhishek Jalan
DevSecOps Engineer
+21 more
17 Blog Posts
4 Discussion Threads
Trending Categories
93
Software12
DevOps34
Frontend Development13
Backend Development13
Server Administration13
Linux Administration12
Data Center14
Sentry11
Terraform15
Ansible9
Docker13
Penetration Testing12
Kubernetes12
NGINX8
JenkinsX10
Jenkins18
SSL6
Ethical-Hacking10
Python8
NodeJs9
RedHat8
Github11
AngularJs15
Google Cloud Platform (GCP)6
SonarQube9
Amazon Web Service (AWS)2
VMware17
Blockchain6
Snipe-IT7