Building a WordPress Site on AWS LightSail

This blog post will guide you through deploying a WordPress instance on AWS LightSail using Infrastructure as code.

Table of Contents

  • AWS Account
  • AWS CLI Configured 
  • Terraform installed

Terraform

Create a directory for your Terraform configuration files. Inside this directory, you will create 3 files:

  1. main.tf: This is the primary configuration file where you will define the AWS Lightsail instance for WordPress.
  2. variables.tf: This file will contain the declarations for variables.
  3. outputs.tf: Here, you will define outputs that you want to see after Terraform has applied your configuration, such as the public IP of the Lightsail instance.

Note: Terraform best practice is to modularise the code, we won’t be doing that as there’s not many parts to this deployment. 

Variables.tf

Copy the below into your variables.tf file. 

setting  your provider block to your local AWS region is normally a good idea, but my local region is ap-southeast-2 which doesn’t support all the API calls required in this Terraform config. Changing my region to us-east-1 resolved that issue for me. 

Replacing the default  variable in “domain_name” with the name of your domain. 


provider "aws" {
  region = "us-east-1"
}

variable "domain_name" {
  description = "The domain name for the WordPress site"
  default     = "cloud-corner.com"
}

variable "instance_name" {
  description = "The name of the Lightsail instance"
  default     = "WordPressInstance"
}

variable "blueprint_id" {
  description = "The blueprint ID for WordPress"
  default     = "wordpress" 
}

The blueprint_id variable is found by running the following command in your terminal. It will return two options: we’re interested in the one highlighted in green, as the pink one is for a multi-site WordPress installation.
aws lightsail get-blueprints --query "blueprints[?contains(name,'WordPress')]"
Execute this command to identify the correct blueprint_id for a standard WordPress installation.

Main.tf

Lets walk through creating  each resource, starting with the WordPress instance.

  • You’ll need to set a bundle ID in your Instance config, you can find this via the code snippet below.
aws lightsail get-bundles --region us-east-1

You’ll also need to update the availability zone where you want the instance deployed. 

resource "aws_lightsail_instance" "wordpress_instance" {
  name              = var.instance_name
  availability_zone = "us-east-1a"
  blueprint_id      = var.blueprint_id
  bundle_id         = "nano_3_0"
}
Creating the static IP.
resource "aws_lightsail_static_ip" "static_ip" {
  name = "${var.instance_name}_ip"
}
Attaching the static IP to the instance.
resource "aws_lightsail_static_ip_attachment" "ip_attachment" {
  static_ip_name = aws_lightsail_static_ip.static_ip.name
  instance_name  = aws_lightsail_instance.wordpress_instance.name
}
Creating the DNS Zone
resource "aws_route53_zone" "primary" {
  name = var.domain_name
}
Creating the DNS Record within the DNS Zone – Here we add www.domain_name.com
resource "aws_route53_record" "www_DNS" {
  zone_id = aws_route53_zone.primary.zone_id
  name    = "www.${var.domain_name}"
  type    = "A"
  ttl     = "300"
  records = [aws_lightsail_static_ip.static_ip.ip_address]
}
and here we add in domain_name.com
resource "aws_lightsail_domain_entry" "www-Cloud-Corner" {
  domain_name = aws_lightsail_domain.domain_entry.domain_name
  name        = ""
  type        = "A"
  target      = aws_lightsail_static_ip.static_ip.ip_address
}
Create the domain entry
resource "aws_lightsail_domain" "domain_entry" {
  domain_name = "cloud-corner.com"
}
Point the DNS record at the static IP we created earlier. the DNS record here being “www.YourDomain.com”
resource "aws_lightsail_domain_entry" "www-Cloud-Corner" {
  domain_name = aws_lightsail_domain.domain_entry.domain_name
  name        = "www"
  type        = "A"
  target      = aws_lightsail_static_ip.static_ip.ip_address
}

Point the DNS record at the static IP we created earlier. the DNS record here being “YourDomain.com”

resource "aws_lightsail_domain_entry" "Cloud-Corner" {
  domain_name = aws_lightsail_domain.domain_entry.domain_name
  name        = "www"
  type        = "A"
  target      = aws_lightsail_static_ip.static_ip.ip_address
}

Defining what’s allowed through the firewall.

resource "aws_lightsail_instance_public_ports" "public_ports" {
  instance_name = aws_lightsail_instance.wordpress_instance.name

  port_info {
    protocol  = "tcp"
    from_port = 80
    to_port   = 80
  }

  port_info {
    protocol  = "tcp"
    from_port = 443
    to_port   = 443
  }

  port_info {
    protocol  = "tcp"
    from_port = 22
    to_port   = 22
  }
}

Applying the configuration.

Run the below commands to apply the config to your AWS LightSail account. 

Terraform init
Terraform validate
Terraform apply

Conclusion

After the apply has finished you should have all the resources setup to start to configure your WordPress site. 

There are some additional steps required that can’t be done via terraform, such as domain verification and getting an SSL certificate for your website.