使用 AWS Copilot CLI 将 .NET 6 API 部署到 AWS App Runner
在本教程博客文章中,我们将了解如何使用 AWS Copilot CLI 将 .NET 6 API 部署到 AWS App Runner。 什么是 App Runner? AWS App Runner 是一项完全托管的服务,让开发人员可以轻松快速地大规模部署容器化 Web 应用程序和 API,而无需具备基础设施经验。从您的源代码或容器映像开始。 App Runner 自动构建和部署 Web 应用程序
在本教程博客文章中,我们将了解如何使用 AWS Copilot CLI 将 .NET 6 API 部署到 AWS App Runner。
什么是 App Runner?
AWS App Runner 是一项完全托管的服务,让开发人员可以轻松快速地大规模部署容器化 Web 应用程序和 API,而无需具备基础设施经验。从您的源代码或容器映像开始。 App Runner 自动构建和部署 Web 应用程序,并通过加密对流量进行负载平衡。 App Runner 还可以自动扩展或缩减以满足您的流量需求。使用 App Runner,您有更多时间专注于应用程序,而不是考虑服务器或扩展。
[](https://res.cloudinary.com/practicaldev/image/fetch/s--tU6MgTwm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn.hashnode .com/res/hashnode/image/upload/v1635622899259/lgLdZDVat.png)
应用程序运行器功能
App Runner 的特点是
-
自动缩放:随着需求的变化在可配置的最小和最大限制之间启动和停止。
-
负载均衡:该服务包括一个透明的、不可配置的负载均衡器。该 URL 可以指向自定义域。
-
SSL 和证书: 部署的服务将为具有 AWS 托管证书的应用程序提供 HTTPS 端点。证书将在即将到期时更新。
-
**构建服务:**您可以推送自己的图像或让 AWS 从代码中为您构建它们。
AWS Copilot CLI
AWS Copilot CLI 是一种工具,供开发人员在 AWS App Runner、Amazon ECS 和 AWS Fargate 上构建、发布和操作生产就绪的容器化应用程序。从开始、推送到登台以及发布到生产,Copilot 可以帮助管理您的应用程序开发的整个生命周期。
aws/copilot-cli
AWS Copilot CLI 是一种工具,供开发人员在 AWS App Runner、Amazon ECS 和 AWS Fargate 上构建、发布和操作生产就绪的容器化应用程序。
设置 .NET 6 最小 API
让我们开始吧。本部分将使用 .NET 创建最小的 API 服务,并使用 Copilot CLI 将应用程序 docker 化以部署在 AWS AppRunner 中。
- 使用 dotnet 创建新的 Web API 项目
dotnet new web -n CoffeeService
- API 代码如下所示,默认由模板生成。
var builder u003d WebApplication.CreateBuilder(args);
var app u003d builder.Build();
app.MapGet("/", () u003d> "Hello World!");
应用程序运行();
看到这个最小的 API 真是太酷了(就像开发 Node.js 代码一样简单)。让我们在本地运行,看看我们是否得到了这个“Hello World”响应。
- 运行代码
cd CoffeeService && dotnet 运行
- 执行 URL 并检查响应
➜ ~ curl http://localhost:5023/
你好世界!
容器化 API
在包含 .csproj 的目录中创建一个名为 Dockerfile 的文件,然后在文本编辑器中打开它。将以下内容复制到 Dockerfile 中。在这个 Dockerfile 中,我们正在构建项目并在aspnet:6.0
运行时映像中运行它。
FROM mcr.microsoft.com/dotnet/sdk:6.0 AS build
WORKDIR /src
COPY ["CoffeeService.csproj", "./"]
RUN dotnet restore "CoffeeService.csproj"
COPY . .
WORKDIR "/src/."
RUN dotnet build "CoffeeService.csproj" -c Release -o /app/build
FROM build AS publish
RUN dotnet publish "CoffeeService.csproj" -c Release -o /app/publish
FROM mcr.microsoft.com/dotnet/aspnet:6.0 AS base
WORKDIR /app
EXPOSE 5023
ENV ASPNETCORE_URLS=http://+:5023
WORKDIR /app
COPY --from=publish /app/publish .
ENTRYPOINT ["dotnet", "CoffeeService.dll"]
进入全屏模式 退出全屏模式
- 构建docker镜像
码头工人建造。 -t 咖啡服务:最新
- 运行docker镜像
docker run -p 5023:5023 咖啡服务:最新
- 执行 URL 并检查响应
➜ ~ curl http://localhost:5023/
你好世界!
伟大的。 .NET 6 最小 API 在本地和 docker 容器中创建和运行。现在让我们使用 AWS Copilot CLI 部署到 AWS AppRunner
使用 AWS Copilot CLI 部署到 AWS AppRunner
运行copilot init
以为此服务设置 AWS AppRunner 应用程序。初始化工具会问你问题。
1.使用现有应用程序或创建新应用程序
- 应用名称
3.工作负载类型
您可以选择其中一种工作负载类型。在此演示中,我们正在设置 AWS App Runner。
- **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)
进入全屏模式 退出全屏模式
1.要构建的Dockerfile的服务名称和路径
- 设置好 ECR 后,您可以设置要部署的环境。
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.
Use existing application: No
Application name: coffee-shop
Workload type: Request-Driven Web Service
Service name: coffee-service
Dockerfile: CoffeeService/Dockerfile
Ok great, we'll set up a Request-Driven Web Service named coffee-service in application coffee-shop listening on port 5023.
✔ Created the infrastructure to manage services and jobs under application coffee-shop..
✔ The directory copilot will hold service manifests for application coffee-shop.
✔ Wrote the manifest for service coffee-service at copilot/coffee-service/manifest.yml
Your manifest contains configurations like your container size and port (:5023).
✔ Created ECR repositories for service coffee-service..
All right, you're all set for local development.
Deploy: Yes
✔ Linked account 495775103319 and region us-east-1 to application coffee-shop..
✔ Proposing infrastructure changes for the coffee-shop-test environment.
- Creating the infrastructure for the coffee-shop-test environment. [create complete] [79.6s]
- An IAM Role for AWS CloudFormation to manage resources [create complete] [16.2s]
- An ECS cluster to group your services [create complete] [9.0s]
- Enable long ARN formats for the authenticated AWS principal [create complete] [4.6s]
- An IAM Role to describe resources in your environment [create complete] [13.9s]
- A security group to allow your containers to talk to each other [create complete] [5.9s]
- An Internet Gateway to connect to the public internet [create complete] [16.1s]
- Private subnet 1 for resources with no internet access [create complete] [19.3s]
- Private subnet 2 for resources with no internet access [create complete] [19.6s]
- Public subnet 1 for resources that can access the internet [create complete] [19.6s]
- Public subnet 2 for resources that can access the internet [create complete] [19.6s]
- A Virtual Private Cloud to control networking of your AWS resources [create complete] [16.1s]
✔ Created environment test in region us-east-1 under application coffee-shop.
Environment test is already on the latest version v1.6.1, skip upgrade.
[+] Building 0.7s (18/18) FINISHED
=> [internal] load build definition from Dockerfile 0.0s
=> => transferring dockerfile: 588B 0.0s
=> [internal] load .dockerignore 0.0s
=> => transferring context: 374B 0.0s
=> [internal] load metadata for mcr.microsoft.com/dotnet/aspnet:6.0 0.5s
=> [internal] load metadata for mcr.microsoft.com/dotnet/sdk:6.0 0.5s
=> [internal] load build context 0.0s
=> => transferring context: 1.63kB 0.0s
=> [build 1/7] FROM mcr.microsoft.com/dotnet/sdk:6.0@sha256:96ce062b7e664999048b86198385fea1ddaff31d8d2ab5f7c42c0077678afeac 0.0s
=> [base 1/4] FROM mcr.microsoft.com/dotnet/aspnet:6.0@sha256:ed9b7dc3e8278a56be619b278762689565e1e21f61da51551fe028dc1d3a536f 0.0s
=> CACHED [base 2/4] WORKDIR /app 0.0s
=> CACHED [base 3/4] WORKDIR /app 0.0s
=> CACHED [build 2/7] WORKDIR /src 0.0s
=> CACHED [build 3/7] COPY [CoffeeService.csproj, ./] 0.0s
=> CACHED [build 4/7] RUN dotnet restore "CoffeeService.csproj" 0.0s
=> CACHED [build 5/7] COPY . . 0.0s
=> CACHED [build 6/7] WORKDIR /src/. 0.0s
=> CACHED [build 7/7] RUN dotnet build "CoffeeService.csproj" -c Release -o /app/build 0.0s
=> CACHED [publish 1/1] RUN dotnet publish "CoffeeService.csproj" -c Release -o /app/publish 0.0s
=> CACHED [base 4/4] COPY --from=publish /app/publish . 0.0s
=> exporting to image 0.0s
=> => exporting layers 0.0s
=> => writing image sha256:74811373b140080ac1cf2f55c1e15a8dcdd9059f9a82d98a97a4f94a4e40e559 0.0s
=> => naming to 495775103319.dkr.ecr.us-east-1.amazonaws.com/coffee-shop/coffee-service 0.0s
Use 'docker scan' to run Snyk tests against images to find vulnerabilities and learn how to fix them
Login Succeeded
Using default tag: latest
The push refers to repository [495775103319.dkr.ecr.us-east-1.amazonaws.com/coffee-shop/coffee-service]
8cb51c566055: Pushed
...
e8b689711f21: Pushed
latest: digest: sha256:daeea40ed5b765e0ca27232964c529f3d6bb18eb8ccced8828a39c3153991c6a size: 1993
✔ Proposing infrastructure changes for stack coffee-shop-test-coffee-service
- Creating the infrastructure for stack coffee-shop-test-coffee-service [create complete] [246.5s]
- An IAM Role for App Runner to use on your behalf to pull your image from ECR [create complete] [10.2s]
- An IAM role to control permissions for the containers in your service [create in progress] [238.4s]
- An App Runner service to run and manage your containers [create complete] [225.4s]
✔ Deployed service coffee-service.
Recommended follow-up action:
You can access your service at https://ptydisq8gp.us-east-1.awsapprunner.com over the internet.
进入全屏模式 退出全屏模式
通过 URL 访问服务。
➜ curl https://ptydisq8gp.us-east-1.awsapprunner.com
Hello World!
进入全屏模式 退出全屏模式
Copilot 正在为我们 - 开发人员做繁重的工作。所以我们可以专注于应用程序。 Copilot 将在 AWS 上构建、推送和启动您的容器到 ECS、Fargate 和 AppRunner。
设置自动化流水线
devops 流程的关键原则是“Ship small, Ship often”。定期部署小功能的过程对于 DevOps 至关重要。随着团队变得更加敏捷,我们需要以多个开发人员的身份自动发布应用程序;多个服务团队将代码推送到源代码存储库中。
AWS Copilot 工具可以帮助您设置管道以自动化应用程序发布。您可以运行这些命令来创建在 git push 上构建和部署应用程序的自动化管道。
aws-apprunner-dotnet6-demo git:(main) copilot pipeline init
1st stage: test
Repository URL: git@github.com:ksivamuthu/aws-apprunner-coffee-shop
✔ Wrote the pipeline manifest for aws-apprunner-coffee-shop at 'copilot/pipeline.yml'
The manifest contains configurations for your CodePipeline resources, such as your pipeline stages and build steps.
Update the file to add additional stages, change the branch to be tracked, or add test commands or manual approval actions.
✔ Wrote the buildspec for the pipeline's build stage at 'copilot/buildspec.yml'
The buildspec contains the commands to build and push your container images to your ECR repositories.
Update the build phase to unit test your services before pushing the images.
Required follow-up actions:
- Commit and push the buildspec.yml, pipeline.yml, and .workspace files of your copilot directory to your repository.
- Run `copilot pipeline update` to create your pipeline.
进入全屏模式 退出全屏模式
copilot buildspec、管道 yaml 文件被提交和推送。代码构建管道设置为在测试环境中自动部署。
[](https://res.cloudinary.com/practicaldev/image/fetch/s--NPC-E_0y--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https:// cdn.hashnode.com/res/hashnode/image/upload/v1635622988061/2kjwADg2L.png)
现在,它已经准备好让更多的开发人员开始摇摆不定了。他们不需要安装副驾驶来部署机器的服务,因为他们正在开发。部署步骤是自动化的。
让我们推送更多代码以查看自动部署的更改。
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.MapGet("/", () => "Hello AWS AppRunner !!");
app.MapGet("/api/coffee", () => new List<dynamic> {
new { CoffeeId = "cappucino", CoffeeName = "Cappucino" },
new { CoffeeId = "latte", CoffeeName = "Latte" },
new { CoffeeId = "mocha", CoffeeName = "Mocha" },
new { CoffeeId = "americano", CoffeeName = "Americano" },
new { CoffeeId = "macchiato", CoffeeName = "Macchiato" },
new { CoffeeId = "frappe", CoffeeName = "Frappe" },
new { CoffeeId = "corretto", CoffeeName = "Corretto" },
new { CoffeeId = "affogato", CoffeeName = "Affogato" },
new { CoffeeId = "filtercoffee", CoffeeName = "Filter Coffee" },
});
app.Run();
进入全屏模式 退出全屏模式
➜ aws-apprunner-dotnet6-demo git:(main) curl https://ptydisq8gp.us-east-1.awsapprunner.com
Hello AWS AppRunner !!
➜ aws-apprunner-dotnet6-demo git:(main) curl -s https://ptydisq8gp.us-east-1.awsapprunner.com/api/coffee | jq '.[0]'
{
"coffeeId": "cappucino",
"coffeeName": "Cappucino"
}
进入全屏模式 退出全屏模式
测试自动缩放
AWS App Runner 自动为您的 App Runner 应用程序向上或向下扩展计算资源(实例)。自动扩展在传入流量很高时提供足够的请求处理,并在流量变慢时降低您的成本。您可以配置一些参数来调整服务的自动缩放行为。
-
设置 - 以下是您可以配置的内容:
-
Max concurrency – 实例处理的最大并发请求数。当并发请求数超过此配额时,App Runner 会扩展服务。
-
Max size – 您的服务可扩展到的最大实例数。最多此数量的实例正在为您的服务主动提供流量。
-
Min size – App Runner 为您的服务提供的最小实例数。服务始终至少具有此数量的预置实例。其中一些积极服务于交通。其余的(预置实例和非活动实例)作为具有成本效益的计算容量储备备用,可以快速激活。您为所有预置实例的内存使用付费。您只需为活动子集的 CPU 使用付费。
App Runner 在部署期间临时将预置实例的数量增加一倍,以保持相同的新旧代码容量。
[](https://res.cloudinary.com/practicaldev/image/fetch/s--POYYmJS8--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn. hashnode.com/res/hashnode/image/upload/v1635623015102/k7i_euFLO.png)
让我们用 100 个并发请求创建负载测试并检查实例数。
hey -z 1m -c 100 https://ptydisq8gp.us-east-1.awsapprunner.com/api/coffee
进入全屏模式 退出全屏模式
[](https://res.cloudinary.com/practicaldev/image/fetch/s--2pdm8W37--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://cdn. hashnode.com/res/hashnode/image/upload/v1635623027989/3vFqcPU32.png)
您可以看到“活动实例”计数根据自动缩放的并发请求而增加。
此演示的源代码仓库
ksivamuthu/aws-apprunner-coffee-shop
什么是 App Runner?
AWS App Runner 是一项完全托管的服务,让开发人员可以轻松快速地大规模部署容器化 Web 应用程序和 API,而无需具备基础设施经验。从您的源代码或容器映像开始。 App Runner 自动构建和部署 Web 应用程序,并通过加密对流量进行负载平衡。 App Runner 还可以自动扩展或缩减以满足您的流量需求。使用 App Runner,您有更多时间专注于应用程序,而不是考虑服务器或扩展。
应用程序运行器功能
App Runner 的特点是
-
自动缩放:随着需求的变化在可配置的最小和最大限制之间启动和停止。
-
负载均衡:该服务包括一个透明的、不可配置的负载均衡器。该 URL 可以指向自定义域。
-
SSL 和证书: 部署的服务将为具有 AWS 托管证书的应用程序提供 HTTPS 端点。这...
在 GitHub 上查看
结论
AWS App Runner 是一项完全托管的服务,可让开发人员轻松快速地大规模部署容器化 Web 应用程序和 API。它为当今的 Node.js 和 Python 运行时以及其他使用 Dockerfile 的运行时提供无缝的“代码到部署”工作流程。Copilot 还可以帮助您拥有自己的可定制管道,只需为运行 AWS AppRunner 的应用程序提供一些命令。
我在IndyAWS聚会上发表了关于 AWS App Runners 的演讲。本次会议涵盖
-
AWS App Runner 的功能,
-
AWS App Runner 解决的关键挑战,
-
如何配置 AWS App Runner 并将其与您的源代码控制集成以在几秒钟内部署您的代码
-
使用支持 AWS App Runner 的工具 AWS Copilot CLI 进行现场演示。
-
想要更深入的潜水?下载演讲的演示幻灯片和 youtube 视频。
我是 Siva - 在奥兰多的 Computer Enterprises Inc 担任高级软件架构师。我是一名 AWS 社区建设者、Auth0 大使,我将写很多关于云、容器、物联网和 Devops 的文章。如果您对其中任何一个感兴趣,请务必关注我(如果您还没有的话)。请关注我**@ksivamuthu** Twitter 或查看我的博客**blog.sivamuthukumar.com**!
更多推荐
所有评论(0)