5-Minute Guide to Deploying Ghost CMS on AWS with Docker, Terraform, and Ansible
I've had my eye on Ghost CMS for a long time. What interests me most about it is its minimal but sleek approach and its great design templates. I would love to see more free templates, but the ones available now are fantastic.
In this article, we'll provision an AWS EC2 instance, install Docker on it, and run a Ghost container. To maintain data persistence, we'll mount it to a volume.
Prerequisites
- AWS account
- Install AWS CLI
- Install Terraform
- Install Ansible
- SSH key for accessing the EC2 instance
Setup and configuration
First, we need to clone the repository and create an SSH key to log into our EC2 instance.
git clone https://github.com/devops-kaizen/aws-ec2-ghost
mkdir aws-ec2-ghost/auth
ssh-keygen -t rsa -b 4096 -f aws-ec2-ghost/auth/ghost_server_key
- git clone: Clones the repository containing the Terraform and Ansible configurations for deploying Ghost CMS.
- mkdir: Creates a directory to store the SSH key that will be used to access the EC2 instance.
- ssh-keygen: Generates a new SSH key pair for secure access to the EC2 instance.
Next, configure AWS CLI for authentication:
# AWS authentication
aws configure # enter Access key ID & Secret access key
aws configure: Sets up the AWS CLI with your AWS account credentials, allowing Terraform to interact with AWS services.
Deployment
Provision Infrastructure
Navigate to the Terraform directory and initialize, plan, and apply the configuration to provision the EC2 instance.
# WORKDIR : aws-ec2-ghost/terraform/
cd aws-ec2-ghost/terraform/
terraform init
terraform plan
terraform apply
- cd: Changes the directory to where the Terraform configuration files are located.
- terraform init: Initializes the Terraform environment, downloading necessary plugins.
- terraform plan: Creates an execution plan, showing what actions Terraform will take to provision the resources.
- terraform apply: Provisions the infrastructure as defined in the Terraform configuration files, creating an EC2 instance.
Copy the IP address of the newly created EC2 instance.
Ansible Setup
In the Ansible inventory file, paste the IP address and provide the correct path to the SSH key.
# WORKDIR : aws-ec2-ghost/ansible
cd aws-ec2-ghost/ansible
ansible-playbook -i inventory deploy-docker.yml
ansible-playbook -i inventory deploy-ghost.yml
- cd: Changes the directory to where the Ansible playbooks are located.
- ansible-playbook -i inventory deploy-docker.yml: Runs the Ansible playbook to install Docker on the newly created EC2 instance. The
-i inventory
option specifies the inventory file where the EC2 instance's IP address is listed. - ansible-playbook -i inventory deploy-ghost.yml: Runs the Ansible playbook to deploy the Ghost CMS container on the EC2 instance.
Backup & Restore
One of the magical aspects of Docker architecture is the ease of backups and restores due to its decoupled nature. Set up the correct paths in the script and run the following commands:
# WORKDIR : aws-ec2-ghost/scripts
cd aws-ec2-ghost/scripts
bash create-transfer-ghost-backup.sh # creates ghost-backup.tar.gz
bash restore-ghost-backup.sh /path/to/ghost-backup.tar.gz
- cd: Changes the directory to where the backup scripts are located.
- create-transfer-ghost-backup.sh: This script creates a backup of the Ghost CMS data and compresses it into a file named
ghost-backup.tar.gz
. It also gives an option to send the backup file to a remote server usingscp
. - restore-ghost-backup.sh /path/to/ghost-backup.tar.gz: This script restores the Ghost CMS data from the backup file. You need to provide the path to the backup file as an argument.
Wrapping Up
Whether you're a DevOps veteran or new to cloud deployments, this guide helps you get started effortlessly. This approach ensures a reproducible and automated deployment process, leveraging the power of Infrastructure as Code (IaC) and configuration management tools.
For the complete code and configurations, visit the aws-ec2-ghost GitHub repository.
Try it out and experience the elegance and functionality of Ghost CMS. Happy blogging!