Skip to main content

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 b-aws-websocket-api

Or directly install it through source.

pip install .

Usage & 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.

Setting environment

Before running tests set environment variables:

  • AWS_ACCESS_KEY_ID
  • AWS_SECRET_ACCESS_KEY
  • AWS_DEFAULT_REGION

Or:

  • AWS_PROFILE
  • AWS_DEFAULT_REGION

Running tests

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

pytest b_aws_websocket_api_test

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

2.0.0

  • Upgrade 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.

1.1.0

  • Add previously removed websocket http url.

1.0.0

  • Change ws connections endpoint url and arn function names.

0.0.12

  • Expose function in lambda integration.

0.0.11

  • Update docker image for pipelines.
  • Add hash properties.
  • Make ws deployment more custom.
  • Update tests.

0.0.9

  • Update readme documentation regarding testing.

0.0.8

  • Add CI/CD pipelines.
  • Update History file.
  • Refactor testing.
  • Update Readme file.

0.0.7

  • Fix stage log group name.

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.

Metadata

Release files for b-aws-websocket-api 2.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for b-aws-websocket-api 2.0.0
File Size Uploaded Provenance
b_aws_websocket_api-2.0.0.tar.gz 16.8 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for b-aws-websocket-api 2.0.0
File Interpreter ABI Platform
b_aws_websocket_api-2.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 35.5 kB

Release files / b_aws_websocket_api-2.0.0.tar.gz

Download URL b_aws_websocket_api-2.0.0.tar.gz
Size 16.8 kB
Tags Source
SHA-256 checksum
How to use checksums
650883dfe0ca76fdf1cafef1a81012566a45172af6eb9b8db45b2b814e882e0e
BLAKE2b-256 checksum
How to use checksums
08d29bc765d48414a952e69ce6ccc334801350f763528d14508816b5eb9eeb03
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Release files / b_aws_websocket_api-2.0.0-py3-none-any.whl

Download URL b_aws_websocket_api-2.0.0-py3-none-any.whl
Size 18.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
210417867d6e9e81e601190aac70b0aaf2c48ad72f47b53fa3cebec7dfe0f889
BLAKE2b-256 checksum
How to use checksums
e292deccfe6cc33d0051e2a88965b9b9d3409958f4fa24ef66b4650a43d85682
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.2 CPython/3.10.12

Security for b-aws-websocket-api 2.0.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page