如何使用Boto3通过Python脚本自动部署AWS API网关?例如,如果我在AWS Portal的AWS Gateway中创建了一个名为“V1”的阶段,我该如何编写脚本来部署该阶段(“V1”)?
当前流程涉及从AWS控制台手动部署阶段,并且无法编写脚本。出于自动化的目的,我希望有一个脚本来做同样的事情。
咨询Boto3文档,我看到有一种创建阶段(http://boto3.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.create_stage)的方法,但没有用于部署阶段。
答案 0 :(得分:2)
要部署典型的(API Gateway / Lambda),我建议使用AWS SAM,而不是编写自己的代码。
它甚至支持Swagger,您可以在SAM定义文件中定义阶段。
e.g。
ApiGatewayApi:
Type: AWS::Serverless::Api
Properties:
StageName: v1
CacheClusterEnabled: true
CacheClusterSize: "0.5"
DefinitionUri: "swagger.yaml"
Variables:
[...]
[...]
MyFunction:
Type: AWS::Serverless::Function
Properties:
Handler: ratings.handler
Runtime: python3.6
Events:
Api:
Type: Api
Properties:
Path: /here
Method: get
RestApiId: !Ref ApiGatewayApi
使用AWS CLI轻松将部署集成到CD管道
aws cloudformation package \
--template-file path/example.yaml \
--output-template-file serverless-output.yaml \
--s3-bucket s3-bucket-name
aws cloudformation deploy \
--template-file serverless-output.yaml \
--stack-name new-stack-name \
--capabilities CAPABILITY_IAM
答案 1 :(得分:2)
If you want to stick with deploying via specific boto3 API calls, then you want to follow this rough sequence of boto3 API calls:
get_rest_apis
to retrieve the API ID.get_deployments
.create_deployment
to create the deployment. Use the stageName
parameter to specify the stage to create.create_base_path_mapping
if needed.update_stage
if you need to turn on something like logging.答案 2 :(得分:1)
是的,您目前通过AWS浏览器控制台手动创建和部署apis的方式不是很容易编写脚本,但您可以使用AWS cli在控制台中点击几乎任何内容。听起来像你想要一个自动化的CI / CD管道。一旦你找出了你用aws cli运行的命令,只需将它们添加到你的CI管道中就可以了。
但实际上,这是一种更简单的方法。转到AWS Codestar。点击"创建新项目"并检查" Web服务"," Python"和" AWS Lambda"。截至今天,只有一个适合所有三个的Codestar模板,因此请选择一个。这将构建一个完整的CI / CD管道(AWS CodePipeline)与一个开发环境,连接到一个git项目。我认为这对您来说是一个好方法,因此您可以利用开发自动化部署工具,而无需担心在主项目之上设置和维护它。