-
Notifications
You must be signed in to change notification settings - Fork 0
/
create_websocket_api.sh
77 lines (66 loc) · 2.17 KB
/
create_websocket_api.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#!/bin/bash
set -e
# Define variables
stage_name="local"
region="us-east-1"
api_name="Gateway"
service_url="YOUR_URL"
# Create API and extract API ID
create_api_and_extract_id() {
echo "Creating WebSocket API..."
api=$(aws apigatewayv2 create-api \
--region $region \
--name $api_name \
--protocol-type WEBSOCKET \
--route-selection-expression '$request.body.action')
api_id=$(echo "$api" | jq -r '.ApiId')
echo "WebSocket API ID: $api_id"
}
# Create integration and return its ID
create_integration() {
local route=$1
local method=$2
local uri=$3
integration=$(aws apigatewayv2 create-integration \
--api-id $api_id \
--integration-type HTTP_PROXY \
--integration-method $method \
--integration-uri "$uri")
echo $(echo "$integration" | jq -r '.IntegrationId')
}
# Create a route
create_route() {
local route_key=$1
local integration_id=$2
aws apigatewayv2 create-route \
--region $region \
--api-id $api_id \
--route-key "$route_key" \
--target "integrations/$integration_id"
}
# Create a deployment and return its ID
create_deployment_and_extract_id() {
deployment=$(aws apigatewayv2 create-deployment \
--region $region \
--api-id $api_id)
echo $(echo "$deployment" | jq -r '.DeploymentId')
}
# Main execution starts here
create_api_and_extract_id
# Create integrations and routes
for route in "\$default" "\$connect" "\$disconnect"; do
uri_suffix=$(echo "$route" | tr -d '$')
integration_id=$(create_integration "$route" "GET" "$service_url/on${uri_suffix}")
create_route "$route" "$integration_id"
echo "Route $route with integration ID $integration_id created successfully."
done
# Create deployment and stage
deployment_id=$(create_deployment_and_extract_id)
echo "Deployment ID: $deployment_id created successfully."
echo "Creating stage..."
aws apigatewayv2 create-stage \
--region $region \
--api-id $api_id \
--deployment-id $deployment_id \
--stage-name $stage_name
echo "Stage $stage_name created successfully."