|
| 1 | +#!/bin/sh |
| 2 | + |
| 3 | +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 | + |
| 10 | +GET_FUNCTION_NAME=test_items_get_function |
| 11 | +PUT_FUNCTION_NAME=test_items_put_function |
| 12 | +DELETE_FUNCTION_NAME=test_items_delete_function |
| 13 | +POST_FUNCTION_NAME=test_items_post_function |
| 14 | + |
| 15 | +function fail() { |
| 16 | + echo $2 |
| 17 | + exit $1 |
| 18 | +} |
| 19 | + |
| 20 | +aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \ |
| 21 | + --region ${REGION} \ |
| 22 | + --function-name ${GET_FUNCTION_NAME} \ |
| 23 | + --runtime nodejs20.x \ |
| 24 | + --handler index.handler \ |
| 25 | + --memory-size 128 \ |
| 26 | + --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \ |
| 27 | + --role ${LAMBDA_ROLE} |
| 28 | + |
| 29 | +[ $? == 0 ] || fail 1 "Failed: AWS / lambda / create-function (GET)" |
| 30 | + |
| 31 | +aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \ |
| 32 | + --region ${REGION} \ |
| 33 | + --function-name ${PUT_FUNCTION_NAME} \ |
| 34 | + --runtime nodejs20.x \ |
| 35 | + --handler index.handler \ |
| 36 | + --memory-size 128 \ |
| 37 | + --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \ |
| 38 | + --role ${LAMBDA_ROLE} |
| 39 | + |
| 40 | +[ $? == 0 ] || fail 1 "Failed: AWS / lambda / create-function (PUT)" |
| 41 | + |
| 42 | +aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \ |
| 43 | + --region ${REGION} \ |
| 44 | + --function-name ${DELETE_FUNCTION_NAME} \ |
| 45 | + --runtime nodejs20.x \ |
| 46 | + --handler index.handler \ |
| 47 | + --memory-size 128 \ |
| 48 | + --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \ |
| 49 | + --role ${LAMBDA_ROLE} |
| 50 | + |
| 51 | +[ $? == 0 ] || fail 1 "Failed: AWS / lambda / create-function (DELETE)" |
| 52 | + |
| 53 | +aws --endpoint-url=${BASE_ENDPOINT} lambda create-function \ |
| 54 | + --region ${REGION} \ |
| 55 | + --function-name ${POST_FUNCTION_NAME} \ |
| 56 | + --runtime nodejs20.x \ |
| 57 | + --handler index.handler \ |
| 58 | + --memory-size 128 \ |
| 59 | + --zip-file fileb://C:/Users/CBA/localstack-ts-api-gateway-lambda-dynamodb-crud-example/dist/index.zip \ |
| 60 | + --role ${LAMBDA_ROLE} |
| 61 | + |
| 62 | +[ $? == 0 ] || fail 1 "Failed: AWS / lambda / create-function (POST)" |
| 63 | + |
| 64 | +LAMBDA_ARN_GET=$(aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \ |
| 65 | + --query "Functions[?FunctionName==\`${GET_FUNCTION_NAME}\`].FunctionArn" --output text --region ${REGION}) |
| 66 | + |
| 67 | +LAMBDA_ARN_PUT=$(aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \ |
| 68 | + --query "Functions[?FunctionName==\`${PUT_FUNCTION_NAME}\`].FunctionArn" --output text --region ${REGION}) |
| 69 | + |
| 70 | +LAMBDA_ARN_DELETE=$(aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \ |
| 71 | + --query "Functions[?FunctionName==\`${DELETE_FUNCTION_NAME}\`].FunctionArn" --output text --region ${REGION}) |
| 72 | + |
| 73 | +LAMBDA_ARN_POST=$(aws --endpoint-url=${BASE_ENDPOINT} lambda list-functions \ |
| 74 | + --query "Functions[?FunctionName==\`${POST_FUNCTION_NAME}\`].FunctionArn" --output text --region ${REGION}) |
| 75 | + |
| 76 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway create-rest-api \ |
| 77 | + --region ${REGION} \ |
| 78 | + --name ${API_NAME} |
| 79 | + |
| 80 | +[ $? == 0 ] || fail 2 "Failed: AWS / apigateway / create-rest-api" |
| 81 | + |
| 82 | +API_ID=$(aws --endpoint-url=${BASE_ENDPOINT} apigateway get-rest-apis \ |
| 83 | + --query "items[?name==\`${API_NAME}\`].id" --output text --region ${REGION}) |
| 84 | + |
| 85 | +PARENT_RESOURCE_ID=$(aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \ |
| 86 | + --rest-api-id ${API_ID} \ |
| 87 | + --query 'items[?path==`/`].id' --output text --region ${REGION}) |
| 88 | + |
| 89 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway create-resource \ |
| 90 | + --region ${REGION} \ |
| 91 | + --rest-api-id ${API_ID} \ |
| 92 | + --parent-id ${PARENT_RESOURCE_ID} \ |
| 93 | + --path-part "items" |
| 94 | + |
| 95 | +RESOURCE_ID_ALL=$(aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \ |
| 96 | + --rest-api-id ${API_ID} \ |
| 97 | + --query 'items[?path==`/items`].id' --output text --region ${REGION}) |
| 98 | + |
| 99 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \ |
| 100 | + --region ${REGION} \ |
| 101 | + --rest-api-id ${API_ID} \ |
| 102 | + --resource-id ${RESOURCE_ID_ALL} \ |
| 103 | + --http-method GET \ |
| 104 | + --authorization-type "NONE" |
| 105 | + |
| 106 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \ |
| 107 | + --region ${REGION} \ |
| 108 | + --rest-api-id ${API_ID} \ |
| 109 | + --resource-id ${RESOURCE_ID_ALL} \ |
| 110 | + --http-method GET \ |
| 111 | + --type AWS_PROXY \ |
| 112 | + --integration-http-method POST \ |
| 113 | + --uri arn:aws:apigateway:${REGION}:lambda:path/2015-03-31/functions/${LAMBDA_ARN_GET}/invocations \ |
| 114 | + --passthrough-behavior WHEN_NO_MATCH |
| 115 | + |
| 116 | +[ $? == 0 ] || fail 5 "Failed: AWS / apigateway / put-integration (GET ALL)" |
| 117 | + |
| 118 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway create-resource \ |
| 119 | + --region ${REGION} \ |
| 120 | + --rest-api-id ${API_ID} \ |
| 121 | + --parent-id ${RESOURCE_ID_ALL} \ |
| 122 | + --path-part "{itemId}" |
| 123 | + |
| 124 | +RESOURCE_ID=$(aws --endpoint-url=${BASE_ENDPOINT} apigateway get-resources \ |
| 125 | + --rest-api-id ${API_ID} \ |
| 126 | + --query 'items[?path==`/items/{itemId}`].id' --output text --region ${REGION}) |
| 127 | + |
| 128 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \ |
| 129 | + --region ${REGION} \ |
| 130 | + --rest-api-id ${API_ID} \ |
| 131 | + --resource-id ${RESOURCE_ID} \ |
| 132 | + --http-method GET \ |
| 133 | + --request-parameters "method.request.path.itemId=true" \ |
| 134 | + --authorization-type "NONE" |
| 135 | + |
| 136 | +[ $? == 0 ] || fail 4 "Failed: AWS / apigateway / put-method (GET ITEM)" |
| 137 | + |
| 138 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \ |
| 139 | + --region ${REGION} \ |
| 140 | + --rest-api-id ${API_ID} \ |
| 141 | + --resource-id ${RESOURCE_ID} \ |
| 142 | + --http-method GET \ |
| 143 | + --type AWS_PROXY \ |
| 144 | + --integration-http-method POST \ |
| 145 | + --uri arn:aws:apigateway:${REGION}:lambda:path/2015-03-31/functions/${LAMBDA_ARN_GET}/invocations \ |
| 146 | + --passthrough-behavior WHEN_NO_MATCH |
| 147 | + |
| 148 | +[ $? == 0 ] || fail 5 "Failed: AWS / apigateway / put-integration (GET ITEM)" |
| 149 | + |
| 150 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \ |
| 151 | + --region ${REGION} \ |
| 152 | + --rest-api-id ${API_ID} \ |
| 153 | + --resource-id ${RESOURCE_ID} \ |
| 154 | + --http-method PUT \ |
| 155 | + --request-parameters "method.request.path.itemId=true" \ |
| 156 | + --authorization-type "NONE" |
| 157 | + |
| 158 | +[ $? == 0 ] || fail 4 "Failed: AWS / apigateway / put-method (PUT ITEM)" |
| 159 | + |
| 160 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \ |
| 161 | + --region ${REGION} \ |
| 162 | + --rest-api-id ${API_ID} \ |
| 163 | + --resource-id ${RESOURCE_ID} \ |
| 164 | + --http-method PUT \ |
| 165 | + --type AWS_PROXY \ |
| 166 | + --integration-http-method POST \ |
| 167 | + --uri arn:aws:apigateway:${REGION}:lambda:path/2015-03-31/functions/${LAMBDA_ARN_PUT}/invocations \ |
| 168 | + --passthrough-behavior WHEN_NO_MATCH |
| 169 | + |
| 170 | +[ $? == 0 ] || fail 5 "Failed: AWS / apigateway / put-integration (PUT ITEM)" |
| 171 | + |
| 172 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \ |
| 173 | + --region ${REGION} \ |
| 174 | + --rest-api-id ${API_ID} \ |
| 175 | + --resource-id ${RESOURCE_ID} \ |
| 176 | + --http-method DELETE \ |
| 177 | + --request-parameters "method.request.path.itemId=true" \ |
| 178 | + --authorization-type "NONE" |
| 179 | + |
| 180 | +[ $? == 0 ] || fail 4 "Failed: AWS / apigateway / put-method (DELETE ITEM)" |
| 181 | + |
| 182 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \ |
| 183 | + --region ${REGION} \ |
| 184 | + --rest-api-id ${API_ID} \ |
| 185 | + --resource-id ${RESOURCE_ID} \ |
| 186 | + --http-method DELETE \ |
| 187 | + --type AWS_PROXY \ |
| 188 | + --integration-http-method POST \ |
| 189 | + --uri arn:aws:apigateway:${REGION}:lambda:path/2015-03-31/functions/${LAMBDA_ARN_DELETE}/invocations \ |
| 190 | + --passthrough-behavior WHEN_NO_MATCH |
| 191 | + |
| 192 | +[ $? == 0 ] || fail 5 "Failed: AWS / apigateway / put-integration (DELETE ITEM)" |
| 193 | + |
| 194 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-method \ |
| 195 | + --region ${REGION} \ |
| 196 | + --rest-api-id ${API_ID} \ |
| 197 | + --resource-id ${RESOURCE_ID_ALL} \ |
| 198 | + --http-method POST \ |
| 199 | + --authorization-type "NONE" |
| 200 | + |
| 201 | +[ $? == 0 ] || fail 4 "Failed: AWS / apigateway / put-method (POST ITEMS)" |
| 202 | + |
| 203 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway put-integration \ |
| 204 | + --region ${REGION} \ |
| 205 | + --rest-api-id ${API_ID} \ |
| 206 | + --resource-id ${RESOURCE_ID_ALL} \ |
| 207 | + --http-method POST \ |
| 208 | + --type AWS_PROXY \ |
| 209 | + --integration-http-method POST \ |
| 210 | + --uri arn:aws:apigateway:${REGION}:lambda:path/2015-03-31/functions/${LAMBDA_ARN_POST}/invocations \ |
| 211 | + --passthrough-behavior WHEN_NO_MATCH |
| 212 | + |
| 213 | +[ $? == 0 ] || fail 5 "Failed: AWS / apigateway / put-integration (POST ITEMS)" |
| 214 | + |
| 215 | +aws --endpoint-url=${BASE_ENDPOINT} apigateway create-deployment \ |
| 216 | + --region ${REGION} \ |
| 217 | + --rest-api-id ${API_ID} \ |
| 218 | + --stage-name ${STAGE} |
| 219 | + |
| 220 | +[ $? == 0 ] || fail 6 "Failed: AWS / apigateway / create-deployment" |
| 221 | + |
| 222 | +ENDPOINT=${BASE_ENDPOINT}/restapis/${API_ID}/${STAGE}/_user_request_/items |
| 223 | + |
| 224 | +echo "API_ID=${API_ID}" >> .local.env |
| 225 | + |
| 226 | +echo "API available at: ${ENDPOINT}" |
0 commit comments