使用 AWS Copilot 的 Amazon ECS 入门
通过部署 Amazon ECS 应用程序,开始使用 AWS Copilot 的 Amazon ECS。
先决条件
在开始之前,确保满足以下要求:
-
设置 AWS 账户。有关更多信息,请参阅 设置以使用 Amazon ECS。
-
安装 AWS Copilot CLI。版本目前支持 Linux 和 macOS 系统。有关更多信息,请参阅安装 AWS Copilot CLI。
-
安装和配置 AWS CLI。有关更多信息,请参阅 AWS 命令行界面。
-
运行
aws configure
设置默认配置文件,该配置文件中的 AWS Copilot CLI 将用于托管您的应用程序和服务。 -
安装并运行 Docker 有关更多信息,请参阅 Docker 入门
。
使用一个命令部署您的应用程序
确保已安装 AWS 命令行工具,并且在启动之前已运行 aws configure
。
使用以下命令部署应用程序。
git clone https://github.com/aws-samples/amazon-ecs-cli-sample-app.git demo-app && \ cd demo-app && \ copilot init --app
demo
\ --nameapi
\ --type'Load Balanced Web Service'
\ --dockerfile'./Dockerfile'
\ --port80
\ --deploy
逐步部署应用程序
步骤 1:配置凭证
运行 aws configure
设置默认配置文件,该配置文件中的 AWS Copilot CLI 用于托管您的应用程序和服务。
aws configure
步骤 2:克隆演示应用
克隆一个简单的 Flask 应用程序和 Dockerfile。
git clone https://github.com/aws-samples/amazon-ecs-cli-sample-app.git demo-app
步骤 3:设置您的应用程序
-
在演示应用程序目录中,运行
init
命令。对于 Windows 用户,请从包含下载的
copilot.exe
文件的文件夹中运行init
命令。copilot init
AWS Copilot通过一系列终端提示引导您完成第一个应用程序和服务的设置,从下一步开始。如果您已经使用 了AWS Copilot 部署应用程序时,系统会提示您从应用程序名称列表中选择一个应用程序。
-
命名您的应用程序。
What would you like to name your application? [? for help]
Enter
.demo
步骤 4:在 “演示” 应用程序中设置 ECS 服务
-
系统会提示您选择服务类型。您正在构建一个 服务小 API的简单 Flask 应用程序 。
Which service type best represents your service's architecture? [Use arrows to move, type to filter, ? for more help] > Load Balanced Web Service Backend Service Scheduled Job
Choose
Load Balanced Web Service
-
为您的服务提供名称。
What do you want to name this Load Balanced Web Service? [? for help]
Enter
是您的服务名称。api
-
选择 Dockerfile。
Which Dockerfile would you like to use for api? [Use arrows to move, type to filter, ? for more help] > ./Dockerfile Use an existing image instead
Choose
.Dockerfile
对于 Windows 用户,请在
demo-app folder
中输入 Dockerfile 的路径 (*`...\demo-app\Dockerfile`*\.)。 -
定义端口。
Which port do you want customer traffic sent to? [? for help] (80)
Enter
或接受默认值。80
-
您将看到一个日志,显示正在创建的应用程序资源。
Creating the infrastructure to manage services under application demo.
-
创建应用程序资源后,部署测试环境。
Would you like to deploy a test environment? [? for help] (y/N)
Enter
.y
Proposing infrastructure changes for the test environment.
-
您将看到一个显示应用程序部署状态的日志。
Note: It's best to run this command in the root of your Git repository. Welcome to the Copilot CLI! We're going to walk you through some questions to help you get set up with an application on ECS. An application is a collection of containerized services that operate together. Use existing application:
No
Application name:demo
Workload type:Load Balanced Web Service
Service name:api
Dockerfile:./Dockerfile
no EXPOSE statements in Dockerfile ./Dockerfile Port:80
Ok great, we'll set up aLoad Balanced Web Service
namedapi
in applicationdemo
listening on port80
. ✔ Created the infrastructure to manage services under applicationdemo
. ✔ Wrote the manifest for serviceapi
at copilot/api
/manifest.yml Your manifest contains configurations like your container size and port (:80). ✔ Created ECR repositories for serviceapi
. All right, you're all set for local development. Deploy: Yes ✔ Created the infrastructure for the test environment. - Virtual private cloud on 2 availability zones to hold your services [Complete] - Virtual private cloud on 2 availability zones to hold your services [Complete] - Internet gateway to connect the network to the internet [Complete] - Public subnets for internet facing services [Complete] - Private subnets for services that can't be reached from the internet [Complete] - Routing tables for services to talk with each other [Complete] - ECS Cluster to hold your services [Complete] ✔ Linked accountaws_account_id
and regionregion
to applicationdemo
. ✔ Created environment test in regionregion
under applicationdemo
. Environment test is already on the latest version v1.0.0, skip upgrade. [+] Building 0.8s (7/7) FINISHED => [internal] load .dockerignore 0.1s => => transferring context: 2B 0.0s => [internal] load build definition from Dockerfile 0.0s => => transferring dockerfile: 37B 0.0s => [internal] load metadata for docker.io/library/nginx:latest 0.7s => [internal] load build context 0.0s => => transferring context: 32B 0.0s => [1/2] FROM docker.io/library/nginx@sha256:aeade65e99e5d5e7ce162833636f692354c227ff438556e5f3ed0335b7cc2f1b 0.0s => CACHED [2/2] COPY index.html /usr/share/nginx/html 0.0s => exporting to image 0.0s => => exporting layers 0.0s => => writing image sha256:3ee02fd4c0f67d7bd808ed7fc73263880649834cbb05d5ca62380f539f4884c4 0.0s => => naming toaws_account_id
.dkr.ecr.region
.amazonaws.com/demo
/api
:cee7709 0.0s WARNING! Your password will be stored unencrypted in /home/user/.docker/config.json. Configure a credential helper to remove this warning. See https://docs.docker.com/engine/reference/commandline/login/#credentials-store Login Succeeded The push refers to repository [aws_account_id
.dkr.ecr.region
.amazonaws.com/demo
/api
] 592a5c0c47f1: Pushed 6c7de695ede3: Pushed 2f4accd375d9: Pushed ffc9b21953f4: Pushed cee7709: digest: sha_digest ✔ Deployed api, you can access it at http://demo-Publi-1OQ8VMS2VC2WG-561733989.region
.elb.amazonaws.com.
步骤 5:验证应用程序是否正在运行。
使用以下命令查看应用程序的状态。
列出您的所有 AWS Copilot 应用。
copilot app ls
显示有关应用程序中的环境和服务的信息。
copilot app show
显示有关您的环境的信息。
copilot env ls
显示有关服务的信息,包括终端、容量和相关资源。
copilot svc show
应用程序中所有服务的列表。
copilot svc ls
显示已部署服务的日志。
copilot svc logs
显示服务状态。
copilot svc status
列出可用的命令和选项。
copilot --help
copilot init --help
步骤 6:学习如何创建 CI/CD 管道
相关说明可在ECS 研讨会
步骤 7:清除
运行以下命令来删除和清理所有资源。
copilot app delete