DEVELOPER BLOG

HOME > DEVELOPER BLOG > AWS ECS EC2 Launch Type: A Step-by-Step Guide

AWS ECS EC2 Launch Type: A Step-by-Step Guide

1. Introduction

Provide an introduction to AWS Elastic Container Service (ECS) and its significance in container orchestration. Highlight the focus of the article on the ECS (EC2 launch type) and its benefits for managing containerized applications on EC2 instances.

2. Purpose

Clearly state the purpose of the article, which is to walk readers through the process of setting up AWS ECS with the EC2 launch type, with and without PrismScaler integration.

3. What is AWS ECS?

AWS Elastic Container Service (ECS) is a scalable and fully managed container orchestration service provided by Amazon Web Services (AWS). It allows you to easily run and manage Docker containers in a highly available and cost-effective manner. With ECS, you can deploy, scale, and manage containerized applications, making it a popular choice for organizations looking to leverage the benefits of containers without the complexities of manual management.   AWS ECS (Elastic Container Service) offers two launch types: EC2 launch type and Fargate launch type. These launch types provide different ways to run and manage containers within the ECS service, allowing you to choose the most suitable option based on your specific requirements and preferences.   EC2 Launch Type:
  • Infrastructure: In the EC2 launch type, you manage a cluster of Amazon EC2 instances on which your containers are scheduled and run. This gives you more control over the underlying infrastructure.
  • Flexibility: You can choose the instance types, customize the operating system, and make networking adjustments. This is particularly useful if you have specific hardware requirements, need to run custom AMIs (Amazon Machine Images), or require more fine-grained control over networking.
  • Scalability: You can scale the number of instances in your cluster to accommodate varying workloads. ECS handles the placement and orchestration of containers on these instances based on task definitions you define.
  • Use Cases: EC2 launch type is suitable for workloads that need more customization, resource control, and the ability to utilize existing EC2 instances in your infrastructure.
Fargate Launch Type:
  • Serverless: Fargate is a serverless compute engine for containers. It abstracts away the underlying infrastructure, and you don't need to provision or manage any EC2 instances. Instead, you specify the CPU and memory requirements for your containers, and AWS takes care of provisioning the necessary resources.
  • Simplicity: Fargate is simpler to use, as you only focus on your containers, and AWS handles the rest, including patching, scaling, and managing the infrastructure.
  • Isolation: Containers launched with Fargate are more isolated from one another, providing stronger security and ensuring resource allocation.
  • Scalability: Fargate automatically scales the infrastructure to match your application's needs, allowing you to focus solely on your application logic.
  • Use Cases: Fargate is ideal for applications where you want a simpler deployment experience, don't want to manage underlying instances, or need to run containers with precise resource requirements in a highly isolated environment.

4. Creating AWS ECS (EC2 Launch Type) with AWS console

Step-by-step tutorial on creating an AWS ECS cluster and tasks using the EC2 launch type through the AWS Management Console. Step 1. Creating ECS Cluster Go to ECS service → Choose Cluster → Click to “Create cluster”:     Enter cluster name Choose the VPC and subnets you want to place your ECS cluster in     Choose Amazon EC2 instances in Infrastructure tab, then configure the Auto scaling group, create a new ASG like image or you can choose your existing ASG:     Click “Create” to create new cluster.   Step 2. Creating ECS task definition A task definition is a blueprint for your application. It is a text file in JSON format that describes the parameters and one or more containers that form your application. The following are some of the parameters that you can specify in a task definition:
  • The Docker image to use with each container in your task
  • How much CPU and memory to use with each task or each container within a task
  • The launch type to use, which determines the infrastructure that your tasks are hosted on
  • The Docker networking mode to use for the containers in your task
  • The logging configuration to use for your tasks
  • Whether the task continues to run if the container finishes or fails
  • The command that the container runs when it's started
  • Any data volumes that are used with the containers in the task
  • The IAM role that your tasks use
