gpt4 book ai didi

amazon-web-services - 如何在AWS codepipeline中自动部署api网关

转载 作者:行者123 更新时间:2023-12-03 07:32:33 26 4
gpt4 key购买 nike

目前我可以通过推送到 github 来部署 lambda。我还自动部署 lambda,但这只是因为 api 网关是 lambda yaml 文件中的事件

AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: Identifies paragraphs in documents and links to the law
Resources:
LambdaParagraphLinker:
Type: 'AWS::Serverless::Function'
Properties:
Handler: LambdaParagraphLinker.lambda_handler
Runtime: python3.6
CodeUri: ./
Description: Identifies paragraphs in documents and links to the
law
MemorySize: 512
Timeout: 10
Events:
Api:
Type: Api
Properties:
Path: /LambdaParagraphLinker
Method: ANY

如何使用 swagger 文件部署 API 网关?

最佳答案

毫无疑问,在 codepipeline 中执行此操作的最佳方法是使用 https://serverless.com/框架。这取代了我以前使用过的所有 super 复杂的黑客工作和解决方法。在我看来,没那么复杂。

创建一个codepipeline,将其链接到src和一个codebuild项目,设置一些权限,完成。

//无服务器.yml

service: my-api
provider:
name: aws
runtime: python2.7

functions:
hello:
handler: handler.hello
events:
- http:
path: api/v1/message
method: post

//buildspec.yml

version: 0.2
phases:
install:
commands:
#BUILD
- sudo apt-get update -y
build:
commands:
- echo $environment
- serverless package --stage $environment --region us-east-1
- serverless deploy --stage $environment --region us-east-1

或者通过执行以下选项之一来折磨自己......

您可以在代码管道内的 cloudformation 中执行此操作。从 gatewayapi 控制台导出 swagger 规范并将其放置在 cloudformation 模板中。

AWSTemplateFormatVersion: '2010-09-09'
Resources:
PlayersAPI:
Type: AWS::ApiGateway::RestApi
Properties:
Name: MyApi
Description: API Description
Body:
"SWAGGER HERE"

将其连接到 lambda 有点麻烦,但我可以描述这些步骤。首先创建一个包含源代码、构建和部署步骤的 codepipeline 项目。

  • src 应该是来自 github 或 codecommit 的标准
  • build 应该输出一个 zip 文件并使用 buildspec.yml 像这样的东西......

//buildspec.yml

version: 0.1
phases:
install:
commands:
#BUILD
- zip -r lambda.zip . -x *.git*
artifacts:
files:
- '**/*.zip'
- '**/*.yml'
discard-paths: no

让构建步骤导出一个工件 MyAppBuild(或任何您想要的名称)

最后的管道步骤是通过控制台在此存储库中创建 lambda 函数作为独立函数(可重用): https://github.com/tkntobfrk/codepipeline-lambda-s3

此 lambda 函数下载管道工件/压缩的 lambda 函数并使用 boto 更新它。

在这些步骤之后,您可以添加另一个步骤作为 cloudformation 部署步骤。将其连接到您刚刚部署的 lambda 函数。

如果您正在处理多个环境,您可以为每个环境创建 lambda 函数和 gatewayapi cloudformation 模板,然后按顺序运行它们。

  • 第 1 阶段:src
  • 第 2 阶段:构建
  • 第 3 阶段:部署 lambda 测试、部署网关 API cloudformation 测试
  • 第 4 阶段:验证测试
  • 第 5 阶段:部署 lambda prod、部署 gateway api cloudformation prod

像这样直接使用 AWS 无服务器也可以。但是,您需要为 uri 使用标准工件位置。 API 的 DefinitionUri:可以是从 gatewayapi 控制台导出的 swagger。

//cloudformation.yml

AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Resources:
MySimpleFunction:
Type: AWS::Serverless::Function
Properties:
Handler: app.lambda_handler
Runtime: python2.7
CodeUri: s3://somebucket/somezip.zip
MyAPI:
Type: AWS::Serverless::Api
Properties:
StageName: prod
DefinitionUri: s3://somebucket/somezip.zip

关于amazon-web-services - 如何在AWS codepipeline中自动部署api网关,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44730419/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com