Load Balancer ECS Internet GW NAT GW web (Load Balanced Web Service) backend (Backend Service) ECR VPC private subnet group public subnet group Application Load Balancer ECS Internet GW NAT GW backend (Backend Service) production (Environment) staging (Environment) web (Load Balanced Web Service) sample-app (Application)
init --name staging Which credentials would you like to use to create staging? [profile default] Would you like to use the default configuration for a new environment? - A new VPC with 2 AZs, 2 public subnets and 2 private subnets - A new ECS Cluster - New IAM Roles to manage services and jobs in your environment Yes, use default. ✔ Linked account 123456789012 and region ap-northeast-1 to application sample-app. ✔ Proposing infrastructure changes for the sample-app-staging environment. ✔ Created environment staging in region ap-northeast-1 under application sample-app. AWSリソース作成 • VPC、サブネット • ECSクラスター • IAMロール
deploy --name backend --env staging [+] Building 3.5s (8/8) FINISHED Login Succeeded The push refers to repository [123456789012.dkr.ecr.ap-northeast-1.amazonaws.com/sample-app/backend] ✔ Proposing infrastructure changes for stack sample-app-staging-backend - Creating the infrastructure for stack sample-app-staging-backend - Service discovery for your services to communicate within the VPC - Update your environment's shared resources - An IAM Role for the Fargate agent to make AWS API calls on your behalf - A CloudWatch log group to hold your service logs - An ECS service to run and maintain your tasks in the environment cluster - An ECS task definition to group your containers and run them on ECS - An IAM role to control permissions for the containers in your tasks ✔ Deployed backend. コンテナイメージを buildしてECRにpush AWSリソースを更新 • Service discovery(Cloud Map、Route 53) • CloudWatch Logs • ECSサービス、タスク
status --name backend --app sample-app --env staging Service Status ACTIVE 1 / 1 running tasks (0 pending) Last Deployment Updated At xx minutes ago Task Definition arn:aws:ecs:ap-northeast-1:123456789012:task-definition/sample-app-staging-backend:1 Task Status ID Image Digest Last Status Started At Stopped At Health Status -- ------------ ----------- ---------- ---------- ------------- 1234abcde abcd1234 RUNNING xx minutes ago - UNKNOWN Alarms Name Condition Last Updated Health ---- --------- ------------ ------ タスク定義を作成、 タスクを1つ起動
type: Backend Service image: build: app/Dockerfile port: 80 cpu: 256 # Number of CPU units for the task. memory: 512 # Amount of memory in MiB used by the task. count: 1 # Number of tasks that should be running in your service. exec: true # Enable running commands in your container. 37 CopilotでECSを構築してみる コンテナイメージの 起動ポートを明示的に 指定する必要がある
Backend Service image: build: app/Dockerfile port: 80 cpu: 256 # Number of CPU units for the task. memory: 512 # Amount of memory in MiB used by the task. count: 1 # Number of tasks that should be running in your service. exec: true # Enable running commands in your container. network: vpc: placement: 'private' 38 CopilotでECSを構築してみる network.vpc.placement 設定を追加
deploy --name backend --app sample-app --env staging - Updating the infrastructure for stack sample-app-staging-backend - Update your environment's shared resources - NAT Gateway 2 enabling workloads placed in private subnet 2 to reach the internet - NAT Gateway 1 enabling workloads placed in private subnet 1 to reach the internet ✔ Deployed backend, its service discovery endpoint is backend.sample-app.local:80. 39 CopilotでECSを構築してみる
Web Serviceを指定する 41 CopilotでECSを構築してみる $ copilot svc init --name web --app sample-app Workload type: Load Balanced Web Service Dockerfile: nginx/Dockerfile no EXPOSE statements in Dockerfile nginx/Dockerfile Port: 80 $ tree copilot copilot └── backend └── manifest.yml └── web └── manifest.yml manifest.yml ファイルが作成される Load Balanced Web Service ではデフォルトで ポート80が指定される
CopilotでECSを構築してみる $ copilot svc deploy --name web --app sample-app --env staging ✔ Deployed web, you can access it at http://sampl-Publi-0ABCDEFGHIJKL-1234567890.ap-northeast-1.elb.amazonaws.com. $ curl http://sampl-Publi-0ABCDEFGHIJKL-1234567890.ap-northeast-1.elb.amazonaws.com OK $ curl http://sampl-Publi-0ABCDEFGHIJKL-1234567890.ap-northeast-1.elb.amazonaws.com/actuator/health {"status":"UP"} ALB -> web -> backend 接続確認 ALB -> web 接続確認
Load Balancer ECS Internet GW NAT GW web (Load Balanced Web Service) backend (Backend Service) ECR VPC private subnet group public subnet group Application Load Balancer ECS Internet GW NAT GW backend (Backend Service) production (Environment) staging (Environment) web (Load Balanced Web Service) sample-app (Application)