11#! /bin/sh
22
3- apt-get -y install jq
3+ LAMBDAS_DIR=/etc/localstack/init/ready.d
4+ if [[ ! -e $LAMBDAS_DIR ]]; then
5+ # for local testing, running the script directly on the host (without init hooks)
6+ LAMBDAS_DIR=./lambda-functions
7+ fi
8+
9+ # set region globally
10+ export AWS_DEFAULT_REGION=us-east-1
11+
12+ # install `jq`, if not yet available
13+ which jq || apt-get -y install jq
414
515# create table
616echo " Create DynamoDB table..."
717awslocal dynamodb create-table \
818 --table-name Products \
919 --attribute-definitions AttributeName=id,AttributeType=S \
1020 --key-schema AttributeName=id,KeyType=HASH \
11- --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
12- --region us-east-1
21+ --provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5
1322
1423
1524# create Lambdas
@@ -20,10 +29,10 @@ awslocal lambda create-function \
2029 --runtime java17 \
2130 --handler lambda.AddProduct::handleRequest \
2231 --memory-size 1024 \
23- --zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
24- --region us-east-1 \
32+ --timeout 45 \
33+ --zip-file fileb:// $LAMBDAS_DIR /target/product-lambda.jar \
2534 --role arn:aws:iam::000000000000:role/productRole \
26- --environment Variables={AWS_REGION=us-east-1 }
35+ --environment Variables={AWS_REGION=$AWS_DEFAULT_REGION }
2736
2837
2938echo " Get Product Lambda..."
@@ -32,54 +41,50 @@ awslocal lambda create-function \
3241 --runtime java17 \
3342 --handler lambda.GetProduct::handleRequest \
3443 --memory-size 1024 \
35- --zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
36- --region us-east-1 \
44+ --timeout 45 \
45+ --zip-file fileb:// $LAMBDAS_DIR /target/product-lambda.jar \
3746 --role arn:aws:iam::000000000000:role/productRole \
38- --environment Variables={AWS_REGION=us-east-1 }
47+ --environment Variables={AWS_REGION=$AWS_DEFAULT_REGION }
3948
4049export REST_API_ID=12345
4150
4251# create rest api gateway
4352echo " Create Rest API..."
44- awslocal apigateway create-rest-api --name quote-api-gateway --tags ' {"_custom_id_":"12345"}' --region us-east-1
53+ awslocal apigateway create-rest-api --name quote-api-gateway --tags ' {"_custom_id_":"12345"}'
4554
4655# get parent id of resource
4756echo " Export Parent ID..."
48- export PARENT_ID=$( awslocal apigateway get-resources --rest-api-id $REST_API_ID --region=us-east-1 | jq -r ' .items[0].id' )
57+ export PARENT_ID=$( awslocal apigateway get-resources --rest-api-id $REST_API_ID | jq -r ' .items[0].id' )
4958
5059# get resource id
5160echo " Export Resource ID..."
52- export RESOURCE_ID=$( awslocal apigateway create-resource --rest-api-id $REST_API_ID --parent-id $PARENT_ID --path-part " productApi" --region=us-east-1 | jq -r ' .id' )
61+ export RESOURCE_ID=$( awslocal apigateway create-resource --rest-api-id $REST_API_ID --parent-id $PARENT_ID --path-part " productApi" | jq -r ' .id' )
5362
54- echo " RESOURCE ID:"
55- echo $RESOURCE
63+ echo " RESOURCE ID: $RESOURCE_ID "
5664
5765echo " Put GET Method..."
5866awslocal apigateway put-method \
5967--rest-api-id $REST_API_ID \
6068--resource-id $RESOURCE_ID \
6169--http-method GET \
6270--request-parameters " method.request.path.productApi=true" \
63- --authorization-type " NONE" \
64- --region=us-east-1
71+ --authorization-type " NONE"
6572
6673echo " Put POST Method..."
6774awslocal apigateway put-method \
6875--rest-api-id $REST_API_ID \
6976--resource-id $RESOURCE_ID \
7077--http-method POST \
7178--request-parameters " method.request.path.productApi=true" \
72- --authorization-type " NONE" \
73- --region=us-east-1
79+ --authorization-type " NONE"
7480
7581
7682echo " Update GET Method..."
7783awslocal apigateway update-method \
7884 --rest-api-id $REST_API_ID \
7985 --resource-id $RESOURCE_ID \
8086 --http-method GET \
81- --patch-operations " op=replace,path=/requestParameters/method.request.querystring.param,value=true" \
82- --region=us-east-1
87+ --patch-operations " op=replace,path=/requestParameters/method.request.querystring.param,value=true"
8388
8489
8590echo " Put POST Method Integration..."
@@ -89,9 +94,8 @@ awslocal apigateway put-integration \
8994 --http-method POST \
9095 --type AWS_PROXY \
9196 --integration-http-method POST \
92- --uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:add-product/invocations \
93- --passthrough-behavior WHEN_NO_MATCH \
94- --region=us-east-1
97+ --uri arn:aws:apigateway:$AWS_DEFAULT_REGION :lambda:path/2015-03-31/functions/arn:aws:lambda:$AWS_DEFAULT_REGION :000000000000:function:add-product/invocations \
98+ --passthrough-behavior WHEN_NO_MATCH
9599
96100echo " Put GET Method Integration..."
97101awslocal apigateway put-integration \
@@ -100,15 +104,13 @@ awslocal apigateway put-integration \
100104 --http-method GET \
101105 --type AWS_PROXY \
102106 --integration-http-method GET \
103- --uri arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:000000000000:function:get-product/invocations \
104- --passthrough-behavior WHEN_NO_MATCH \
105- --region=us-east-1
107+ --uri arn:aws:apigateway:$AWS_DEFAULT_REGION :lambda:path/2015-03-31/functions/arn:aws:lambda:$AWS_DEFAULT_REGION :000000000000:function:get-product/invocations \
108+ --passthrough-behavior WHEN_NO_MATCH
106109
107110echo " Create DEV Deployment..."
108111awslocal apigateway create-deployment \
109112 --rest-api-id $REST_API_ID \
110- --stage-name dev \
111- --region=us-east-1
113+ --stage-name dev
112114
113115awslocal sns create-topic --name ProductEventsTopic
114116
@@ -117,23 +119,23 @@ awslocal sqs create-queue --queue-name ProductEventsQueue
117119awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn
118120
119121awslocal sns subscribe \
120- --topic-arn arn:aws:sns:us-east-1 :000000000000:ProductEventsTopic \
122+ --topic-arn arn:aws:sns:$AWS_DEFAULT_REGION :000000000000:ProductEventsTopic \
121123 --protocol sqs \
122- --notification-endpoint arn:aws:sqs:us-east-1 :000000000000:ProductEventsQueue
124+ --notification-endpoint arn:aws:sqs:$AWS_DEFAULT_REGION :000000000000:ProductEventsQueue
123125
124126awslocal lambda create-function \
125127 --function-name process-product-events \
126128 --runtime java17 \
127129 --handler lambda.DynamoDBWriterLambda::handleRequest \
128130 --memory-size 1024 \
129- --zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
130- --region us-east-1 \
131+ --timeout 20 \
132+ --zip-file fileb:// $LAMBDAS_DIR /target/product-lambda.jar \
131133 --role arn:aws:iam::000000000000:role/productRole
132134
133135awslocal lambda create-event-source-mapping \
134136 --function-name process-product-events \
135137 --batch-size 10 \
136- --event-source-arn arn:aws:sqs:us-east-1 :000000000000:ProductEventsQueue
138+ --event-source-arn arn:aws:sqs:$AWS_DEFAULT_REGION :000000000000:ProductEventsQueue
137139
138140awslocal sqs set-queue-attributes \
139141 --queue-url http://localhost:4566/000000000000/ProductEventsQueue \
0 commit comments