Deploy a WordPress Amazon Lightsail instance using Terraform
Tags: aws, terraform, wordpress
Published: 2022-05-15 18:30:51


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] (https://www.techworld-with-nana.com):


[What is Terraform | Terraform by HashiCorp ]( https://www.terraform.io/intro)

[Terraform explained in 15 mins | Terraform Tutorial for Beginners] (https://www.youtube.com/watch?v=l5k1ai_GBDE)
<br>

**Pre-requisites:**


• AWS Account
• [Named profile configured](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-profiles.html)
• [Terraform must be installed]( https://learn.hashicorp.com/tutorials/terraform/install-cli?in=onboarding/tfcb-week-2)
• Visual Studio Code 
• [AWS Toolkit extension for VS Code]( https://marketplace.visualstudio.com/items?itemName=AmazonWebServices.aws-toolkit-vscode)
<br>
All the code in this blog can be found in the repo:

[Amazon-Lightsail-Terraform]( https://github.com/AdrianM10/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 on blueprints can be found [here]( https://awscli.amazonaws.com/v2/documentation/api/latest/reference/lightsail/get-blueprints.html).

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]( https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lightsail_instance)

[Regions and Availability Zones in Amazon Lightsail]( https://lightsail.aws.amazon.com/ls/docs/en_us/articles/understanding-regions-and-availability-zones-in-amazon-lightsail)