Skip to main content

Convenient wrapper around CfnApi.

Project description

B.CfnApiV2

Pipeline

An API Gateway resource that adds convenient functionality over traditional CfnApi resource. It lets you easily enable authorization, stages, and CDNs.

Description

Essentially this resource is a wrapper resource for aws_apigatewayv2 module's CfnApi. Meaning, that you can easily swap CfnApi and this Api resource with no major impact. But why would you want to switch a traditional CfnApi to this one. Mainly these convenient features:

  • Easy to add CloudFront distribution on top of the API (enabling CDN).
  • Easy to enable Stage and attached to the api.
  • Easy to add authorization with UserPoolAuthorizer.

Remarks

Biomapas aims to modernise life-science industry by sharing its IT knowledge with other companies and the community. This is an open source library intended to be used by anyone. Improvements and pull requests are welcome.

Related technology

  • Python3
  • AWS CDK
  • AWS Lambda
  • AWS API Gateway
  • AWS CloudFront
  • AWS User Pool authorization

Assumptions

This project assumes you know what Lambda functions are and how code is being shared between them (Lambda layers).

  • Excellent knowledge in IaaC (Infrastructure as a Code) principles.
  • Excellent knowledge in Lambda functions and API Gateway service.
  • Good experience in AWS CDK and AWS CloudFormation.

Useful sources

Install

Before installing this library, ensure you have these tools setup:

  • Python / Pip
  • AWS CDK

To install this project from source run:

pip install .

Or you can install it from a PyPi repository:

pip install b-cfn-api-v2

Usage & Examples

The traditional way of creating an API looks something like this:

from aws_cdk.aws_apigatewayv2 import CfnApi

CfnApi(
    scope=Stack(),
    id='Api',
    name='Api',
    description='Sample description.',
    protocol_type='HTTP',
    cors_configuration=CfnApi.CorsProperty(
        allow_methods=['GET', 'PUT', 'POST', 'OPTIONS', 'DELETE'],
        allow_origins=['*'],
        allow_headers=[
            'Content-Type',
            'Authorization'
        ],
        max_age=300
    )
)

To create our resource Api is exactly the same:
(It works since Api is a pure wrapper of CfnApi resource.)

from b_cfn_api_v2.api import Api

Api(
    scope=Stack(),
    id='Api',
    name='Api',
    description='Sample description.',
    protocol_type='HTTP',
    cors_configuration=Api.CorsProperty(
        allow_methods=['GET', 'PUT', 'POST', 'OPTIONS', 'DELETE'],
        allow_origins=['*'],
        allow_headers=[
            'Content-Type',
            'Authorization'
        ],
        max_age=300
    )
)

Three main advantages of this Api resource:

  • Easy to enable default stage.
from b_cfn_api_v2.api import Api

api = Api(...)
api.enable_default_stage('dev')
  • Easy to enable authorization.
from b_cfn_api_v2.api import Api
from b_cfn_custom_userpool_authorizer.config.user_pool_config import UserPoolConfig

api = Api(...)
api.enable_authorizer(UserPoolConfig(
    user_pool_id='id',
    user_pool_region='region',
    user_pool_client_id='client'
))
  • Easy to enable CDN.
from b_cfn_api_v2.api import Api
from aws_cdk.aws_cloudfront import CachePolicy

api = Api(...)
api.enable_cdn(default_behavior_cache_policy=CachePolicy.CACHING_OPTIMIZED)

Testing

This package has integration tests based on pytest. To run tests simply run:

pytest b_cfn_api_v2_test/integration/tests

Contribution

Found a bug? Want to add or suggest a new feature? Contributions of any kind are gladly welcome. You may contact us directly, create a pull-request or an issue in github platform. Lets modernize the world together.

Release history

1.1.0

1.0.0

  • Fully documented, tested, and working version release.

0.0.1

  • Initial build.

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

b_cfn_api_v2-1.1.0.tar.gz (14.2 kB view details)

Uploaded Source

Built Distribution

b_cfn_api_v2-1.1.0-py3-none-any.whl (20.5 kB view details)

Uploaded Python 3

File details

Details for the file b_cfn_api_v2-1.1.0.tar.gz.

File metadata

  • Download URL: b_cfn_api_v2-1.1.0.tar.gz
  • Upload date:
  • Size: 14.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for b_cfn_api_v2-1.1.0.tar.gz
Algorithm Hash digest
SHA256 d55e361dffaf82a4405d942e17491f933d3ea1703709e2022209cc7759f89279
MD5 0c0c339b37ccedf8c1e740003532a866
BLAKE2b-256 32d1a356bbc044dff14e14eced18b1b8f0d3f36498b45598e3c43b753e061041

See more details on using hashes here.

Provenance

File details

Details for the file b_cfn_api_v2-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: b_cfn_api_v2-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.22.0 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/18.0.1 rfc3986/2.0.0 colorama/0.4.3 CPython/3.8.10

File hashes

Hashes for b_cfn_api_v2-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c9d02d4cd1a71cb0669f74fca0bfc619e6693634efa153ff52bc7da428ad1ef
MD5 e22f98d823c8d7819c00e0e17ee97697
BLAKE2b-256 790942484a814ad8e6f5e6f3e933dcf3680d404fa7fea6e3ee48352fb0c0329d

See more details on using hashes here.

Provenance

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