Deploy a WordPress Amazon Lightsail instance using Terraform

Deploy a WordPress Amazon Lightsail instance using Terraform

Lately I’ve been learning about Terraform and getting into the habit of deploying infrastructure as code across different cloud service providers, it’s been a while since I found a use case to provision infrastructure on AWS.

For inspiration behind this blog post I took a trip down memory lane circa late 2021, when I was going through a WordPress learning phase I initially used Amazon Lightsail for WordPress hosting, for some reason I used to click through the GUI to provision an Amazon Lightsail instance.

I had previously looked at documentation around using CloudFormation and never bothered to pursue that avenue for Lightsail. With that in the past, this new found confidence and understanding in using infrastructure as code and in particular with Terraform it was time to solve this mini challenge of yester year.

HashiCorp Terraform is an infrastructure as code tool that lets you define both cloud and on-prem resources in human-readable configuration files that you can version, reuse, and share.


If you’re new to Terraform it might be worthwhile to read the official documentation or watch an awesome intro video by Nana Janashia :

What is Terraform | Terraform by HashiCorp
Terraform explained in 15 mins | Terraform Tutorial for Beginners

Pre-requisites:


• AWS Account
Named profile configured
Terraform must be installed
• Visual Studio Code
AWS Toolkit extension for VS Code


All the code in this blog can be found in the repo:

Amazon-Lightsail-Terraform

Once you have cloned the repo, you’ll need to run terraform init, this command is used to initialize the working directory that contains our Terraform configuration files.

I have included a variables.tf file to move away from as many hard-coded values in the main.tf file as possible.

This will allow versatility and makes the template reusable for different types of Lightsail blueprints eg. LAMP, Node.js, Joomla, GitLab etc

Official documentation from AWS can be found here.

In order to find the available blueprint IDs, type the below command in the AWS CLI:

aws lightsail get-blueprints

When you have entered your desired inputs in the variables.tf file, you can apply your configuration using terraform apply and the Amazon Lightsail instance will be provisioned.

Useful resources:

Resource: aws_lightsail_instance
Regions and Availability Zones in Amazon Lightsail
Author

Adrian Mudzwiti

Posted on

2022-05-15

Updated on

2022-05-15

Licensed under

Comments