Open coding-to-music opened 3 years ago
930 http GET https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/objects/foo3
931 http GET https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/objects/foo34
932 echo '{"foo": "bar"}' | http PUT https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/objects/foo34
933 http GET https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/objects/foo34
934 chalice deploy
935 http 'https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/introspect?query1=value1&query2=value2' 'X-TestHeader: Foo'
936 chalice deploy
937 http POST https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/ states=WA states=CA --debug
938 http --form POST https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/ states=WA states=CA --debug
939 http --form POST https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/ states=WA states=CA
940 chalice deploy
941 http --form POST https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/ states=WA states=CA
942 chalice deploy
943 http https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/
944 chalice deploy
945 http https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/
946 chalice deploy
947 http https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/
948 http --form POST https://pyuve2v4bh.execute-api.us-east-1.amazonaws.com/api/ states=WA states=CA
https://aws.github.io/chalice/tutorials/cdk.html
https://github.com/coding-to-music/chalice-learning-examples
https://aws.github.io/chalice/tutorials/wsecho.html
An echo server is a simple server that echos any message it receives back to the client that sent it.
$ pip install -U chalice
$ chalice new-project echo-server
$ cd echo-server
Our Chalice application will need boto3 as a dependency for both API Gateway to send websocket messages. Let’s add a boto3 to the requirements.txt file:
$ echo "boto3>=1.9.91" > requirements.txt
Now that the requirement has been added. Let’s install it locally since our next script will need it as well:
$ pip install -r requirements.txt
Next replace the contents of the app.py file with the code below.
from boto3.session import Session
from chalice import Chalice
from chalice import WebsocketDisconnectedError
app = Chalice(app_name="echo-server")
app.websocket_api.session = Session()
app.experimental_feature_flags.update([
'WEBSOCKETS'
])
@app.on_ws_message()
def message(event):
try:
app.websocket_api.send(
connection_id=event.connection_id,
message=event.body,
)
except WebsocketDisconnectedError as e:
pass # Disconnected so we can't send the message back.```
root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/chalice-learning/echo-server# chalice deploy
Creating deployment package.
Creating IAM role: echo-server-dev
Creating lambda function: echo-server-dev-websocket_message
Creating websocket api: echo-server-dev-websocket-api
Resources deployed:
- Lambda ARN: arn:aws:lambda:us-east-1:708090526287:function:echo-server-dev-websocket_message
- Websocket API URL: wss://ety3nz5cfh.execute-api.us-east-1.amazonaws.com/api/
root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/chalice-learning/echo-server# pip install websocket-client
Collecting websocket-client
Downloading websocket_client-1.1.0-py2.py3-none-any.whl (68 kB)
|████████████████████████████████| 68 kB 1.9 MB/s
Installing collected packages: websocket-client
Successfully installed websocket-client-1.1.0
After deploying the Chalice app the output will contain a URL for connecting to the websocket API labeled: - Websocket API URL:. The websocket-client package installs a command line tool called wsdump.py which can be used to test websocket echo server:
root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/chalice-learning/echo-server# wsdump.py wss://ety3nz5cfh.execute-api.us-east-1.amazonaws.com/api/
Press Ctrl+C to quit
> foo
< foo
> bar
< bar
> abc
< abc
> ls -l
< ls -l
> root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/chalice-learning/echo-server# chalice logs -n websocket_message
root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/chalice-learning/echo-server#
root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/chalice-learning/echo-server# chalice delete
Deleting Websocket API: ety3nz5cfh
Deleting function: arn:aws:lambda:us-east-1:708090526287:function:echo-server-dev-websocket_message
Deleting IAM role: echo-server-dev
root@docker-ubuntu-s-1vcpu-2gb-nyc1-01:~/ap/chalice-learning/echo-server#
Chalice REST API Tutorial
https://aws.github.io/chalice/tutorials/basicrestapi.html