Putting all things together using AWS Task Definition

This article is the continuation of WordPress with Docker, AWS (ECS, Code Pipeline, Load Balancer, RDS, EFS) Complete Series. For better understating, please start from the beginning of the series.

In the previous article, We have created the ECS Task Definition which has our Docker Image container along with environment variables and we also have mounted EFS volumes for plugins and cache.

In this article, our website will go live with Load Balancer and Autoscalling groups (having multiple WordPress instances on demand). This means that AWS can increase the number of WordPress instances on demand and it can scale it down too.

We will do the following in this article:

  • Create ECS Cluster
  • Create Service for WordPress Task Definition
  • Configure Autoscalling

Create ECS Cluster

Go to ECS from your AWS console service tab and select Clusters, Click on Create Cluster

Step 1: Select Cluster template

Select EC2 Linux + Networking

Step 2: Configure cluster

I have used m5a.large instance size. You can choose whatever instance size is suitable for you.

Please select the instance Security Group we created earlier. This cluster will create an EC2 instance for us. We will place our WordPress Docker image on this instance.

Hit Create button and it will create the Cluster for you.

You should be able to see your cluster in the list.

Add Service to your Cluster

Click on the cluster your have created and click on Create

Adding Load Balancer

Select your wordpress Load Balancer created earlier.

Click on Add to load balancer button

Select the target group created for the Load Balancer.

Un-select the Enable service discovery integration

Configure Autoscaling Group

You can change these variables as per your requirements. In our case, it will create two EC2 instances for us and place our WordPress website on each of them.

Creating Scale UP and Scale Down policies.

Something has to trigger an alarm so AWS EC2 can add or reduce number of instances for us. I am using CPU Utilization in our example. There are many more options.

To keep it simple, we are saying that if the CPU Utilization is above 60% for 5 minutes, add another instance. If the CPU Utilization is below 50% for 5 minutes, reduce instances to just 1.

Service and Task Tabs

Now if you go back to your Cluster, You should be able to see that the new service and task has been added.

Sucess !!!!

if everything goes well which I hope it will, Copy and past your Load Balancer DNS URL in the browser and you should see your WordPress website setup screen.

If, for some reason, things don’t go well and you don’t see this page. Please check how to Trouble shoot the problem below.

Troubleshooting

If something has gone wrong, your task will fail again and again. in order to find out what is wrong, click on the task and it will take you to a screen like this:

If something went wrong inside your container, you can investigate it inside the Logs tab.

Otherwise expand the web container under the Details tab of the task and you will see Exit code and reason why the task has failed.

Written by

14 thoughts on “Putting all things together using AWS Task Definition

  1. Hey man, I replied to the slack message. This has been a great series, keep it up! I believe troubleshoot is one word. πŸ™‚

      1. Hello, This has been a phenomemal series. Any chance you might add the ‘missing’ two posts listed on the start of the series?

        Most interested in the AWS Codepipeline you listed….

        Thanks again! πŸ™‚

  2. Great write up! I feel like I learned a billion things working through this. I can’t figure out one last problem though. When I load the ALB DNS record I get an error that I am redirected too many times.

    I’ve tried a few things but can’t get around it. Any suggestions?

    Thanks again for such a useful series.

    1. Hey Travis,

      Sorry for the late response. Are you using SSL certificates on ALB ?

      what if you give me a call on my skype: salmansohail1

  3. What about wp-content? Can that be put on EFS as well? My users upload directly to our WordPress instance, instead of using a CDN because the site isn’t that large.

  4. Hello Salman. Thank you for this amazing content.

    Unfortunately I wasn’t be able to make it work with ALB (SSL). Even though I can access fronted of WordPress but wp-login.php suck in loop and it doesn’t allow me to login to management console. Please let me know if you have a solution for this issue.

    1. Konstantin,

      I think I know what is the problem. If you have multiple instances, you should enable the stickiness to let’s say 24 hours on your ALB.

      WordPress session cookie cause this issue. I should have mentioned that in my post.

      I hope it will solve this issue for you.

  5. Hi Salmansohail, thank for your tutorial. I have completed in the final step, however no tasks found, and EC2 instance actually creating. I also checked ECS Instance tab but no EC2 instance in there. This is the error log in Event tab:
    service demo-service was unable to place a task because no container instance met all of its requirements. The closest matching container-instance 3a3d914441bf4f099b917098eb8fe7d7 doesn’t have the agent connected. For more information, see the Troubleshooting section.

    Please help me. Thanks,

    1. The first thing that occurs to me is your instances might be going to unhealthy state. Try to check your target group health check.
      Other than that, you can also check the cloud watch logs that may give you a hint too.

Leave a Reply

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