How to deploy WordPress on AWS Lightsail with terraform

Anthony Snk
4 min readJun 5, 2021

--

This is my first post, so I would like to write something related to AWS and Terraform, in this post, we will work to deploy a WordPress with Lightsail.

Install Terraform

First of all, we have to install terraform on out computer if you are using Linux you can use this script to install Terraform v.0.15.

If you are using Windows a recommend you install chocolatey and run the next command

choco install terraform

Run terraform version If you don't get an error everything is OK.

Create IAM user

We have to have Acces key and Secret Key from AWS, So I will create one for this example.
Go to IAM services, click on add user

Give a name for your new user, remember check programmatic access, click on next.

We can add a tag for this user, this part is optional after a click on next and after a click on create a user.

Don't forget to download CSV file with keys, remember don't share with someone else this access key, don't public on GitHub too.

Now we have to have installed CLI of AWS. Set up your AWS CLI to connect to AWS how-to-install-CLI-AWS. After installing CLI run the next command, where tf-tutorial is a new symbolic you can put any name that you want, this profile we will use with terraform.

aws configure --profile tf-tutorial

Remember don't share the keys with anyone, so I will delete this key after I complete this post. NOTE: write table with lowercase.

So we are ready to start with terraform files.

provider.tf

We are using the profile that we created before this means we don't have to put our key on our terraform files, also we put default_tags (apply for all resources), we will use region with variables.

inputs.tf

This file is used to set up the variables that the main.tf needs to build of resources, when we are using default statement is not necessary put a value on terraform.tfvars

main.tf

This file contains our resources to build a lightsail, We are creating 3 resources:

  • Lightsail instance
  • Static EIP
  • Attach EIP to instances

outputs.tf

In this file we can get attributes outputs on our console, the important item here is the value, we have to call resource.name.attribute

terraform.tfvars

The file terraform.tfvars is very important because if we set up variables on the variable.tf we have given a value for these variables, so in this file, we are doing.

Terraform init

We have to download providers modules and start our project so go ahead with the next command.

terraform init

Terraform plan

If we want to see the resources before building the infrastructure we can with terraform plan.

terraform plan

Terraform apply

Now we can continue with the process to build our WordPress so execute the next command.

terrraform apply --auto-approve

Go to console in Lightsail, as we can see terraform created for us new instances with EIP, if we want so visit this EIP we will get our WordPress site.

How to get into WordPress dashboard.

On the Lightsail home page, choose the quick connect icon for the instance you want to connect to.

The browser-based SSH client window opens, as shown in the following example.

Type the following command to retrieve the default application password:

cat bitnami_application_password

NOTE: If you’re in a directory other than the user home directory, then type cat $HOME/bitnami_application_password.

You should see a response similar to this, which contains the application password:

Destroy infraestructure

If we want to destroy our test to don't get cost go ahead with the next command, this will delete all resources created

terraform destroy -auto-approve

Thanks for reading, I hope this post can help someone.

--

--