DEVELOPER BLOG

HOME > DEVELOPER BLOG > 【Introduction to AWS】Creating Microservices in Amazon ECS - PrismScaler

【Introduction to AWS】Creating Microservices in Amazon ECS - PrismScaler

1. Introduction

Hello! We are a writer team from Definer Inc. Microservices is an architectural approach that structures an application as a collection of small, loosely coupled, and independently deployable services. Each service focuses on a specific business capability and communicates with other services through well-defined APIs. This approach promotes modularity, scalability, and flexibility, allowing teams to develop, deploy, and manage services independently. Microservices enable organizations to build complex applications that are easier to develop, maintain, and scale. Amazon Elastic Container Service (ECS) is a fully managed container orchestration service provided by Amazon Web Services (AWS). It simplifies the deployment, management, and scaling of containerized applications. With ECS, you can run Docker containers in a highly scalable and reliable environment. It provides features like auto-scaling, load balancing, service discovery, and integration with other AWS services. ECS allows you to build and deploy microservices architectures on AWS with ease, ensuring high availability, scalability, and cost efficiency. Creating microservices in Amazon Elastic Container Service (ECS) provides a scalable and managed environment for deploying and running containerized applications. Amazon ECS is a highly scalable and reliable container orchestration service offered by Amazon Web Services (AWS). It simplifies the deployment, management, and scaling of containerized applications using Docker containers. With ECS, you can build and deploy microservices architectures that are resilient, efficient, and easy to manage. In this issue, you are wondering how to create a microservice in Amazon ECS. Let's take a look at the actual screens and resources to explain in detail.

2. What is AWS ECS?

AWS ECS (Elastic Container Service) is a fully managed container orchestration service provided by Amazon Web Services (AWS). It enables you to run, manage, and scale Docker containers easily and efficiently. ECS allows you to deploy containerized applications on a cluster of EC2 instances or using the AWS Fargate service, which abstracts away the underlying infrastructure and allows you to focus solely on your containers. Key features and components of AWS ECS: (1) Task Definitions: A task definition is a blueprint that defines how a Docker container should be launched, including container images, resource requirements, networking settings, and other configurations. (2) Tasks and Services: In ECS, a task represents a running instance of a task definition. A service manages and maintains the desired number of tasks and ensures high availability and scalability of your application. (3) Clusters: An ECS cluster is a logical group of EC2 instances or Fargate tasks that run your containers. It provides the underlying infrastructure for running tasks. (4) Integration with Other AWS Services: ECS integrates with other AWS services, such as Amazon VPC (Virtual Private Cloud), IAM (Identity and Access Management), CloudWatch for monitoring, and Application Load Balancers for load distribution among containers. (5) Autoscaling: ECS supports automatic scaling of tasks based on demand. You can set up scaling policies to adjust the number of tasks or services based on metrics like CPU utilization or the number of incoming requests. (6) Managed Updates: ECS allows you to perform rolling updates to your services, ensuring minimal downtime during updates and deployments. (7) Task Placement Strategies: ECS provides different task placement strategies, like "spread" or "binpack," to optimize resource utilization and spread containers across instances. (8) Task Scheduling: You can use the ECS task scheduler to place tasks based on rules or constraints, making it easy to distribute tasks across the cluster. AWS ECS is particularly useful for containerizing applications, microservices architecture, and running batch processing workloads in a scalable and cost-effective manner. It integrates well with other AWS services, making it a popular choice for container orchestration in the AWS ecosystem. Additionally, ECS offers support for AWS Fargate, a serverless compute engine, which abstracts away the need to manage EC2 instances, allowing you to focus on running containers without worrying about infrastructure management.

3. Purpose/Use Cases

The purpose of creating microservices in Amazon ECS is to enable the development and deployment of complex applications as a collection of loosely coupled, independently deployable, and scalable services. Here are some key purposes and benefits of using Amazon ECS for creating microservices: (1) Scalability and Resilience (2) Containerization (3) Service Discovery and Load Balancing (4) Task Definition and Deployment Flexibility (5) Integration with AWS Ecosystem (6) DevOps and Continuous Deployment (7) Cost Optimization

4. Container Image Setup

The first step is to set up the system.   (1) Creation of ECR Repository: To create an ECR repository, follow these steps:  - Log in to the AWS Management Console.  - Navigate to the Amazon ECR service.  - Click on the "Create repository" button.  - Provide a unique name for your repository.  - Optionally, configure repository settings such as encryption and image scanning.  - Click on the "Create repository" button to create the ECR repository.  - The ECR repository acts as a secure and managed container image registry where you can store and manage your Docker container images. (2) Push container image to ECR: To push a container image to ECR, follow these steps:  - Ensure that you have Docker installed on your local machine.  - Log in to ECR using the AWS Command Line Interface (CLI)  - Tag the container image with the ECR repository URI  - Push the tagged container image to ECR
## ECR creation command
aws ecr create-repository \
--repository-name test-repository \
--image-scanning-configuration scanOnPush=true \
--region ap-northeast-1                
## Get nginx image
docker run nginx
## Check nginx images
docker images


