29 構築するサービス構成 VPC private subnet group public subnet group Application 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)
copilot env init ● Environmentを指定する 33 CopilotでECSを構築してみる $ copilot env 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ロール
copilot svc deploy ● EnvironmentとServiceを指定してECS環境にデプロイする 34 CopilotでECSを構築してみる $ copilot svc 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サービス、タスク
copilot svc status ● 起動中のServiceを確認する 35 CopilotでECSを構築してみる $ copilot svc 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つ起動
36 構築できたサービス構成 ECR VPC private subnet group public subnet group ECS Internet GW staging (Environment) backend (Backend Service) sample-app (Application)
manifest.ymlの更新(オプション) ● Backend ServiceのService Discoveryを有効にする $ vi copilot/backend/manifest.yml name: backend 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を構築してみる コンテナイメージの 起動ポートを明示的に 指定する必要がある
manifest.ymlの更新(オプション) ● Backend Serviceをプライベートサブネットに配置する $ vi copilot/backend/manifest.yml name: backend 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. network: vpc: placement: 'private' 38 CopilotでECSを構築してみる network.vpc.placement 設定を追加
43 構築できたサービス構成 ECR VPC private subnet group public subnet group Application Load Balancer ECS Internet GW NAT GW backend (Backend Service) staging (Environment) web (Load Balanced Web Service) sample-app (Application)
45 構築できたサービス構成 VPC private subnet group public subnet group Application 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)