Package: b-cfn-api-v2
B.CfnApiV2An API Gateway resource that adds convenient functionality over traditionalCfnApiresource.
It lets you easily enable authorization, stages, and CDNs.DescriptionEssentially this resource is a wrapper resource foraws_apigatewayv2module'sCfnApi. Meaning,
that you can easily swapCfnApiand thisApiresource with no major impact. But why would you
want to switch a traditionalCfnApito this one. Mainly these convenient features:Easy to addCloudFrontdistribution on top of the API (enabling CDN).Easy to enableStageand attached to the api.Easy to add authorization withUserPoolAuthorizer&ApiKeyAuthorizer.RemarksBiomapasaims 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 technologyPython3AWS CDKAWS LambdaAWS API GatewayAWS CloudFrontAWS User Pool authorizationAssumptionsThis 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 sourcesAWS CDK:https://docs.aws.amazon.com/cdk/api/latest/docs/aws-construct-library.htmlAWS CloudFormation:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/Welcome.htmlAWS API Gateway:https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.htmlAWS API Gateway V2:https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-resource-apigatewayv2-api.htmlCustom User Pool Authorizer:https://github.com/Biomapas/B.CfnCustomUserPoolAuthorizerCustom api key authorizer:https://github.com/Biomapas/B.CfnCustomApiKeyAuthorizerCustom user pool authorizer:https://github.com/Biomapas/B.CfnCustomUserPoolAuthorizerInstallBefore installing this library, ensure you have these tools setup:Python / PipAWS CDKTo install this project from source run:pip install .Or you can install it from a PyPi repository:pip install b-cfn-api-v2Usage & ExamplesThe traditional way of creating an API looks something like this:fromaws_cdk.aws_apigatewayv2importCfnApiCfnApi(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 resourceApiis exactly the same:(It works sinceApiis a pure wrapper ofCfnApiresource.)fromb_cfn_api_v2.apiimportApiApi(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 thisApiresource:Easy to enable default stage.fromb_cfn_api_v2.apiimportApiapi=Api(...)api.enable_default_stage('dev')Easy to enable authorization.fromb_cfn_api_v2.apiimportApifromb_cfn_custom_userpool_authorizer.config.user_pool_configimportUserPoolConfigapi=Api(...)# Your authorized endpoint will require `Authorization`# supplied in headers.# Read more:# https://github.com/Biomapas/B.CfnCustomUserPoolAuthorizerapi.enable_user_pool_authorizer(UserPoolConfig(user_pool_id='id',user_pool_region='region',user_pool_client_id='client'))# Your authorized endpoint will require `ApiKey` and `ApiSecret`# supplied in headers.# Read more:# https://github.com/Biomapas/B.CfnCustomApiKeyAuthorizerapi.enable_api_key_authorizer()Easy to enable CDN.fromb_cfn_api_v2.apiimportApifromaws_cdk.aws_cloudfrontimportCachePolicyapi=Api(...)api.enable_cdn(default_behavior_cache_policy=CachePolicy.CACHING_OPTIMIZED)TestingThis package has integration tests based onpytest.
To run tests simply run:pytest b_cfn_api_v2_test/integration/testsContributionFound 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 history5.0.0Upgrade CDK support from v1 to v2.Upgrade GitHub pipelines checkout version from v2 to v3.Set GitHub pipelines node version 18.Set GitHub pipelines python version 3.10.4.0.0Fix origin paths between pure api or its cloudfront distribution.
Both should require stage name to be entered as a part of a URL.
This is a breaking change.3.1.0Enable easy stage logging.3.0.0Upgrade breaking dependencies.2.0.1Make dependencies range more loose with maximum version not reaching2.0.0.2.0.0Use the newest2.0.0custom api keys authorizer which now hashes
api secrets and is no longer compatible with previous versions.1.1.1Update readme.1.1.0Implement api keys custom authorizer (https://github.com/Biomapas/B.CfnCustomApiKeyAuthorizer).1.0.0Fully documented, tested, and working version release.0.0.1Initial build.