Package: b-aws-websocket-api
B.AwsWebsocketApiAn AWS CDK based python library that helps you to create websocket based APIs.DescriptionCreating 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.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 technologyPython 3AWS CDKAWS CloudFormationAWS API GatewayWebsocketsAssumptionsThe 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 sourcesRead more about AWS API Gateway:https://docs.aws.amazon.com/apigateway/latest/developerguide/welcome.html.Read more about AWS websocket API:https://docs.aws.amazon.com/apigateway/latest/developerguide/apigateway-websocket-api-overview.htmlInstallThe project is built and uploaded to PyPi. Install it by using pip.pip install b-aws-websocket-apiOr directly install it through source.pip install .Usage & ExamplesCreate a stack to hold your resources:fromaws_cdk.coreimportStackstack=Stack(...)Create an API:fromb_aws_websocket_api.ws_apiimportWsApiapi=WsApi(scope=stack,id='TestWsApi',description='Test description.',name='TestWsApi',route_selection_expression='$request.body.action',)Create a stage (usually it is calledprod):fromb_aws_websocket_api.ws_stageimportWsStagestage=WsStage(scope=stack,id='TestStage',ws_api=api,stage_name='prod',auto_deploy=True,)Create a lambda function to handle incoming requests (frames):fromb_aws_websocket_api.ws_functionimportWsFunctionfromaws_cdk.aws_lambdaimportCode,Runtimebackend=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):fromb_aws_websocket_api.ws_lambda_integrationimportWsLambdaIntegrationintegration=WsLambdaIntegration(scope=stack,id='TestIntegration',integration_name='TestIntegration',ws_api=api,function=backend)Create a custom route backed by a lambda function:fromb_aws_websocket_api.ws_routeimportWsRouteroute=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:fromb_aws_websocket_api.ws_deploymentimportWsDeploymentdeployment=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 executecdk deploy *and enjoy your new websocket API!TestingThe 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 environmentBefore running tests set environment variables:AWS_ACCESS_KEY_IDAWS_SECRET_ACCESS_KEYAWS_DEFAULT_REGIONOr:AWS_PROFILEAWS_DEFAULT_REGIONRunning testsThen run tests from a root directory withpytestpython testing library:pytest b_aws_websocket_api_testNote that integration tests usually take a while to complete (from 5 to 30
minutes on average).ContributionFound 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 history2.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.1.1.0Add previously removed websocket http url.1.0.0Change ws connections endpoint url and arn function names.0.0.12Expose function in lambda integration.0.0.11Update docker image for pipelines.Add hash properties.Make ws deployment more custom.Update tests.0.0.9Update readme documentation regarding testing.0.0.8Add CI/CD pipelines.Update History file.Refactor testing.Update Readme file.0.0.7Fix stage log group name.0.0.6Move imports in testing manager.0.0.5Fix readme.0.0.4Update name.0.0.3Remove idea files.0.0.2Added project files. Added tests.0.0.1Initial build. No project files, just a semi-empty project.