This article will introduce you to a beginner project, that will teach you how to deploy your own blog on WordPress using AWS LightSail using Terraform.

repository

Intro

If you’re like many tech enthusiasts, you’re likely constantly engaging in cloud projects to enhance your skills and bolster your CV. One such project that emerged from my own need for organization was the creation of a personal blog. As my local filesystem became cluttered with project documentation, I realized the need for a centralized platform to track my progress. Thus, I embarked on building a blog infrastructure on AWS LightSail, and I’m here to guide you through the process.

Pre-requisites.

Before diving into the setup process, there are a few pre-requisites you’ll need to have in place:

  • Domain Name: If you plan to use Route53, ensure you have a hosted zone purchased from the Amazon directory or another third-party domain provider.
  • SSL/TLS Certificate: While our module will create a certificate within LightSail for traffic security, you can also opt to create your own certificate in AWS Certificate Manager (ACM) or use certificates from other third-party providers.

Step 1:
The first step is to configure your domain, which involves creating a domain for your blog and configuring DNS records. Below is a Terraform code snippet demonstrating this configuration:

resource "aws_lightsail_domain" "lightsail_domain" {

  domain_name = var.domain_name

}

resource "aws_lightsail_domain_entry" "lightsail_domain_entry" {

  domain_name = aws_lightsail_domain.lightsail_domain.domain_name

  name        = var.subdomain

  type        = var.record_type

  target      = local.local_host

}


In this snippet, variables like domain_name, subdomain, and record_type are utilized to customize the domain setup according to your needs. (see variables.tf below)


Step 2: Creating a Certificate

Next, you’ll need to create a certificate within LightSail to secure your traffic. This certificate ensures encrypted communication between your users and the blog.

Here’s how you can do it with Terraform:

resource "aws_lightsail_certificate" "lightsail_cert" {

  name        = var.cert_name

  domain_name = var.domain_name

}

Step 3: Provisioning LightSail Compute and Elastic IP

Now, let’s set up the LightSail compute instance and allocate an elastic IP address:

resource "aws_lightsail_instance" "lightsail_instance" {

  blueprint_id      = var.app_blueprint

  bundle_id         = var.bundle

  name              = var.instance_name

  availability_zone = var.az

}

resource "aws_lightsail_static_ip" "lightsail_sip" {

  name = var.static_ip_name

}

resource "aws_lightsail_static_ip_attachment" "lightsail_static_ip_attachment" {

  static_ip_name = aws_lightsail_static_ip.lightsail_sip.id

  instance_name  = aws_lightsail_instance.lightsail_instance.id

}

resource "aws_lightsail_instance_public_ports" "lightsail_ports" {

  instance_name = aws_lightsail_instance.lightsail_instance.id

  port_info {

    protocol  = "tcp"

    from_port = var.http_from_port

    to_port   = var.http_to_port

  }

  port_info {

    protocol  = "tcp"

    from_port = var.https_from_port

    to_port   = var.https_to_port

  }

}

These variable values (var,) are primarily defaulted in our variables.tf file (shown below)

Step 4: Setting Up Distribution

To enable secure website traffic, we create a distribution and attach our certificate:

resource "aws_lightsail_distribution" "wordpress_distro" {

  name             = var.distro_name

  depends_on       = [aws_lightsail_static_ip_attachment.lightsail_static_ip_attachment]

  bundle_id        = var.distro_bundle

  certificate_name = aws_lightsail_certificate.lightsail_cert.id

  origin {

    name        = aws_lightsail_instance.lightsail_instance.name

    region_name = var.region

  }

  default_cache_behavior {

    behavior = var.cache_behavior

  }

}

This distribution ensures that our blog content is securely delivered to users.

Step 5: Finally, we create an output to store the elastic IP address in our state file:

output "eip_address" {

  value = aws_lightsail_static_ip.lightsail_sip.ip_address

}

This output captures the IP address for future reference.

Step 6: Injecting Variables

In the last step, we call our module and inject the necessary variables:

module "lightsail" {

  source = "./module"

  domain_name    = "monkeyswearpurplepajamas.com"

  record_type    = "A" # A if associated with an AWS resource or CNAME if your domain provider is third party. i.e. GoDaddy.

  subdomain      = "www"   # i.e. www if you want to create a www.example.com record.

  static_ip_name = "test-eip"

  cert_name      = "testcert"

  instance_name  = "test-machine"

  domain_host    = module.lightsail.eip_address

  cache_behavior = "dont-cache"

}


Here, we provide specific values for variables like domain_name, subdomain, and others, tailoring the setup to our requirements.

By following these steps and customizing the provided variables, you can swiftly deploy your own blog infrastructure on AWS LightSail, streamlining your project management and enhancing your cloud skills. Happy blogging!

For Reference:

Variables.tf

Leave a Reply

Your email address will not be published. Required fields are marked *