5

How to Deploy a Full Environment for a Wordpress Site via Docker by Using Terraf...

 3 years ago
source link: https://dzone.com/articles/how-to-deploy-a-full-environment-for-wordpress-sit
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to Deploy a Full Environment for a Wordpress Site via Docker by Using Terraform

This tutorial will get you started with developing your first infrastructure in minutes!

Join the DZone community and get the full member experience.

Join For Free

14270488-cac46061-0344-4e29-a4ab-c3b67a80e10d.png

For many users, preparing an environment in AWS to host their website/system can be tough, especially if they’re new to cloud and what that actually means.

Also, after the preparation is done, the creation of resources might take a while, too! But what if you could do all of the following in just twenty minutes?

  • Create a VPC and all essentials for public and private subnets.
  • Create an EFS and prepare an access point to this storage option.
  • Create a launch template to replicate your configuration into multiple EC2s in different subnets.
  • Prepare your website as a Docker image to have a consistent replica of your website and its DB.

It feels impossible, right? It’s actually not — this is the beauty of IAC and Terraform. Why don’t we go deeper? 

Note: This is a POC script and it did get tested. Yes, it could be better but for the purposes of this article, it’s a good starting point.

What Will You Get?

Check the image below:

14270493-22c9af6c-bdea-4327-b53c-2ba0549be3fd.png

After you run the code as it is, you will get a load balancer placed in one of the public subnets, one EC2 machine that got deployed by the auto scaling launch template, and EFS.

These things are enough to run your website, at least for this use case. 

But what actually happened when I run the script is the following:

  • For VPC, the script will make a VPC, subnets (public and private), routing tables, and internet gateway.
  • For auto scaling, the script will make a launching template with what is needed to replicate the configuration into multiple instances as is required and will make sure that the minimum required instances are always meet the configuration.
  • For the load balancer, the script will prepare an endpoint associated with health check metrics.
  • For EC2, the script will launch an instance from the configuration script that got attached to the auto scaling launching template. This will install and set up Docker inside the machine and will pull the project from GitHub.
  • For EFS, the script will create all needed resources like security group, access point and access point attachment for this storage.

Is that too much? I guess not; some other systems needs a lot more. But let’s agree on one thing: Doing all of these things in 20 minutes manually is exhausting.

The Steps of Deployment

It is really easy and requires just a few clicks. Let’s get started!

Before we start, clone this link.

1. Do You Have Terraform Installed?

First things first: Check whether you have Terraform installed on your machine.

14270495-45a6f790-e33e-45b7-ad2b-af7b2e2e1dca.png

2. Do You Have Your AWS Credentials?

Because we are going to deploy these instructions to AWS, make sure one of these two options are available:

  • AWS CLI is configured, so you should only know which profile you will use if multiple configurations are placed.
  • AWS access and secret keys, because of course we need credentials to access.

Note: Check provider.tf once you decide which connection to use.

3. Some Necessary Commands

Open a terminal window and navigate into the project.

Because we are going to make a new instance, we might need to access the instance, so run these commands.

Shell
ssh-keygen -t rsa -b 4096 -C "[email protected]" -f $PWD/id_rsa

Note: Do not change the key name unless you go to vars.ft and change it there also.

Then, run this command to get all needed modules:

Shell
xxxxxxxxxx
terraform init

14270499-f57647f1-1d3e-496a-876a-bba28f23e663.png

4. Trigger the Script

There is only one step left, maybe two if you want to check what will happen.

To check what resources will be created, run this command:

Shell
xxxxxxxxxx
terraform plan

To bring all these planned resources to the life, run this command:

Shell
xxxxxxxxxx
terraform apply

fter all resources are alligned in the correct order, a message will ask you to approve these changes. Type yes.

14270503-b57b3a88-6ffe-4cfc-8e95-9358c5e7323e.png

And now, you can relax until the whole process is finished.

14270504-a5f6102b-ee0a-45f7-b253-25623dc57794.png

Note: After the Terraform script is done, please wait 5-10 minutes before checking the website. The infrastructure is done but the instance script is in progress. EFS mounting needs some time to be done.

To clean everything, run this command:

Shell
xxxxxxxxxx
terraform destroy

Important Points

Why EFS in this simple use case? Because I am using Docker to host the website and DB all together, and you have the auto scaling launch template, EFS should insure the consistency of your DB because the DB has a volume pointed into EFS. Any new instance will use EFS as volume.

At the end of the script execution, Terraform will return an ELB link and this will not work directly. Why? Because the initial Wordpress page is going to be /admin to finish all needed configuration. So, go to aws console -> EC2 -> your created instance and visit its public IP to finish the setup. Then all is going to be good.

Summary

I have no idea if this precess would be helpful for anyone, but since I know how to make it, so why not share it?

Creating a full basic environment in AWS has many funny turns, especially for newcomers to this platform. This script will provide a few important points to think about for now. Using Docker is really good option, using Terraform is not hard, and finally, IAC is something they need to work on.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK