跳至主要內容

[AWS] Deploy to Amazon ECS

Amazon Elastic Container Service (Amazon ECS)  is a fully managed container orchestration service that simplifies your deployment, management, and scaling of containerized applications. Simply describe your application and the resources required, and Amazon ECS will launch, monitor, and scale your application across flexible compute options with automatic integrations to other supporting AWS services that your application needs. Perform system operations such as creating custom scaling and capacity rules, and observe and query data from application logs and telemetry.

product-page-diagram-amazon-ecs

Application lifecycle. The following diagram shows the application lifecycle and how it works with the Amazon ECS components.

ecs-lifecycle

After you create and store your image, you create an Amazon 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 container images that form your application. For example, you can use it to specify which ports to open for your application and what data volumes to use.

After you define your task definition, you deploy it as either a service or a single task on your cluster. A cluster is a logical grouping of tasks or services that runs on the capacity infrastructure that is registered to a cluster.

task is the instantiation of a task definition within a cluster. You can run a standalone task, or you can run a task as part of a service. You can use an Amazon ECS service to run and maintain a desired number of tasks simultaneously in an Amazon ECS cluster. If any of your tasks fail or stop for any reason, the Amazon ECS service scheduler launches another instance based on your task definition. It therefore maintains a desired number of tasks in the service.

After you deploy the task or service, you can use Amazon CloudWatch  to monitor your deployment and application.

Amazon ECS deeply integrates with the AWS environment to provide an easy-to-use solution for running container workloads. As a fully managed service, Amazon ECS comes with AWS configuration and operational best practices built-in. It’s integrated with both AWS and third-party tools, such as Amazon Elastic Container Registry and Docker. This integration makes it easier for teams to focus on building the applications, not the environment. You can run and scale your container workloads across AWS Regions in the cloud, and on-premises, without the complexity of managing a control plane.

Running the UnicornStore with Amazon ECS

Below you can find a visualization on how the UnicornStore application will be deployed and communicate with Amazon ECS:

unicornstore-architecture-ecs

Creating ECS Task definition

  1. Go to Amazon ECS  or navigate to Amazon ECS in the AWS console:
ecs-console
  1. Click Task definitions → Create new task definition.

Configuring task definition and containers

Task definition configuration

  • Task definition family: unicorn-store-spring

Infrastructure requirements

  • Launch Type: AWS Fargate (serverless)
  • OS, Architecture, Network mode: Linux/x86_64
  • CPU: 1 vCPU
  • Memory: 2 Gb
  • Task role: unicornstore-ecs-task-role
  • Task execution role: unicornstore-ecs-task-execution-role

Container – 1

  • Container details:
  • Name: unicorn-store-spring
  • Image URI: the output of the command below
1
echo $(aws ecr describe-repositories --repository-names unicorn-store-spring | jq --raw-output '.repositories[0].repositoryUri'):latest

Port mappings

  • Container port: 8080

Environment variables

  1. Click Add environment variable

Key: SPRING_DATASOURCE_URL

  • Value type: ValueFrom
  • Value: the output of the command below
1
aws cloudformation describe-stacks --stack-name UnicornStoreInfrastructure --query 'Stacks[0].Outputs[?OutputKey==`arnSsmParameterDatabaseJDBCConnectionString`].OutputValue' --output text
  1. Click Add environment variable

Key: SPRING_DATASOURCE_PASSWORD

  • Value type: ValueFrom
  • Value: the output of the command below
1
aws cloudformation describe-stacks --stack-name UnicornStoreInfrastructure --query 'Stacks[0].Outputs[?OutputKey==`arnUnicornStoreDbSecretPassword`].OutputValue' --output text

Configuring environment, storage, monitoring, and tags

Logging

  • Use log collection: checked

Click Create

task-done

Creating ECS cluster

  1. Select Clusters in the left navigation window
  2. Click Create cluster
  3. Follow the wizard

Cluster configuration

  • Cluster name: unicorn-store-spring

Infrastructure

  • AWS Fargate (serverless)

Click Create

Wait until the cluster was successfully created:

ecs-cluster

Creating ECS service

  1. Select the unicorn-store-spring cluster and click Services
  2. Click Create

Deployment configuration

  • Application type: Service
  • Task definition:
  • Family: unicorn-store-spring
  • Revision: LATEST
  • Service name: unicorn-store-spring
  • Desired tasks: 1

Networking

  • VPC: UnicornVPC
  • Subnets: Select Public subnets

Security group: Create a new security group

  • Security group name: UnicornStoreEcsSG
  • Security group description: UnicornStoreEcsSG

Inbound rules for security groups:

Rule 1.

  • Type: Custom TCP
  • Protocol: TCP
  • Port range: 8080
  • Source: Anywhere

Rule 2.

  • Type: HTTP
  • Protocol: TCP
  • Port range: 80
  • Source: Anywhere

Public IP: checked

Load balancing:

  • Load balancer type: Application Load Balancer
  • Application Load Balancer: Create a new load balancer
  • Load balancer: unicorn-store-spring
  • Health check grace period: 60
  • Choose container to load balance: unicorn-store-spring 8080:8080
  • Listener: Create new listener
  • Listener: 80:HTTP
  • Target group: Create new target group
  • Target group name: unicorn-store-spring
  • Health check path: /actuator/health

Click Create

Wait until the service creation is successful. This might take a few minutes.

ecs-service

Testing the application on Amazon ECS

  1. Get the Application Load Balancer Url:
1
2
export SVC_URL=http://$(aws elbv2 describe-load-balancers --names unicorn-store-spring --query "LoadBalancers[0].DNSName" --output text)
echo $SVC_URL
  1. Test the deployed service:
1
2
3
4
5
6
7
curl --location $SVC_URL; echo
curl --location --request POST $SVC_URL'/unicorns' --header 'Content-Type: application/json' --data-raw '{
    "name": "'"Something-$(date +%s)"'",
    "age": "20",
    "type": "Animal",
    "size": "Very big"
}' | jq
  1. You should get the following result:
test-success

Accessing the application logs

To further inspect the application startup or runtime behavior you can navigate to the application logs with the following steps.

  1. Go to the Amazon ECS  console.
  2. Select “Clusters” → unicorn-store-spring → “Services” unicorn-store-spring.
  3. Select “Logs” and inspect the startup time of the application:
ecs-logs

Section finished

In this section learned how to deploy and manage the UnicornStore Java applications using Amazon ECS. Your learned how to create ECS clusters, services, task definitions and configured a load balancer.


分類:aws
由 Compete Themes 設計的 Author 佈景主題