Ultimate Guide To Using Terraform With AWS Lightsail For Cloud Management

This is a simple project to create and deploy a new WordPress LightSail web server using Terraform. You can refer to the full code at the GitHub repository

What is Lightsail?

Amazon LightSail is a simplified cloud service provided by Amazon Web Services (AWS). It offers an easy way to launch and manage virtual private servers (VPS), databases, and other resources. LightSail offers predictable pricing and a user-friendly interface, making it an ideal choice for those new to cloud services

What is Terraform?

Terraform is an open-source tool developed by HashiCorp for building, changing, and versioning infrastructure safely and efficiently. It uses a declarative configuration language to define infrastructure as code (IaC), allowing you to manage cloud and on-premises resources in a consistent and repeatable manner. Terraform is widely used for its ability to automate infrastructure provisioning and management.

Read more: Ultimate Guide To Using Terraform With AWS Lightsail For Cloud Management

The deployment process

Before we dive into the deployment process, ensure that Terraform and AWS CLI are installed in your environment. This guide does not cover the installation steps for these tools.

We have created the following Terraform files for this deployment.

main.tf

First, we need to define the provider, which in this case is AWS. We also specify the region where our resources will be created, which is the us-east-1 AWS region.

terraform {
  required_providers {
    aws = {
        source = "hashicorp/aws"
        version = "4.14.0"
    }
  }
}
provider "aws" {
  region = "us-east-1"
}

Next, we create a static IP for the Lightsail server and the Lightsail server itself. The static IP creation involves two steps: creating the static IP and associating it with the Lightsail instance.

#Create a static IP address for the Lightsail instance

resource "aws_lightsail_static_ip" "static_ip" {
  name = "Wordpress-IP"
}

#Attach  the IP address with the Lightsail instance. 
resource "aws_lightsail_static_ip_attachment" "static_ip_attachment" {
  instance_name = "Wordpress-blog"
  static_ip_name = "Wordpress-IP"
}

For the Lightsail server, we need to configure the following minimum features:

  • Instance Name: The name of the instance.
  • Availability Zone: which indicates where the resource will be created.
  • Blueprint ID: A template to use, such as WordPress, Joomla, Magento, etc. In our case, we are using WordPress.
  • Bundle ID: The performance specification, which corresponds to the size of the Lightsail server.

There are many other features you can configure, but they are not covered here.

This is a basic way to create a Lightsail server instance. In subsequent projects, we will cover more advanced features and methods, including using Terraform variables, DNS configuration, instance firewall ports management, and more.

resource "aws_lightsail_instance" "wordpress_instance" {
  name = "Wordpress-blog"
  blueprint_id = "wordpress"
  availability_zone = "us-east-1a"
  bundle_id = "micro_3_0"
  tags = {
    Environment = "My Personal Blog"
    
  }
}

output.tf

The code below output the instance static IP address after successfull deployment of the lightsail server. This would allow us to know which IP to use to access our website.

output "ip" {
  value = aws_lightsail_instance.wordpress_instance.public_ip_address
}

Thank you for following along!



Leave a Reply

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