Skip to content

Commit 492495d

Browse files
authored
Merge pull request #1 from localstack-samples/minor-fixes
Add minor enhancements in init script for testing outside docker-compose
2 parents e900478 + ad03243 commit 492495d

File tree

3 files changed

+40
-36
lines changed

3 files changed

+40
-36
lines changed

.gitignore

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,6 @@ FIS-experiments/lambda-functions/lambda-functions.iml
2323
route53-failover/lambda-functions/lambda-functions.iml
2424
/extension-outages/terraform/.terraform
2525
/extension-outages/terraform/.terraform.lock.hcl
26-
/extension-outages/terraform/terraform.tfstate
26+
/extension-outages/terraform/terraform.tfstate
27+
28+
target/

FIS-experiments/init-resources.sh

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
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
616
echo "Create DynamoDB table..."
717
awslocal 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

2938
echo "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

4049
export REST_API_ID=12345
4150

4251
# create rest api gateway
4352
echo "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
4756
echo "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
5160
echo "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

5765
echo "Put GET Method..."
5866
awslocal 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

6673
echo "Put POST Method..."
6774
awslocal 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

7682
echo "Update GET Method..."
7783
awslocal 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

8590
echo "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

96100
echo "Put GET Method Integration..."
97101
awslocal 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

107110
echo "Create DEV Deployment..."
108111
awslocal apigateway create-deployment \
109112
--rest-api-id $REST_API_ID \
110-
--stage-name dev \
111-
--region=us-east-1
113+
--stage-name dev
112114

113115
awslocal sns create-topic --name ProductEventsTopic
114116

@@ -117,23 +119,23 @@ awslocal sqs create-queue --queue-name ProductEventsQueue
117119
awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn
118120

119121
awslocal 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

124126
awslocal 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

133135
awslocal 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

138140
awslocal sqs set-queue-attributes \
139141
--queue-url http://localhost:4566/000000000000/ProductEventsQueue \

FIS-experiments/lambda-functions/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,8 @@
1111
<version>1.0-SNAPSHOT</version>
1212

1313
<properties>
14-
<maven.compiler.source>17</maven.compiler.source>
15-
<maven.compiler.target>17</maven.compiler.target>
14+
<maven.compiler.source>11</maven.compiler.source>
15+
<maven.compiler.target>11</maven.compiler.target>
1616
<exec.cleanupDaemonThreads>false</exec.cleanupDaemonThreads>
1717
</properties>
1818

0 commit comments

Comments
 (0)