Skip to content

Commit 0b7bd25

Browse files
committed
break down init hook into separate parts
1 parent 659a64b commit 0b7bd25

File tree

5 files changed

+49
-34
lines changed

5 files changed

+49
-34
lines changed

.github/workflows/ci.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,16 @@ jobs:
2828

2929
- name: Spin up LocalStack
3030
run: |
31-
docker-compose up -d
31+
docker compose up -d
3232
sleep 100
3333
env:
3434
LOCALSTACK_AUTH_TOKEN: ${{ secrets.LOCALSTACK_AUTH_TOKEN }}
3535

36+
- name: Setup the solution
37+
run: |
38+
pip install awscli-local
39+
./setup-solution.sh
40+
3641
- name: Run Integration Tests
3742
run: |
3843
pip3 install boto3 pytest

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ The output will be:
8989
[{"service": "dynamodb", "region": "us-east-1"}]
9090
```
9191

92+
### Implement error handling
93+
94+
The application is designed to handle the outage by sending a message to the SQS queue using a Lambda function. Run the following command to create the necessary resources:
95+
96+
```bash
97+
./setup-solution.sh
98+
```
99+
92100
You can now test the infrastructure again by running the following command:
93101

94102
```bash

init-resources.sh

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -101,32 +101,3 @@ awslocal apigateway create-deployment \
101101
--rest-api-id $REST_API_ID \
102102
--stage-name dev \
103103
--region=us-east-1
104-
105-
awslocal sns create-topic --name ProductEventsTopic
106-
107-
awslocal sqs create-queue --queue-name ProductEventsQueue
108-
109-
awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn
110-
111-
awslocal sns subscribe \
112-
--topic-arn arn:aws:sns:us-east-1:000000000000:ProductEventsTopic \
113-
--protocol sqs \
114-
--notification-endpoint arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue
115-
116-
awslocal lambda create-function \
117-
--function-name process-product-events \
118-
--runtime java17 \
119-
--handler lambda.DynamoDBWriterLambda::handleRequest \
120-
--memory-size 1024 \
121-
--zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
122-
--region us-east-1 \
123-
--role arn:aws:iam::000000000000:role/productRole
124-
125-
awslocal lambda create-event-source-mapping \
126-
--function-name process-product-events \
127-
--batch-size 10 \
128-
--event-source-arn arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue
129-
130-
awslocal sqs set-queue-attributes \
131-
--queue-url http://localhost:4566/000000000000/ProductEventsQueue \
132-
--attributes VisibilityTimeout=10

setup-solution.sh

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#!/bin/sh
2+
3+
awslocal sns create-topic --name ProductEventsTopic
4+
5+
awslocal sqs create-queue --queue-name ProductEventsQueue
6+
7+
awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn
8+
9+
awslocal sns subscribe \
10+
--topic-arn arn:aws:sns:us-east-1:000000000000:ProductEventsTopic \
11+
--protocol sqs \
12+
--notification-endpoint arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue
13+
14+
awslocal lambda create-function \
15+
--function-name process-product-events \
16+
--runtime java17 \
17+
--handler lambda.DynamoDBWriterLambda::handleRequest \
18+
--memory-size 1024 \
19+
--zip-file fileb://lambda-functions/target/product-lambda.jar \
20+
--region us-east-1 \
21+
--role arn:aws:iam::000000000000:role/productRole
22+
23+
awslocal lambda create-event-source-mapping \
24+
--function-name process-product-events \
25+
--batch-size 10 \
26+
--event-source-arn arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue
27+
28+
awslocal sqs set-queue-attributes \
29+
--queue-url http://localhost:4566/000000000000/ProductEventsQueue \
30+
--attributes VisibilityTimeout=10

tests/test_outage.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,17 @@ def test_lambda_functions_exist(lambda_client):
3333

3434
def initiate_dynamodb_outage():
3535
outage_payload = [{"service": "dynamodb", "region": "us-east-1"}]
36-
requests.post(CHAOS_ENDPOINT, json=outage_payload)
36+
response = requests.post(CHAOS_ENDPOINT, json=outage_payload)
37+
assert response.ok
3738
return outage_payload
3839

3940
def check_outage_status(expected_status):
4041
outage_status = requests.get(CHAOS_ENDPOINT).json()
4142
assert outage_status == expected_status
4243

4344
def stop_dynamodb_outage():
44-
requests.post(CHAOS_ENDPOINT, json=[])
45+
response = requests.post(CHAOS_ENDPOINT, json=[])
46+
assert response.ok
4547
check_outage_status([])
4648

4749
def test_dynamodb_outage(dynamodb_resource):
@@ -68,11 +70,10 @@ def test_dynamodb_outage(dynamodb_resource):
6870
stop_dynamodb_outage()
6971

7072
# Wait for a few seconds
73+
# Adding a better retry mechanism is left as an exercise
7174
time.sleep(60)
7275

7376
# Query if there are items in DynamoDB table
7477
table = dynamodb_resource.Table(DYNAMODB_TABLE_NAME)
7578
response = table.scan()
7679
items = response["Items"]
77-
print(items)
78-
assert any(item["name"] == "Super Widget" for item in items)

0 commit comments

Comments
 (0)