Open convexset opened 2 years ago
I see how this might be troublesome since this seems to be configured within the WebsocketEventSourceHandler
at...
https://github.com/aws/chalice/blob/master/chalice/app.py#L1768
... which is super easy and convenient. However, all other request contexts do not include this.
I would say that an automatically generated list of the last deployed resources on app
would be a viable solution. That is, the content of .chalice/deployed/MYSTAGE.json
. This means that a deploy would be needed, followed by another if there is a non-zero "diff".
This architectural change is troublesome, but does open up additional possibilities. How about CFN/CDK with exports available to the app?
Additionally, as it is, this fails with event sources. My workaround is "doing the regular thing" of setting up my own send method based on the aforementioned deployed data:
import boto3
from chalice import WebsocketDisconnectedError
# after each deployment, data from .chalice/deployed/ is written to...
from . import chalice_exports
STAGE = os.environ.get('STAGE', 'dev')
current_chalice_exports = chalice_exports.exports.get(STAGE)
apigw_management_client = None
if current_chalice_exports is not None:
websocket_api_domain = current_chalice_exports['websocket_api']['websocket_api_url'].split('/')[2]
websocket_api_stage = current_chalice_exports['websocket_api']['websocket_api_url'].split('/')[3]
log.info(f'Configuring WebSockets: domain={websocket_api_domain}, stage={websocket_api_stage}')
apigw_management_client = boto3.client(
'apigatewaymanagementapi',
endpoint_url=f'https://{websocket_api_domain}/{websocket_api_stage}'
)
else:
log.error(f'Information to configure WebSockets not available')
def _send(connection_id, serialized_data):
# https://github.com/aws/chalice/blob/master/chalice/app.py#L687
if apigw_management_client is None:
raise Exception('websocket_api not yet deployed')
try:
result = apigw_management_client.post_to_connection(
ConnectionId=connection_id,
Data=serialized_data,
)
except apigw_management_client.exceptions.GoneException:
raise WebsocketDisconnectedError(connection_id)
return result
It seems that WebSockets support only available within WebSockets methods. From other application code, one gets:
This may seem alright, but application code may need to communicate with clients connected over WebSockets.
I don't think it's too much to ask to have this enabled by default.
To work around this, what I've done is to have a script put my Chalice exports into
chalicelib/chalice_exports.py
which looks like:(I actually use Chalice with Amplify, so this had to be done to provide the same information to the front end.)
Then I have somewhere in my application code:
This is called from
app.py
as such:(Sorry, a little lazy to edit this.)