How to create a Ghost Blog Terraform Module

How to create a Ghost Blog Terraform Module
Techiio-author
Written by Abhishek JalanApril 18, 2020
4 min read
DevOps
7 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 7 VIEWS SHARE
Techiio-author
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
7 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 7 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Abhishek Jalan
DevSecOps Engineer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+60 more
19 Blog Posts
9 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance