Skip to main content

AWS CDK package that helps creating web socket APIs.

Project description

B.AwsWebsocketApi

An AWS CDK based python library that helps you to create websocket based APIs.

Description

Creating APIs in AWS is pretty challenging. You have to create stages, deployments, apis, resources, methods, etc. One might argue that creating a websocket based API is even harder. This AWS CDK based python library tries to solve this inconvenience by offering resources and examples on how to create a websocket API the correct way. We have spent countless hours to perfect it but still there will always be some "gotchas" in AWS environment.

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

  • Python 3
  • AWS CDK
  • AWS CloudFormation
  • AWS API Gateway
  • Websockets

Assumptions

The project assumes the following:

  • You have basic-good knowledge in python programming.
  • You have basic-good knowledge in AWS.
  • You have basic knowledge in websockets.

Useful sources

Install

The project is built and uploaded to PyPi. Install it by using pip.

pip install biomapas-aws-api-ws

Or directly install it through source.

pip install .

Examples

Create a stack to hold your resources:

from aws_cdk.core import Stack
stack = Stack(...)

Create an API:

from b_aws_websocket_api.ws_api import WsApi
api = WsApi(
    scope=stack,
    id='TestWsApi',
    description='Test description.',
    name='TestWsApi',
    route_selection_expression='$request.body.action',
)

Create a stage (usually it is called prod):

from b_aws_websocket_api.ws_stage import WsStage
stage = WsStage(
    scope=stack,
    id='TestStage',
    ws_api=api,
    stage_name='prod',
    auto_deploy=True,
)

Create a lambda function to handle incoming requests (frames):

from b_aws_websocket_api.ws_function import WsFunction
from aws_cdk.aws_lambda import Code, Runtime
backend = WsFunction(
    scope=stack,
    id='TestFunction',
    function_name='TestFunction',
    code=Code.from_inline(
        'def handler(*args, **kwargs):\n'
        '    return {\n'
        '        "isBase64Encoded": False,\n'
        '        "statusCode": 200,\n'
        '        "headers": {},\n'
        '        "body": "{\\"message\\": \\"success\\"}"\n'
        '    }\n'
    ),
    handler='index.handler',
    runtime=Runtime.PYTHON_3_6,
)

Create a lambda integration (later will be needed for a route):

from b_aws_websocket_api.ws_lambda_integration import WsLambdaIntegration
integration = WsLambdaIntegration(
    scope=stack,
    id='TestIntegration',
    integration_name='TestIntegration',
    ws_api=api,
    function=backend
)

Create a custom route backed by a lambda function:

from b_aws_websocket_api.ws_route import WsRoute
route = WsRoute(
    scope=stack,
    id='TestRoute',
    ws_api=api,
    route_key='test',
    authorization_type='NONE',
    route_response_selection_expression='$default',
    target=f'integrations/{integration.ref}',
)

Finally deploy the API:

from  b_aws_websocket_api.ws_deployment import WsDeployment
deployment = WsDeployment(
    scope=stack,
    id='TestDeployment',
    ws_stage=stage
)

And don't forget to solve dependencies for the resources!

deployment.node.add_dependency(route)
deployment.node.add_dependency(stage)

Now execute cdk deploy * and enjoy your new websocket API!

Testing

The project has tests that can be run. Note, that tests are integration tests inherently because they test how resources are created in AWS environment. Since resources are created and tested in AWS you are subject for all the applicable charges while tests are being run.

Before running tests set an environment variable BIOMAPAS_AWS_API_WS_TEST_PROFILE. This environment variable specifies an aws account to use for deployment and testing. Usually this profile is called default when created with aws configure cli command.

Set on Windows:

set BIOMAPAS_AWS_API_WS_TEST_PROFILE=default

Set on Linux:

export BIOMAPAS_AWS_API_WS_TEST_PROFILE=default

Then run tests from a root directory with pytest python testing library:

pytest

Note that integration tests usually take a while to complete (from 5 to 30 minutes on average).

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

0.0.6

Move imports in testing manager.

0.0.5

Fix readme.

0.0.4

Update name.

0.0.3

Remove idea files.

0.0.2

Added project files. Added tests.

0.0.1

Initial build. No project files, just a semi-empty project.

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_aws_websocket_api-0.0.6.tar.gz (16.7 kB view details)

Uploaded Source

Built Distribution

b_aws_websocket_api-0.0.6-py3-none-any.whl (18.1 kB view details)

Uploaded Python 3

File details

Details for the file b_aws_websocket_api-0.0.6.tar.gz.

File metadata

  • Download URL: b_aws_websocket_api-0.0.6.tar.gz
  • Upload date:
  • Size: 16.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for b_aws_websocket_api-0.0.6.tar.gz
Algorithm Hash digest
SHA256 81ae2a21a02525a9b69aee51c31e010d7e7a03bfc1e7ffd59e42db58a012092c
MD5 c4be217f7043b8354436af739e78385b
BLAKE2b-256 4247feeb61bf9f2a539b40177092c0767c6b9eafb400c3f462149348b2fed9b0

See more details on using hashes here.

File details

Details for the file b_aws_websocket_api-0.0.6-py3-none-any.whl.

File metadata

  • Download URL: b_aws_websocket_api-0.0.6-py3-none-any.whl
  • Upload date:
  • Size: 18.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.1.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.8.5

File hashes

Hashes for b_aws_websocket_api-0.0.6-py3-none-any.whl
Algorithm Hash digest
SHA256 61949df80420959c3d0b89cd725f2f3c717befb049b6d1406c4401a05dd603e4
MD5 3c1e8ecff611919e01d4cb754418459f
BLAKE2b-256 c822063a233e2d9db3fb1da01140f2bb0719e425fd91907bdefcda5998f5c9a4

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