Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Automating Deployment of AWS API Gateway Stage

How would I go about automating the deployment of an AWS API Gateway via a Python script using Boto3? For example, if I have created a stage named "V1" in the AWS Console for API Gateway, how would I write a script to deploy that stage ("V1")?

The current process involves deploying the stage manually from the AWS Console and is not scriptable. For purposes of automation, I would like to have a script to do the same.

Consulting the Boto3 documentation, I see there's a method for creating a stage (http://boto3.readthedocs.io/en/latest/reference/services/apigateway.html#APIGateway.Client.create_stage), but none for deploying one.

like image 748
Michael G. Morey Avatar asked Jan 28 '26 20:01

Michael G. Morey


2 Answers

If you want to stick with deploying via specific boto3 API calls, then you want to follow this rough sequence of boto3 API calls:

  • Use get_rest_apis to retrieve the API ID.
  • Possibly check if it's deployed already using get_deployments.
  • Use create_deployment to create the deployment. Use the stageName parameter to specify the stage to create.
  • Consider using create_base_path_mapping if needed.
  • Also consider using update_stage if you need to turn on something like logging.
like image 83
dmulter Avatar answered Jan 30 '26 11:01

dmulter


To deploy a typical (API Gateway/Lambda) I would recommend AWS SAM, instead of writing own code.

It even supports Swagger and you can define your stages in SAM definition files.

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

Deployment is easily integrable into CD pipelines using AWS CLI

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

See also: Deploying Lambda-based Applications

like image 40
H6. Avatar answered Jan 30 '26 12:01

H6.



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!