Michael G. Morey
Michael G. Morey

Reputation: 360

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.

Upvotes: 3

Views: 6310

Answers (3)

dmulter
dmulter

Reputation: 2758

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

Upvotes: 4

H6_
H6_

Reputation: 32808

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

Upvotes: 3

Jim
Jim

Reputation: 4172

Yes, your current way of creating and deploying the apis manually through the AWS browser console is not very scriptable, but pretty much anything you can click in the console can be done with the AWS cli. It sounds to me like you want an automated CI / CD pipeline. Once you figure out what commands you would run with the aws cli, just add them to your CI pipeline and you should be good to go.

But actually, there's an even easier way. Go to AWS Codestar. Click "create new project" and check "Web Service", "Python", and "AWS Lambda". As of today there's only one Codestar template that fits all three, so choose that one. This will scaffold a full CI / CD pipeline (AWS CodePipeline) with one dev environment, hooked up to a git project. I think would be a good way for you so you can leverage the dev-opsy automated deployment stuff without having to worry about setting up and maintaining this on top of your main project.

Upvotes: 1

Related Questions