## Log in to ECR
aws ecr get-login-password | docker login --username AWS --password-stdin https://${AWS account number}.dkr.ecr.ap-northeast-1.amazonaws.com/
## Tag the image
docker tag nginx:latest ${AWS account number}.dkr.ecr.ap-northeast-1.amazonaws.com/test-repository
## Push image to ECR
docker push ${AWS account number}.dkr.ecr.ap-northeast-1.amazonaws.com/test-repository
                  

5. Creation of ECS

Next, we will create the ECS.   (1) First, create a task definition: To create a task definition in Amazon ECS, follow these steps:  - Prepare a JSON file that describes the inputs for your task definition. This file includes information such as the container image, container port mappings, resource requirements, and any environment variables or volume configurations needed for your application.  - Using the AWS Command Line Interface (CLI) to create the task definition  - If the creation is successful, the command will return a JSON response that includes details of the registered task definition.  - The task definition defines how your containers should be run within an Amazon ECS task. (2) Next, create an ECS service: To create an ECS service, follow these steps:  - Using the AWS CLI to create the ECS service  - If the creation is successful, the command will return a JSON response that includes details of the created ECS service.  - The ECS service manages the running instances of your task definition and ensures the desired number of instances are running and healthy. (3) Confirmation of container startup: To confirm that the container has started successfully, follow these steps:  - Go to the Amazon ECS console.  - Click on the "default" cluster (or the name of your cluster).  - Navigate to the "Tasks" tab.  - Click on the task in which the container is running to view the task details.  - Find the "Public IP" field in the task details. If the task is associated with a public IP address, you can use it to access the running container.  - Open a web browser and enter the public IP address obtained above.  - You should be able to see the nginx page or the application running inside the container.
##Create a Json file that will serve as input for the task definition
touch input.json
##Input in input.json
vim input.json

## Register task definition
aws ecs register-task-definition --cli-input-json file://input.json
## Confirm registration of task definition
aws ecs list-task-definition-families --family-prefix test-nginx

## Start ECS service
aws ecs create-service \
--cluster default \filter
--service-name test-nginx \
--task-definition test-nginx:1 \
--network-configuration "awsvpcConfiguration={subnets=[${subnet ID}],securityGroups=[${security group ID}],assignPublicIp=ENABLED}" \
--launch-type FARGATE \}
--desired-count 1                
 

Contents of input.json
{
    "family": "test-nginx",
    "containerDefinitions": [
        {
            "name": "test-nginx",
            "image": "${AWS Account Number}.dkr.ecr.ap-northeast-1.amazonaws.com/test-repository:latest",
            "cpu": 512,
            "memory": 1024,
            "links": [],
            "portMappings": [
                {
                    "containerPort": 80,
                    "hostPort": 80,
                    "protocol": "tcp"
                }
            ],
            "essential": true,
            "entryPoint": [],
            "command": [],
            "environment": [
                {
                    "name": "ENV",
                    "value": "dev"
                }
            ],
            "environmentFiles": [],
            "mountPoints": [],
            "volumesFrom": [],
            "dnsServers": [],
            "dnsSearchDomains": [],
            "extraHosts": [],
            "dockerSecurityOptions": [],
            "dockerLabels": {},
            "ulimits": [],
            "systemControls": []
        }
    ],
    "taskRoleArn": "${IAM Roles for ECS Tasks}",
    "executionRoleArn": "${IAM role for ECS task execution}",
    "networkMode": "awsvpc",
    "requiresCompatibilities": [
        "EC2",
        "FARGATE"
    ],
    "cpu": "512",
    "memory": "1024"
}                  

6. Cited/Referenced Articles

7. About the proprietary solution "PrismScaler"

・PrismScaler is a web service that enables the construction of multi-cloud infrastructures such as AWS, Azure, and GCP in just three steps, without requiring development and operation. ・PrismScaler is a web service that enables multi-cloud infrastructure construction such as AWS, Azure, GCP, etc. in just 3 steps without development and operation. ・The solution is designed for a wide range of usage scenarios such as cloud infrastructure construction/cloud migration, cloud maintenance and operation, and cost optimization, and can easily realize more than several hundred high-quality general-purpose cloud infrastructures by appropriately combining IaaS and PaaS.  

8. Contact us

This article provides useful introductory information free of charge. For consultation and inquiries, please contact "Definer Inc".

9. Regarding Definer

・Definer Inc. provides one-stop solutions from upstream to downstream of IT. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition/design development/implementation, and maintenance and operation. ・We are committed to providing integrated support for advanced IT technologies such as AI and cloud IT infrastructure, from consulting to requirement definition, design development, implementation, maintenance, and operation. ・PrismScaler is a high-quality, rapid, "auto-configuration," "auto-monitoring," "problem detection," and "configuration visualization" for multi-cloud/IT infrastructure such as AWS, Azure, and GCP.