<p>AWS Fargate, a fully managed compute engine for containers, provides a scalable, cost-efficient, and secure alternative to managing EC2 instances for AWS ECS clusters. Terraform is an Infrastructure-as-Code tool that allows you to define and provision cloud infrastructure resources using simple and declarative configuration files. By migrating to Fargate, businesses can save time and money while enabling development teams to focus solely on developing applications. This blog will explore how Terraform can automate your migration from EC2 to Fargate.</p>
<p><strong>What is AWS Fargate?</strong></p>
<p>AWS Fargate eliminates the need to manage servers or clusters and simplifies the process of building and deploying applications. Fargate supports Amazon ECS, Amazon EKS, and Amazon ECR to deploy container-based applications. It’s a serverless computing engine for containers without you having to provide an underlying EC2 instance for running containers. Serverless computing is an innovative cloud-based architecture that allows developers to build and run applications without managing infrastructure.</p>
<p>Fargate manages the underlying compute resources, allowing you to focus on building, running, and scaling applications. With Fargate, you can specify the CPU and memory resources needed for your tasks and applications. Fargate also supports auto-scaling, allowing applications to scale up or down based on demand.</p>
<div><img src="https://images.prismic.io/gspann/cf3a1079-3cff-436c-b918-cc87cefd0cbf_Image+1_+With+or+Without+Fargate.png?auto=compress,format"></div><br/>
<p>Running ECS with Fargate eliminates the need to configure and scale any underlying EC2 instances. You create a cluster, add packaged application containers, specify the resource CPU and memory requirements, define networking and IAM policies, and launch the application. Each Fargate task has its own isolated space and does not share the kernel, elastic network interface (ENI), CPU, or memory resources with other tasks.</p>
<div id="contentBlocker"></div>
<div><img src="https://images.prismic.io/gspann/a1c574fc-c56f-405d-999e-aa2a1b31db7d_Image+2_+General+Fargate+Architecture.png?auto=compress,format"></div><br/>
<p><strong>Make Your AWS Fargate Journey Easier with Automation</strong></p>
<p>Assuming that you use one of the popular infrastructure provisioning tools like Terraform, CloudFormation, or Pulumi to automate your ECS services deployment. In this blog, we guide you through the required changes in the migration script.</p>
<p>When you work with Terraform, implemented configurations are reusable and can be shared across various projects. An application inside a container can be launched, lifted, and managed quickly and easily using AWS Fargate and Terraform. You can write code that describes the desired state of your infrastructure and then apply that code to create the resources needed to automatically support your applications. This includes everything from EC2 instances to load balancers, security groups, and more. </p>
<p>Terraform provides several additional benefits to ease migration. For example, it allows you to apply version control to your infrastructure code, which makes it easier to collaborate on changes and roll back to previous versions if needed. Terraform also enables you to test changes to your infrastructure in a staging environment before deploying to production, so you can catch issues early and reduce the risk of downtime. It also integrates with other tools like AWS CodePipeline to create a streamlined, automated deployment pipeline that can further simplify the migration process. </p>
<p>Personally, I prefer Terraform for infrastructure automation. It is a powerful Infrastructure-as-Code (IaC) tool and the developers use it to gain prominence in the industry. Many learning materials are available online and the Terraform offers good community support.</p>
<p><strong>Getting Started</strong></p>
<p>Migrating your container from ECS to Fargate is relatively easy. First and foremost, modify some fields to say “FARGATE.” Now, let’s guide you through the required changes in a Terraform script for the migration.</p>
<ul>
<li><strong>Launch type</strong></li>
<p>To migrate Amazon ECS service to Fargate, you have two launch-type options: EC2 and Fargate. The default launch type is EC2; it offers greater control over your infrastructure servers. These servers are EC2 instances that you manage. You need to specify “Fargate” as the launch type. This field change allows you to deploy containerized applications to ECS without managing the underlying EC2 instances.</p>
<p>“requiresCompatibilities” is an optional parameter with its value set to “FARGATE.” It ensures that the Fargate task definition parameters are compatible with the specified launch type. If no value is specified, it defaults to “EC2.”</p>
<div><img src="https://images.prismic.io/gspann/e975bf34-3000-4e3a-8dba-07880c95f425_1.jpg?auto=compress,format"></div><br/>
<li><strong>Networking</strong></li>
<p>An important change when migrating to Fargate is the network configuration. The default value of the “networkMode” property is “bridge,” which is used by ECS. Fargate works only with “awsvpc” network mode. You must change the value to “awsvpc” in your task definition.</p>
<div><img src="https://images.prismic.io/gspann/11a7f8fc-526a-406c-bc94-d9c6d5601469_2.jpg?auto=compress,format"></div><br/>
<p>This means that every task has its own Elastic Network Interface (ENI). Tasks will also have their own IPv4 address from the deployed subnet. This address assignment allows multiple applications or copies of a single application to run on the same port number without conflicts. Also, you can assign different security groups to each task at the container level, which gives you more fine-grained security. You can assign an application just the minimum permissions it needs, greatly enhancing security.</p>
<li><strong>CPU and Memory settings</strong></li>
<p>The Fargate launch type CPU and memory are defined at the task level. This definition is typically the maximum amount of memory and CPU usage your task or container can consume. The values depend on each other, so check this <a href="https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size">link</a> for supported combinations. A memory allocation of at least 512MB or greater is required for each task. If you are unsure about CPU or memory requirements, start with a bigger size, watch your application usage in Cloudwatch, and adjust the size accordingly.</p>
<div><img src="https://images.prismic.io/gspann/e2f1c106-3a6f-42ee-b43b-12f3105980b4_3.jpg?auto=compress,format"></div><br/>
<li><strong>IAM roles and Permissions</strong></li>
<p>Check if your ECS instance uses a service-linked role named “AWSServiceRoleForECS.” This service usually already exists in your AWS account and might end up attached to your ECS service. Please remove it, as it is not needed anymore.</p>
<p>With a Fargate launch type task, your need to specify the execution role in the task definition. The role labeled “execution_role_arn” is what you need to launch the task by Fargate. Amazon ECS provides the managed policy named “AmazonECSTaskExecutionRolePolicy” which contains the set of permissions required to interact with other AWS services, such as Amazon ECR and CloudWatch, among others.</p>
<p>Here’s the updated terraform block with changes highlighted in green:</p>
<div><img src="https://images.prismic.io/gspann/6f4df97c-529b-449e-b462-ebc8c4b90c4b_4_1.jpg?auto=compress,format"></div><br/>
<div><img src="https://images.prismic.io/gspann/3c9c58e2-50a6-4d2e-8783-4ef8c913c9cb_4_2.jpg?auto=compress,format"></div><br/>
<p>To use the AWS managed ECS task execution role you would do something like this:</p>
<div><img src="https://images.prismic.io/gspann/7d7f171c-e324-49b8-b857-18fd883ae59e_4_3.jpg?auto=compress,format"></div><br/>
<li><strong>Load balancer’s Target Type</strong></li>
<p>The default target type of the load balancer “TargetGroup” is instance. You must change the “TargetType” to “IP” when deploying to Fargate. Also, verify that the port of the “TargetGroup” is now the same as your container port.</p>
<p>The updated terraform block with changes highlighted in green:</p>
<div><img src="https://images.prismic.io/gspann/6bb3e809-9c95-422e-bb0b-01d3c6f6cc53_5.jpg?auto=compress,format"></div><br/>
<li><strong>Logging</strong></li>
<p>If you’re using the Fargate launch type for your tasks, the tasks cannot talk to any other container or process on the same host while using a third-party tool to handle logging or metrics. An example of this type of tool is DataDog, a platform for IT infrastructure and application performance monitoring. </p>
<p>If that is the case, you can add these agents to your task definition and run them as “sidecars,” or you can use an AWS Lambda function to forward logs from CloudWatch.</p>
<p>To turn on the Fargate-supported log drivers, you need to add the required “logConfiguration” parameters to your task definition for the logging. Examples of log drivers include “awslogs,” which sends container logs to CloudWatch, or “splunk,” or “awsfirelens.”</p>
<p>Add the below log configuration to your container definitions:</p>
<div><img src="https://images.prismic.io/gspann/58ae0c3e-c8af-4191-b228-d6a5287b2a75_6.jpg?auto=compress,format"></div><br/>
<li><strong>Volumes</strong></li>
<p>“Volume” is an optional parameter that you can specify to add volumes to your containers. A container volume is a logical storage unit in a container platform used to store and manage application data. Fargate supports non-persistent empty data volumes for containers.</p>
<p>This is what our container definition looks like after migrating to Fargate:</p>
<div><img src="https://images.prismic.io/gspann/e42317b1-ad5d-4e65-bc26-aeb70ffe26e8_7.jpg?auto=compress,format"></div><br/>
<p>Post migration, your platform looks like this:</p>
<div><img src="https://images.prismic.io/gspann/fb573e55-e7c9-4c23-98b4-dc54080a422f_Image+3_+ECS+Cluster+Dashboard.png?auto=compress,format"></div><br/>
</ul>
<p><strong>Benefits of Migration</strong></p>
<p>Migrating from Amazon EC2 to Amazon Fargate offers many business benefits, such as cost savings, improved scalability and availability, enhanced security, and simplified management. </p>
<ul><li><strong>No more EC2 instances to manage:</strong> There are no EC2 instances for you to manage, maintain, and patch. Also, you’ll have no AMI or security updates, which means zero management overhead for developers. You will get more time to focus on developing applications.</li>
<li><strong>Cost savings:</strong> Fargate is highly cost-effective as you pay for what you use, and there is no over-provisioning. You will be charged for only running containerized tasks as the service uses CPU and memory resources-based pricing.</li>
<li><strong>Scaling:</strong> Scaling your Fargate tasks is almost instantaneous. You can dynamically scale your running tasks based on CPU, memory, and other metrics. You no longer have to deal with scaling EC2 “fleets” for your containers or taking care of bin packing to get your instance to run cost-effectively.</li>
<li><strong>Compliance and security:</strong> Using Fargate gives you out-of-the-box security. Containers running on the AWS Fargate Launch Type now meet the criteria for ISO, PCI, SOC 1, SOC 2, and SOC 3 compliance, along with the requirements for HIPAA eligibility.</li></ul>
<p><strong>Conclusion</strong></p>
<p>AWS Fargate offers businesses a powerful combination of scalability, cost-efficiency, and improved security. By migrating from EC2 to Fargate, companies can simplify the process of building and deploying applications, free up valuable developer time, and reduce costs.</p>
<p>Migrating from AWS ECS to Fargate is straightforward when you use Terraform or similar automation tools. You just need to change a few fields and configuration parameters in the Terraform script to move your service to Fargate. ECS running on Fargate can deliver a serverless experience, significantly changing how you build, deploy, and scale your applications. We hope this guide has provided valuable insights and guidance for your migration journey and look forward to seeing its positive impact on your business.</p>
GSPANN for Amazon Web Services
Maximize Business Efficiency: Use Terraform to Turbocharge AWS Fargate Migration
Serverless computing has gained a lot of popularity in recent years. Development teams can shift their focus solely on developing applications instead of worrying about the server infrastructure, all at a reduced cost. This blog explores how you can use Terraform to migrate an AWS ECS service to a serverless Fargate environment.
AWS Fargate, a fully managed compute engine for containers, provides a scalable, cost-efficient, and secure alternative to managing EC2 instances for AWS ECS clusters. Terraform is an Infrastructure-as-Code tool that allows you to define and provision cloud infrastructure resources using simple and declarative configuration files. By migrating to Fargate, businesses can save time and money while enabling development teams to focus solely on developing applications. This blog will explore how Terraform can automate your migration from EC2 to Fargate.
What is AWS Fargate?
AWS Fargate eliminates the need to manage servers or clusters and simplifies the process of building and deploying applications. Fargate supports Amazon ECS, Amazon EKS, and Amazon ECR to deploy container-based applications. It’s a serverless computing engine for containers without you having to provide an underlying EC2 instance for running containers. Serverless computing is an innovative cloud-based architecture that allows developers to build and run applications without managing infrastructure.
Fargate manages the underlying compute resources, allowing you to focus on building, running, and scaling applications. With Fargate, you can specify the CPU and memory resources needed for your tasks and applications. Fargate also supports auto-scaling, allowing applications to scale up or down based on demand.
Running ECS with Fargate eliminates the need to configure and scale any underlying EC2 instances. You create a cluster, add packaged application containers, specify the resource CPU and memory requirements, define networking and IAM policies, and launch the application. Each Fargate task has its own isolated space and does not share the kernel, elastic network interface (ENI), CPU, or memory resources with other tasks.