ほぼ以下ドキュメントの手順通りに行います。
サンプルアプリのクローン
まず、GitHubからサンプルアプリをクローンします。
$ git clone https://github.com/aws-samples/amazon-ecs-cli-sample-app.git demo-app
$ cd demo-app
認証情報の設定
上記ドキュメントでは認証情報にデフォルトのプロファイルを使用していますが、私は名前付きプロファイルを利用しているため、名前付きプロファイルの認証情報を利用できるように設定します。
$ export AWS_PROFILE={profile名}
AWS_PROFILE環境変数に使用したいプロファイル名を設定すれば、Copilotはそのプロファイル名の認証情報を利用します。
copilot init
ECSを作成するために、copilot init を実行します。
あとは質問に答えていくだけで、ECSが作成されます。
なお、以降のコードブロックは質問単位で区切っていますが、実際のターミナルでは途切れることなく質問を受けています。
質問の際に「?」か「help」を入力すると質問の詳細について表示されるため、以降のコードブロックでは「?」を入力した際に表示される説明も含んでいます。
また、コメント部分は日本語訳です。
$ copilot init
// Copilot CLIへようこそ!
// これから、AWS上のコンテナ化されたアプリケーションをセットアップするのに役立ついくつかの質問を通して、あなたを案内します。
// アプリケーションは、一緒に動作するコンテナ化されたサービスの集合体です。
Welcome to the Copilot CLI! We're going to walk you through some questions
to help you get set up with a containerized application on AWS. An application is a collection of
containerized services that operate together.
// 同じアプリケーションのサービスとジョブは同じVPCとECS Clusterを共有し、サービスはサービスディスカバリーで発見することができます。
Services and jobs in the same application share the same VPC and ECS Cluster and services are discoverable via service discovery.
What would you like to name your application? [? for help] demo
Application name: demo
アプリケーションのサービスに何をさせたいかを聞かれるので、「Load Balanced Web Service (Internet to ECS on Fargate)」を選択します。
// Request-Driven Web Serviceは、AWS App Runnerで管理されるインターネットに面したHTTPサーバーで、入ってくるリクエストに応じてスケールアップする。
A Request-Driven Web Service is an internet-facing HTTP server managed by AWS App Runner that scales based on incoming requests.
To learn more see: https://git.io/JEEfb
// ロードバランスウェブサービスは、ロードバランサーの背後にあるAWS Fargate上のAmazon ECSによって管理されるインターネットに面したHTTPサーバーです。
A Load Balanced Web Service is an internet-facing HTTP server managed by Amazon ECS on AWS Fargate behind a load balancer.
To learn more see: https://git.io/JEEJe
// バックエンドサービスとは、VPC内の他のサービスからアクセス可能な、インターネットに接続されていないプライベートなサービスです。
A Backend Service is a private, non internet-facing service accessible from other services in your VPC.
To learn more see: https://git.io/JEEJt
// Worker Serviceは、アプリケーションのトピックに発行されたメッセージを消費することができるプライベートサービスです。
A Worker Service is a private service that can consume messages published to topics in your application.
To learn more see: https://git.io/JEEJY
// Worker Serviceは、アプリケーションのトピックに発行されたメッセージを消費することができるプライベートサービスです。
A Scheduled Job is a task which is invoked on a set schedule, with optional retry logic.
To learn more see: https://git.io/JEEU4
Which workload type best represents your architecture? [Use arrows to move, type to filter]
> Request-Driven Web Service (App Runner)
Load Balanced Web Service (Internet to ECS on Fargate)
Backend Service (ECS on Fargate)
Worker Service (Events to SQS to ECS on Fargate)
Scheduled Job (Scheduled event to State Machine to Fargate)
Workload type: Load Balanced Web Service
アプリケーションのサービス名を聞かれているので、「api」と入力します。
The name will uniquely identify this service within your app demo.
Deployed resources (such as your ECR repository, logs) will contain this service's name and be tagged with it.
What do you want to name this service?
Service name: api
どの場所のDockerfileを使用するか、または既存のイメージを使用するか聞かれているので、カレントディレクトリのDockerfileを選択します。
Dockerfile to use for building your container image.
Which Dockerfile would you like to use for api? [Use arrows to move, type to filter]
> ./Dockerfile
Enter custom path for your Dockerfile
Use an existing image instead
Dockerfile: ./Dockerfile
parse EXPOSE: no EXPOSE statements in Dockerfile ./Dockerfile
ロードバランサーがルーティングする宛先ポートを聞かれているので、デフォルトの80にします(エンターするだけで80が選ばれます)。
// このポートは、ロードバランサーがこのサービスに受信トラフィックをルーティングするために使用されます。
// Dockerfile がインターネットとの通信に使用するポートを指定してください。
The port will be used by the load balancer to route incoming traffic to this service.
You should set this to the port which your Dockerfile uses to communicate with the internet.
Which port do you want customer traffic sent to? (80)
Port: 80
Ok great, we'll set up a Load Balanced Web Service named api in application demo listening on port 80.
アプリケーション作成中のログが表示されます。
Creating the infrastructure to manage services and jobs under application demo.
アプリケーション作成が完了すれば、以下のログが表示されます。
この時点で、CloudFormationには「demo-infrastructure-roles」スタックが作成されており、IAMロール2つが作成されていましたが、このスタックからECS関連のリソースは作成されていませんでした。
✔ Created the infrastructure to manage services and jobs under application demo.
✔ The directory copilot will hold service manifests for application demo.
✔ Wrote the manifest for service api at ../api/manifest.yml
Your manifest contains configurations like your container size and port (:80).
✔ Created ECR repositories for service api.
All right, you're all set for local development.
アプリケーションリソースの準備ができたので、「y」を入力してテスト環境をデプロイします。
//サービスがデプロイされている環境。これにより、本番環境に配置する前にサービスをテストできます。
An environment with your service deployed to it. This will allow you to test your service before placing it in production.
Would you like to deploy a test environment? (y/N) y
Deploy: Yes
以下のログが表示された際、CloudFormation StackSet に「demo-infrastructure」が作成され、スタック「StackSet-demo-infrastructure-xxx」が作成されました。
リソースは、ECR、KMSキー、S3が作成されています。
Linked account {accountId} and region ap-northeast-1 to application demo.
その後、デプロイが成功すると以下のログが表示されます。
CloudFormation スタック「demo-test」「demo-test-api」が作成され、VPCやECSなどのリソースが作成されていました。
✔ Deployed service api.
Recommended follow-up action:
- You can access your service at http://demo-Publi-148GBD0BL13HV-1625450431.ap-northeast-1.elb.amazonaws.com over the internet.
- Be a part of the Copilot ✨community✨!
Ask or answer a question, submit a feature request...
Visit 👉 https://aws.github.io/copilot-cli/community/get-involved/ to see how!
上記サービスのURLにアクセスすると、ECSのロゴが表示される画面が確認できます。
削除
Copilot CLIを利用してのECS作成が確認ができたので、以下のコマンドを実行して削除して終了です。
$ copilot app delete