Skip to main content

Package to automatize the configuration of an Flask API REST application inside the AWS API Gateway.

Project description

ct-api-gateway-deployer

This project aims to automatizate the deployment of an API inside the AWS API Gateway service.

Also, it provides a helper class to build a Flask API Rest application.

How it works

The Python package provided in the project will require two configurations files:

  • One file with the AWS configurations to access and deploy the API routes in the API Gateway service;
  • Other file with the API routes to be deployed.

When the project executable is executed in the command line, it will read the routes file to build an OpenAPI JSON object with the AWS specifications to be imported in the API Gateway.

The OpenAPI configuration created in the process is save as an YML file, that can be configured in the command line to be keep and stored in the disk after the process finish to run.

With the YML file, the process will access the AWS and upload the OpenAPI configuration to the API Gateway.

Also, this package provides the FlaskTools class, that allows build an Flask REST API application using the same routes file.

Installing

The Python package can be installing using the pip command:

pip install ct-api-gateway-deployer

Usage

Basic usage

python ct_api_gateway_deployer --aws-config=aws.config.json --routes=routes.json

Command line options

The options can be showed using the console helper:

python ct_api_gateway_deployer --help

Command option Required Description
--aws-config=string True Refers to the file's path with the configurations to access the AWS's services and create the API Gateway.
--routes=string True Refers to the file's path with the API's routes configurations to be deployed.
--keep-output-openapifile=boolean False Boolan flag to keep the OpenAPI file generated during the deployment process. By default, the file is erased in the end of the process.
--output-openapifile-path=string False Refers the path where will be write the OpenAPI file to be deployed in the API Gateway. By default, will create the file "swagger.yml" in the root path where the command was executed.
-h, --help False Show help message.

Amazon Web Services File Configuration

The --aws-config command option requires a JSON file that specify the configuration object to access the AWS environment. The JSON objects follows the bellow structure:

{
    "region": "",
    "aws": {
        "access_key_id": "",
        "secret_access_key": ""
    },
    "apiGateway": {
        "name": "",
        "rest_api_id": "",
        "baseEndpointURL": "",
        "stage": "",
        "basePath": ""
    }
}

Where:

  • Basic object
Attribute Type Description
region string AWS region identifier where the API Gateway will be deployed.
aws aws attribute AWS access configurations.
apiGateway apiGateway attribute AWS API Gateway configurations where the API will be deployed.
  • aws attribute
Attribute Type Description
access_key_id string AWS access key used to communicate with the API Gateway service.
secret_access_key string AWS secret access key used to communicate with the API Gateway service.
  • apiGateway attribute
Attribute Type Description
name string AWS API Gateway name.
rest_api_id string AWS API Gateway identifier.
stage string AWS API Gateway stage where the API will be deployed.
baseEndpointURL string Default endpoint for the API resources. Is overwritten by the url_prefix; configured in the routes files.
basePath string Default suffix path for the base endpoint. Is overwritten by the stage when the API is deployed by the API Gateway.

API Routes File Configuration

The --routes command option requires a JSON file that specify the configuration API's routes and configuration. Some attributes of the JSON object will configure directly the API Gateway environment specified in the --aws-config; also, other attributes, will be used to configure a Flask REST API through the FlaskTools class provided in the package.

The JSON object follows the bellow structure:

{
    "blueprint" : {
        "name": "",
        "url_prefix": "",
        "resources": [{
            "name": "",
            "flask": {
                "resourceModule": "",
                "resourceClass": "",
                "strictSlashes": false
            },
            "methods": [{
                "path": "",
                "cors": {
                  "enable": true, 
                  "removeDefaultResponseTemplates": true,
                  "allowHeaders": [""]
                },
                "queryParams": [{
                   "name": "",
                   "type": ""
                }],
                "actions": [{
                    "type": "",
                    "integration": "",
                    "proxyIntegration": true,
                    "vpcLink": "",
                    "authorization": ""
                }]
            }]
        }]
    }
}

Where:

  • Basic object
Attribute Type Description
blueprint blueprint attribute Blueprint configuration. The blueprint concept is explained in: http://flask.pocoo.org/docs/1.0/blueprints/#blueprints.
  • blueprint attribute
Attribute Type Description
name string Blueprint's name.
url_prefix string Blueprint's default url prefix for the API resources.
resources List of resource attribute The list of resources provided by blueprint in the API.
  • resource attribute
Attribute Type Description
name string The resource's name.
flask flask attribute The Flask's configurations that will be used by the FlaskTools class.
methods List of method attribute The list of methods that will be allowed for the parent resource.
  • flask attribute
Attribute Type Description
resourceModule string Module where the Flask resource class is available.
resourceClass string Nome of the Flask class that implements flask_restful.Resource.
strictSlashes boolean Boolean flag to ignore the slash character (/) at the end of the in Flask route.
  • method attribute
Attribute Type Description
path string Path to the method endpoint.
cors cors attribute Configurations for CORS in the API Gateway.
queryParams List of queryParams attribute Parameters in the routes query.
actions List of action attribute List of HTTP actions allowed in the method.
Attribute Type Description
enable boolean Flag to enable the CORS configuration.
removeDefaultResponseTemplates boolean Flag to remove the responseTemplate configuration. Used when the default HTTP application/json response is not wanted.
allowHeaders List of string List of attributes allowed in the request header.
  • queryParams attribute
Attribute Type Description
name string Name of the parameter.
type string Type of the parameter. Used in the parse for the route in the Flask REST API resource class.
  • action attribute
Attribute Type Description
type string Constant to identify the HTTP request action. Choose one from: GET or POST or PUT or DELETE.
integration string The integration connection type used in the API Gateway. Choose one from: VPC_LINK or INTERNET.
proxyIntegration boolean Flag to enable the integration of the received request on the API Gateway be replicated in the HTTP backend.
vpcLink string AWS VPC identifier. Just use when the integration parameter is configured as VPC_LINK; otherwise, set as null.
authorization string Adds verification for authorization headers. Choose from: AWS_IAM or null.

FlaskTools class

The FlaskTools Python class can be imported in an Flask REST API project to create an blueprint with the resources routes specified in the routes file.

The method signature is:

from flask import Flask

class FlaskTools:

    @staticmethod
    def add_resources(application: Flask, router_file_path: str) -> None

Where:

  • application: is a Flask object where the Blueprint that contains the resources and routes will be appended.
  • router_file_path: is the path to the JSON file with the routes' configurations.

Links

Feedback

Every feedback is welcome. Bugs reports, feature request, comments and others can be send directly to the contributors' email.

Contributors

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ct_api_gateway_deployer-1.1.4.tar.gz (11.2 kB view details)

Uploaded Source

File details

Details for the file ct_api_gateway_deployer-1.1.4.tar.gz.

File metadata

  • Download URL: ct_api_gateway_deployer-1.1.4.tar.gz
  • Upload date:
  • Size: 11.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/50.3.0 requests-toolbelt/0.9.1 tqdm/4.49.0 CPython/3.6.2

File hashes

Hashes for ct_api_gateway_deployer-1.1.4.tar.gz
Algorithm Hash digest
SHA256 9ddd65da3552ffd5b7a2adceea7b7c123e65f2769131c4117cdbbadd790e67d2
MD5 ed672c5eddd55c368e7cfe1b09c3251e
BLAKE2b-256 cb4f4d80a38302cb29ba22945dcd849772c450789d38d4c8b0ab49be14dd7f91

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page