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

20 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. I’ve followed your tutorial but I’m getting an error ” caught SIGWINCH, shutting down gracefully”

    This leads to the container semi-randomly reseting. I can see the install wordpress page for a few seconds and then the container just restart

  3. 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

  4. 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.

  5. 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.

      1. Hi Salman, I Just came here to thank you, I readed all your previous articles about wordpress with ecs.
        I have a question about ALB stickiness, some people said to me that will be better to use some kind of in memory database, like Redis, to handle php sessions.
        Well, I never worked with redis, but do you think that is a good decision to make? Can you point me some articles about how to setup redis to work with wordpress + ECS + ALB?

        1. Hello Rafael,

          Thanks for liking my articles. I would say using Redis will be an overkill for a wordpress website. This is purely based on my experience when I used Redis + wordpress.

          Redis cache can be bloody expensive on AWS too. I would take another approach to speed up the website, I will use a caching plugin such as “WP Fastest cache” or “WP total cache” etc. I have taken Redis approach first but when I tried the caching plugins. It was much better. Since the caching plugin also uses physical file system like plugins, I will recommend that you create another volume in the same EFS drive for cached files or write a script that will move all your cached files to CDN. If you’re using S3 for CDN, I have that script.

  6. 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.

  7. Hi Salmansohail,

    Firstly thnaks for this great content, I’m trying to deploy my wordpress site following every single step, but the task get stopped when trys to instaciate the docker image, this is what I see STOPPED (DockerTimeoutError: Could not transition to create),

    may be is too late for my comment bu t I can’t solve this issue,

    thanks a lot

  8. Hello!
    Thanks for the answer! I already use cache plugins, but about the Redis what I meant was to use it for handle php sessions between the ec2 instances on cluster.

    Doing this I can disable the “sticky session” on ALB.
    I’m totally ok with sticky sessions on ALB, but when I asked someone else, they told me that maybe sticky sessions can be a bottle neck on high traffic. So these people suggest me to look for shared php sessions with redis.

    Look this: https://github.com/ethitter/WP-Redis-User-Session-Storage
    or this: https://easyengine.io/tutorials/php/redis-php-sessions/
    I also heard about of memcached to do the same job: https://www.digitalocean.com/community/tutorials/how-to-share-php-sessions-on-multiple-memcached-servers-on-ubuntu-14-04

    Do you think that this approach makes sense?

    About S3 Buckets with Cloudfront for the CDN I want to use it. Today my CDN is Cloudflare, but the free plan only, and I’m thinking to move on, leave Cloudflare and use S3+Cloudfront as CDN. Your script will be much appreciated, if you can and want to share πŸ™‚

    1. Your concern regarding the bottle neck is correct if you will have huge traffic. My only concern with the AWS redis cache is the cost. I believe it is over priced.

      However, you can create an ec2 instance and install the redis server over there I guess. I have made some websites with series traffic in the past, so far I did not need to use redis session sharing.

Leave a Reply

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