1
1
#! /bin/sh
2
2
3
+ # Base endpoint for the local AWS environment
3
4
BASE_ENDPOINT=http://localhost:4566
4
- API_NAME=items_crud
5
- ROUTE_NAME=items
6
- STAGE=test
7
- REGION=us-east-1
8
- LAMBDA_ROLE=arn:aws:iam::123456789012:role/lambda-role
9
5
6
+ # API configuration parameters
7
+ API_NAME=items_crud # Name of the API
8
+ ROUTE_NAME=items # Route for the API
9
+ STAGE=test # Deployment stage of the API
10
+ REGION=us-east-1 # AWS region for deployment
11
+ LAMBDA_ROLE=arn:aws:iam::123456789012:role/lambda-role # IAM role for Lambda functions
12
+
13
+ # Function names for different CRUD operations
10
14
GET_FUNCTION_NAME=test_items_get_function
11
15
PUT_FUNCTION_NAME=test_items_put_function
12
16
DELETE_FUNCTION_NAME=test_items_delete_function
13
17
POST_FUNCTION_NAME=test_items_post_function
14
18
19
+ # Function to handle failures and exit the script
15
20
function fail() {
16
- echo $2
17
- exit $1
21
+ echo $2 # Print the error message
22
+ exit $1 # Exit the script with the specified error code
18
23
}
19
24
25
+ # # https://docs.localstack.cloud/user-guide/aws/lambda/
26
+
27
+ # Create the GET Lambda function
20
28
aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
21
29
--region ${REGION} \
22
30
--function-name ${GET_FUNCTION_NAME} \
@@ -26,8 +34,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
26
34
--zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
27
35
--role ${LAMBDA_ROLE}
28
36
37
+ # Check if the function creation was successful
29
38
[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (GET)"
30
39
40
+ # Create the PUT Lambda function
31
41
aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
32
42
--region ${REGION} \
33
43
--function-name ${PUT_FUNCTION_NAME} \
@@ -37,8 +47,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
37
47
--zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
38
48
--role ${LAMBDA_ROLE}
39
49
50
+ # Check if the function creation was successful
40
51
[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (PUT)"
41
52
53
+ # Create the DELETE Lambda function
42
54
aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
43
55
--region ${REGION} \
44
56
--function-name ${DELETE_FUNCTION_NAME} \
@@ -48,8 +60,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
48
60
--zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
49
61
--role ${LAMBDA_ROLE}
50
62
63
+ # Check if the function creation was successful
51
64
[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (DELETE)"
52
65
66
+ # Create the POST Lambda function
53
67
aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
54
68
--region ${REGION} \
55
69
--function-name ${POST_FUNCTION_NAME} \
@@ -59,8 +73,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
59
73
--zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
60
74
--role ${LAMBDA_ROLE}
61
75
76
+ # Check if the function creation was successful
62
77
[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (POST)"
63
78
79
+ # Retrieve the ARNs for the created Lambda functions
64
80
LAMBDA_ARN_GET=$( aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \
65
81
--query " Functions[?FunctionName==\` ${GET_FUNCTION_NAME} \` ].FunctionArn" --output text --region ${REGION} )
66
82
@@ -73,36 +89,46 @@ LAMBDA_ARN_DELETE=$(aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \
73
89
LAMBDA_ARN_POST=$( aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \
74
90
--query " Functions[?FunctionName==\` ${POST_FUNCTION_NAME} \` ].FunctionArn" --output text --region ${REGION} )
75
91
92
+ # # https://docs.localstack.cloud/user-guide/aws/apigateway/
93
+
94
+ # Create the API Gateway REST API
76
95
aws --endpoint-url=${BASE_ENDPOINT} apigateway create-rest-api \
77
96
--region ${REGION} \
78
97
--name ${API_NAME}
79
98
99
+ # Check if the API creation was successful
80
100
[ $? == 0 ] || fail 2 " Failed: AWS / apigateway / create-rest-api"
81
101
102
+ # Retrieve the API ID for the created REST API
82
103
API_ID=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-rest-apis \
83
104
--query " items[?name==\` ${API_NAME} \` ].id" --output text --region ${REGION} )
84
105
106
+ # Get the parent resource ID (the root resource)
85
107
PARENT_RESOURCE_ID=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \
86
108
--rest-api-id ${API_ID} \
87
109
--query ' items[?path==`/`].id' --output text --region ${REGION} )
88
110
111
+ # Create a new resource under the root resource ("/items")
89
112
aws --endpoint-url=${BASE_ENDPOINT} apigateway create-resource \
90
113
--region ${REGION} \
91
114
--rest-api-id ${API_ID} \
92
115
--parent-id ${PARENT_RESOURCE_ID} \
93
116
--path-part " items"
94
117
118
+ # Retrieve the resource ID for the newly created "/items" resource
95
119
RESOURCE_ID_ALL=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \
96
120
--rest-api-id ${API_ID} \
97
121
--query ' items[?path==`/items`].id' --output text --region ${REGION} )
98
122
123
+ # Define the GET method for the "/items" resource
99
124
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
100
125
--region ${REGION} \
101
126
--rest-api-id ${API_ID} \
102
127
--resource-id ${RESOURCE_ID_ALL} \
103
128
--http-method GET \
104
129
--authorization-type " NONE"
105
130
131
+ # Define the integration for the GET method
106
132
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
107
133
--region ${REGION} \
108
134
--rest-api-id ${API_ID} \
@@ -113,18 +139,22 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
113
139
--uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_GET} /invocations \
114
140
--passthrough-behavior WHEN_NO_MATCH
115
141
142
+ # Check if the integration was successful
116
143
[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (GET ALL)"
117
144
145
+ # Create a resource for individual items ("/items/{itemId}")
118
146
aws --endpoint-url=${BASE_ENDPOINT} apigateway create-resource \
119
147
--region ${REGION} \
120
148
--rest-api-id ${API_ID} \
121
149
--parent-id ${RESOURCE_ID_ALL} \
122
150
--path-part " {itemId}"
123
151
152
+ # Retrieve the resource ID for the "/items/{itemId}" resource
124
153
RESOURCE_ID=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \
125
154
--rest-api-id ${API_ID} \
126
155
--query ' items[?path==`/items/{itemId}`].id' --output text --region ${REGION} )
127
156
157
+ # Define the GET method for the "/items/{itemId}" resource
128
158
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
129
159
--region ${REGION} \
130
160
--rest-api-id ${API_ID} \
@@ -133,8 +163,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
133
163
--request-parameters " method.request.path.itemId=true" \
134
164
--authorization-type " NONE"
135
165
166
+ # Check if the GET method creation was successful
136
167
[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (GET ITEM)"
137
168
169
+ # Define the integration for the GET method of an individual item
138
170
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
139
171
--region ${REGION} \
140
172
--rest-api-id ${API_ID} \
@@ -145,8 +177,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
145
177
--uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_GET} /invocations \
146
178
--passthrough-behavior WHEN_NO_MATCH
147
179
180
+ # Check if the integration for the GET method was successful
148
181
[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (GET ITEM)"
149
182
183
+ # Define the PUT method for the "/items/{itemId}" resource
150
184
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
151
185
--region ${REGION} \
152
186
--rest-api-id ${API_ID} \
@@ -155,8 +189,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
155
189
--request-parameters " method.request.path.itemId=true" \
156
190
--authorization-type " NONE"
157
191
192
+ # Check if the PUT method creation was successful
158
193
[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (PUT ITEM)"
159
194
195
+ # Define the integration for the PUT method
160
196
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
161
197
--region ${REGION} \
162
198
--rest-api-id ${API_ID} \
@@ -167,8 +203,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
167
203
--uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_PUT} /invocations \
168
204
--passthrough-behavior WHEN_NO_MATCH
169
205
206
+ # Check if the integration for the PUT method was successful
170
207
[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (PUT ITEM)"
171
208
209
+ # Define the DELETE method for the "/items/{itemId}" resource
172
210
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
173
211
--region ${REGION} \
174
212
--rest-api-id ${API_ID} \
@@ -177,8 +215,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
177
215
--request-parameters " method.request.path.itemId=true" \
178
216
--authorization-type " NONE"
179
217
218
+ # Check if the DELETE method creation was successful
180
219
[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (DELETE ITEM)"
181
220
221
+ # Define the integration for the DELETE method
182
222
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
183
223
--region ${REGION} \
184
224
--rest-api-id ${API_ID} \
@@ -189,17 +229,21 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
189
229
--uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_DELETE} /invocations \
190
230
--passthrough-behavior WHEN_NO_MATCH
191
231
232
+ # Check if the integration for the DELETE method was successful
192
233
[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (DELETE ITEM)"
193
234
235
+ # Define the POST method for the "/items" resource
194
236
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
195
237
--region ${REGION} \
196
238
--rest-api-id ${API_ID} \
197
239
--resource-id ${RESOURCE_ID_ALL} \
198
240
--http-method POST \
199
241
--authorization-type " NONE"
200
242
243
+ # Check if the POST method creation was successful
201
244
[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (POST ITEMS)"
202
245
246
+ # Define the integration for the POST method
203
247
aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
204
248
--region ${REGION} \
205
249
--rest-api-id ${API_ID} \
@@ -210,17 +254,23 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
210
254
--uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_POST} /invocations \
211
255
--passthrough-behavior WHEN_NO_MATCH
212
256
257
+ # Check if the integration for the POST method was successful
213
258
[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (POST ITEMS)"
214
259
260
+ # Create a deployment for the API
215
261
aws --endpoint-url=${BASE_ENDPOINT} apigateway create-deployment \
216
262
--region ${REGION} \
217
263
--rest-api-id ${API_ID} \
218
264
--stage-name ${STAGE}
219
265
266
+ # Check if the deployment was successful
220
267
[ $? == 0 ] || fail 6 " Failed: AWS / apigateway / create-deployment"
221
268
269
+ # Define the endpoint for accessing the API
222
270
ENDPOINT=${BASE_ENDPOINT} /restapis/${API_ID} /${STAGE} /_user_request_/items
223
271
224
- echo " API_ID=${API_ID} " >> .local.env
272
+ # Save the endpoint to a local environment file
273
+ echo " LOCAL_API_ENDPOINT=${ENDPOINT} " >> .local.env
225
274
275
+ # Output the API endpoint
226
276
echo " API available at: ${ENDPOINT} "
0 commit comments