Navigate to Task definitions → Click to “Create new task definition”:     Enter task definition name and choose infrastructure requirements:     Configure container:    
  • For Name, enter a name for the container.
  • For Image URI, enter the image to use to start a container. Images in the Amazon ECR Public Gallery registry may be specified using the Amazon ECR Public registry name only. For example, if public.ecr.aws/ecs/amazon-ecs-agent:latest is specified, the Amazon Linux container hosted on Amazon ECR Public Gallery is used. For all other repositories, specify the repository using either the repository-url/image:tag or repository-url/image@digest formats.
  • A port mapping allows the container to access ports on the host to send or receive traffic. Under Port mappings, do one of the following:
    • When you use the awsvpc network mode, for Container port and Protocol, choose the port mapping to use for the container.
    • When you use the bridge network mode, for Container port and Protocol, choose the port mapping to use for the container.
Select the Use log collection option is an optional option to specify a log configuration. For each available log driver, there are log driver options to specify. The default option sends container logs to CloudWatch Logs.     Then, create the task definition. Step 3. Creating ECS service in ECS cluster Go to your ECS cluster → In the Services tab → Choose “Create”:     Choose Launch type is EC2 and choose the task definition you just created:     In Networking section, you can leave it by default or configure to your VPC and subnets:     Then, create. The result is:     EC2 instance:   Advance: If you want your ECS cluster can connect to other AWS service such as RDS, you have to configure security group for RDS instance to allow inbound from the security group where ECS EC2 instances are placed. With DevOps knowledge: 8-10 hours Without DevOps knowledge: 4-5 days  

5. Creating AWS ECS (EC2 Launch Type) with PrismScaler

With PrismScaler, you can create an ECS cluster and its services with just a few simple operations on a single form. PrismScaler form All you need to do is filling the VPC and subnet you want to place your ECS cluster.     In the case has RDS instance, you also need more RDS instance ID and subnets for DB:       PrismScaler will automatically build a simple ECS cluster and its container on AWS. We also create security groups and IAM role to help EC2 instance connect to RDS instance. 5-15 minutes (without DevOps knowledge)Describe Elastic IP and VPC endpoint

6. Describe Elastic IP and VPC endpoint

Elastic IP for ECS Services
  • Amazon ECS services often consist of a set of tasks (containers) that run on a cluster of EC2 instances or Fargate tasks. These tasks collectively serve a particular application or service.
  • By default, when you run tasks on an ECS cluster, each task is assigned a dynamic private IP address and uses the public IP address of the EC2 instances or Fargate tasks to communicate with the outside world.
  • To provide a static public IP for your ECS service, you can allocate an Elastic IP address and then associate it with one of the EC2 instances in your ECS cluster.
  • Once the Elastic IP is associated with the EC2 instance, the ECS service, which runs tasks on this instance, will be reachable via the static public IP address. This can be useful for scenarios where you need to provide a consistent public IP for your ECS service, such as for external access, load balancers, or DNS configurations.
  • Use Cases:
    • If you have specific EC2 instances in your ECS cluster that need to be reachable via a stable public IP address.
    • When you want to provide direct external access to specific instances in the cluster.
    • Useful when you need a public IP that's independent of ECS services.
  • Security and Network Traffic:
    • Elastic IPs don't inherently provide private, secure communication between ECS tasks and the ECS service. The EC2 instances with the associated Elastic IPs can still communicate with ECS through the public internet or an internet gateway.
VPC endpoint for ECS Services
  • A VPC (Virtual Private Cloud) endpoint for Amazon ECS (Elastic Container Service) is a type of VPC endpoint that allows you to establish a private connection between your Amazon ECS tasks running in a VPC and the Amazon ECS service without requiring the traffic to traverse the public internet. This VPC endpoint is specific to Amazon ECS, and it enhances security, improves network performance, and simplifies the network configuration for your ECS tasks.
  • Use Cases:
    • When you want to enhance security by avoiding public internet exposure for ECS tasks.
    • To improve network performance and reduce data transfer costs by keeping traffic within the AWS network.
    • When you need to restrict ECS service access to specific VPCs or subnets.
  • Security and Network Traffic:
    • VPC endpoints provide a private and secure communication channel between ECS tasks and the ECS service. Traffic doesn't traverse the public internet, enhancing security.

7. Reference

Provide a list of relevant sources and documentation for readers to explore further. Include links to AWS official documentation and other reputable resources.

Amazon EC2 Auto Scaling group capacity providers - Amazon Elastic Container Service Creating a service using the console - Amazon Elastic Container Service Creating a task definition using the console - Amazon Elastic Container Service