11#! /bin/sh
22
3+ # Base endpoint for the local AWS environment
34BASE_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
95
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
1014GET_FUNCTION_NAME=test_items_get_function
1115PUT_FUNCTION_NAME=test_items_put_function
1216DELETE_FUNCTION_NAME=test_items_delete_function
1317POST_FUNCTION_NAME=test_items_post_function
1418
19+ # Function to handle failures and exit the script
1520function 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
1823}
1924
25+ # # https://docs.localstack.cloud/user-guide/aws/lambda/
26+
27+ # Create the GET Lambda function
2028aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
2129 --region ${REGION} \
2230 --function-name ${GET_FUNCTION_NAME} \
@@ -26,8 +34,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
2634 --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
2735 --role ${LAMBDA_ROLE}
2836
37+ # Check if the function creation was successful
2938[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (GET)"
3039
40+ # Create the PUT Lambda function
3141aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
3242 --region ${REGION} \
3343 --function-name ${PUT_FUNCTION_NAME} \
@@ -37,8 +47,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
3747 --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
3848 --role ${LAMBDA_ROLE}
3949
50+ # Check if the function creation was successful
4051[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (PUT)"
4152
53+ # Create the DELETE Lambda function
4254aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
4355 --region ${REGION} \
4456 --function-name ${DELETE_FUNCTION_NAME} \
@@ -48,8 +60,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
4860 --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
4961 --role ${LAMBDA_ROLE}
5062
63+ # Check if the function creation was successful
5164[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (DELETE)"
5265
66+ # Create the POST Lambda function
5367aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
5468 --region ${REGION} \
5569 --function-name ${POST_FUNCTION_NAME} \
@@ -59,8 +73,10 @@ aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \
5973 --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \
6074 --role ${LAMBDA_ROLE}
6175
76+ # Check if the function creation was successful
6277[ $? == 0 ] || fail 1 " Failed: AWS / lambda / create-function (POST)"
6378
79+ # Retrieve the ARNs for the created Lambda functions
6480LAMBDA_ARN_GET=$( aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \
6581 --query " Functions[?FunctionName==\` ${GET_FUNCTION_NAME} \` ].FunctionArn" --output text --region ${REGION} )
6682
@@ -73,36 +89,46 @@ LAMBDA_ARN_DELETE=$(aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \
7389LAMBDA_ARN_POST=$( aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \
7490 --query " Functions[?FunctionName==\` ${POST_FUNCTION_NAME} \` ].FunctionArn" --output text --region ${REGION} )
7591
92+ # # https://docs.localstack.cloud/user-guide/aws/apigateway/
93+
94+ # Create the API Gateway REST API
7695aws --endpoint-url=${BASE_ENDPOINT} apigateway create-rest-api \
7796 --region ${REGION} \
7897 --name ${API_NAME}
7998
99+ # Check if the API creation was successful
80100[ $? == 0 ] || fail 2 " Failed: AWS / apigateway / create-rest-api"
81101
102+ # Retrieve the API ID for the created REST API
82103API_ID=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-rest-apis \
83104 --query " items[?name==\` ${API_NAME} \` ].id" --output text --region ${REGION} )
84105
106+ # Get the parent resource ID (the root resource)
85107PARENT_RESOURCE_ID=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \
86108 --rest-api-id ${API_ID} \
87109 --query ' items[?path==`/`].id' --output text --region ${REGION} )
88110
111+ # Create a new resource under the root resource ("/items")
89112aws --endpoint-url=${BASE_ENDPOINT} apigateway create-resource \
90113 --region ${REGION} \
91114 --rest-api-id ${API_ID} \
92115 --parent-id ${PARENT_RESOURCE_ID} \
93116 --path-part " items"
94117
118+ # Retrieve the resource ID for the newly created "/items" resource
95119RESOURCE_ID_ALL=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \
96120 --rest-api-id ${API_ID} \
97121 --query ' items[?path==`/items`].id' --output text --region ${REGION} )
98122
123+ # Define the GET method for the "/items" resource
99124aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
100125 --region ${REGION} \
101126 --rest-api-id ${API_ID} \
102127 --resource-id ${RESOURCE_ID_ALL} \
103128 --http-method GET \
104129 --authorization-type " NONE"
105130
131+ # Define the integration for the GET method
106132aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
107133 --region ${REGION} \
108134 --rest-api-id ${API_ID} \
@@ -113,18 +139,22 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
113139 --uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_GET} /invocations \
114140 --passthrough-behavior WHEN_NO_MATCH
115141
142+ # Check if the integration was successful
116143[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (GET ALL)"
117144
145+ # Create a resource for individual items ("/items/{itemId}")
118146aws --endpoint-url=${BASE_ENDPOINT} apigateway create-resource \
119147 --region ${REGION} \
120148 --rest-api-id ${API_ID} \
121149 --parent-id ${RESOURCE_ID_ALL} \
122150 --path-part " {itemId}"
123151
152+ # Retrieve the resource ID for the "/items/{itemId}" resource
124153RESOURCE_ID=$( aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \
125154 --rest-api-id ${API_ID} \
126155 --query ' items[?path==`/items/{itemId}`].id' --output text --region ${REGION} )
127156
157+ # Define the GET method for the "/items/{itemId}" resource
128158aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
129159 --region ${REGION} \
130160 --rest-api-id ${API_ID} \
@@ -133,8 +163,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
133163 --request-parameters " method.request.path.itemId=true" \
134164 --authorization-type " NONE"
135165
166+ # Check if the GET method creation was successful
136167[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (GET ITEM)"
137168
169+ # Define the integration for the GET method of an individual item
138170aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
139171 --region ${REGION} \
140172 --rest-api-id ${API_ID} \
@@ -145,8 +177,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
145177 --uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_GET} /invocations \
146178 --passthrough-behavior WHEN_NO_MATCH
147179
180+ # Check if the integration for the GET method was successful
148181[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (GET ITEM)"
149182
183+ # Define the PUT method for the "/items/{itemId}" resource
150184aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
151185 --region ${REGION} \
152186 --rest-api-id ${API_ID} \
@@ -155,8 +189,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
155189 --request-parameters " method.request.path.itemId=true" \
156190 --authorization-type " NONE"
157191
192+ # Check if the PUT method creation was successful
158193[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (PUT ITEM)"
159194
195+ # Define the integration for the PUT method
160196aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
161197 --region ${REGION} \
162198 --rest-api-id ${API_ID} \
@@ -167,8 +203,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
167203 --uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_PUT} /invocations \
168204 --passthrough-behavior WHEN_NO_MATCH
169205
206+ # Check if the integration for the PUT method was successful
170207[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (PUT ITEM)"
171208
209+ # Define the DELETE method for the "/items/{itemId}" resource
172210aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
173211 --region ${REGION} \
174212 --rest-api-id ${API_ID} \
@@ -177,8 +215,10 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
177215 --request-parameters " method.request.path.itemId=true" \
178216 --authorization-type " NONE"
179217
218+ # Check if the DELETE method creation was successful
180219[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (DELETE ITEM)"
181220
221+ # Define the integration for the DELETE method
182222aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
183223 --region ${REGION} \
184224 --rest-api-id ${API_ID} \
@@ -189,17 +229,21 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
189229 --uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_DELETE} /invocations \
190230 --passthrough-behavior WHEN_NO_MATCH
191231
232+ # Check if the integration for the DELETE method was successful
192233[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (DELETE ITEM)"
193234
235+ # Define the POST method for the "/items" resource
194236aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \
195237 --region ${REGION} \
196238 --rest-api-id ${API_ID} \
197239 --resource-id ${RESOURCE_ID_ALL} \
198240 --http-method POST \
199241 --authorization-type " NONE"
200242
243+ # Check if the POST method creation was successful
201244[ $? == 0 ] || fail 4 " Failed: AWS / apigateway / put-method (POST ITEMS)"
202245
246+ # Define the integration for the POST method
203247aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
204248 --region ${REGION} \
205249 --rest-api-id ${API_ID} \
@@ -210,17 +254,23 @@ aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \
210254 --uri arn:aws:apigateway:${REGION} :lambda:path/2015-03-31/functions/${LAMBDA_ARN_POST} /invocations \
211255 --passthrough-behavior WHEN_NO_MATCH
212256
257+ # Check if the integration for the POST method was successful
213258[ $? == 0 ] || fail 5 " Failed: AWS / apigateway / put-integration (POST ITEMS)"
214259
260+ # Create a deployment for the API
215261aws --endpoint-url=${BASE_ENDPOINT} apigateway create-deployment \
216262 --region ${REGION} \
217263 --rest-api-id ${API_ID} \
218264 --stage-name ${STAGE}
219265
266+ # Check if the deployment was successful
220267[ $? == 0 ] || fail 6 " Failed: AWS / apigateway / create-deployment"
221268
269+ # Define the endpoint for accessing the API
222270ENDPOINT=${BASE_ENDPOINT} /restapis/${API_ID} /${STAGE} /_user_request_/items
223271
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
225274
275+ # Output the API endpoint
226276echo " API available at: ${ENDPOINT} "
0 commit comments