Serverless Containers - AWS Fargate

Ever since I learned about Docker containers, web application development has never been the same for me. Since then, I have been using Docker containers for every web application I develop. Docker containers have made my web applications “portable” in a sense - easy to set up, run, and share. Combining Docker containers with AWS, even makes it relatively easier to deploy. But I’m not really savvy with using AWS, so I was wondering if it was possible to use the convenience of Docker containers with the benefits of AWS in a way that I won’t have to think so much about managing AWS. That’s what led me to discover AWS Fargate.

I’ll be showing some simple steps on how to get started with AWS Fargate. But before that, there are some concepts that we need to get familiar with.

Containers

We usually develop software using a setup that closely resembles our target system. Our software may be developed on a specific OS with some software libraries and other dependencies installed. If we need to set up our software on other machines, we would have to ensure we set it up in exactly the same way. Containerization solves this by packaging your software, along with its libraries and the OS dependencies needed to run your application, into an executable called a container. When you deploy or share these containers, you are assured that every machine will have all the dependencies it needs to run the container.

Task Definitions

A task is simply something to do. A task definition is the description of what to do and what you need to do it. In setting up AWS Fargate we would need to create a task definition to tell AWS how to run our container.

AWC ECS

ECS or Elastic Container Service is a service for managing Docker containers. With ECS you can scale, run, deploy, or manage clusters and resources for your containers. We need ECS to run AWS Fargate. I won’t be going into detail about ECS here since our goal is to get our container running with very minimal AWS configuration.

AWC Fargate

AWS Fargate is a service that allows you to run your containers in a “serverless” way. Fargate takes away the complexities of managing your containers and lets AWS manage your containers for you. Fargate will handle the resource provisioning, scaling, and configuration of your application, all you’ll have to think about is your container. With Fargate you can focus more on developing your application and less on your deployment configuration.

How to Deploy a Container with AWS Fargate

I highly recommend that you read about the features of ECS and explore the various options that you have available for the service. For this guide we will follow the simplest method for getting your container running with AWS Fargate. I will not be covering how to make a Docker container since the goal here is to introduce AWS Fargate. I do recommend reading the official Docker documentation for more details.

  1. Login to your AWS Account.
  2. Navigate to the AWS ECS console. AWS ECS Navigation
  3. Select Clusters under Amazon ECS then click on the “Get Started” button. The next page should show you a diagram of ECS objects and how they are related. AWS ECS Button
  4. Configure Container Definition. Select the image you want to use with your container. If you already have a container registered in Dockerhub that you would like to use, then select the “Configure” button for the custom option. AWS ECS container custom Then fill in the values for the Standard configuration. There are a couple of required fields, the “Container name” could be any name you want to identify your container, the “Image” field should be the image tag on Dockerhub (example php:7.4-apache). If your container is a web app make sure to add port 80 to allow public access to your app. AWS ECS container definition Click the Update button of the Edit container screen when you’re done with the Container Definition.
  5. Verify that the Task Definition has compatibilities set to Fargate, then click “Next”.
  6. Configure your service. On the “Define your service” screen you may enable the load balancer if you want. But for this guide we can just leave the default options as they are. Click “Next” when you’re done. AWS ECS load balancer
  7. Configure Clusters. On the “Configure your cluster” page you can only change the cluster name but for now we’ll leave it as default. Click “Next” when you’re done.
  8. Review configurations. The Review page allows you to verify all the configurations and gives you a chance to make corrections or update any configuration settings. When you are satisfied with the configuration you may click “Create”.
  9. Wait for the creation to complete. The creation page should show something like this: AWS ECS creation When all the items are completed the “View Service” button should be enabled.
  10. Verify your service is running. Click on the “View Service” button and on the succeeding page select the “Tasks” tab. AWS ECS Service tasks You should see your task definition listed with a status of RUNNING. AWS ECS running task
  11. Verify your application can be publicly accessed. If your container is a web application, you can verify access via a web browser. To get the IP address to access do the following:
    • Click on the running task. AWS ECS running task
    • Under the Network section click on the ENI Id link. AWS ECS ENI This should lead to the EC2 page where you can view the details of your network interfaces including the public IP address.
    • Copy the “Public IPv4 address” value to your browser and verify that your application is accessible.

If you are able to see your landing page (if you used your own docker container) or the sample page of the Amazon app (if you used the sample container) then congratulations! You have just deployed a container with AWS Fargate.

Cleaning Up

To clean up your AWS Fargate deployment, simply delete your cluster on the AWS ECS console.

  1. Navigate to AWS ECS.
  2. Click on Clusters, in the left navigation menu.
  3. On the Clusters page, select the cluster to delete.
  4. Click on the “Delete Cluster” button and follow the confirmation prompts. AWS ECS delete

Deleting the cluster cleans up the associated resources that were created with the cluster, including Auto Scaling groups, VPCs, or load balancers.

For more information on AWS Fargate you can read the official documentation here.