Skip to content

Commit

Permalink
break down init hook into separate parts
Browse files Browse the repository at this point in the history
  • Loading branch information
HarshCasper committed Aug 26, 2024
1 parent 659a64b commit 0b7bd25
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 34 deletions.
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,16 @@ jobs:

- name: Spin up LocalStack
run: |
docker-compose up -d
docker compose up -d
sleep 100
env:
LOCALSTACK_AUTH_TOKEN: ${{ secrets.LOCALSTACK_AUTH_TOKEN }}

- name: Setup the solution
run: |
pip install awscli-local
./setup-solution.sh
- name: Run Integration Tests
run: |
pip3 install boto3 pytest
Expand Down
8 changes: 8 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,14 @@ The output will be:
[{"service": "dynamodb", "region": "us-east-1"}]
```

### Implement error handling

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:

```bash
./setup-solution.sh
```

You can now test the infrastructure again by running the following command:

```bash
Expand Down
29 changes: 0 additions & 29 deletions init-resources.sh
Original file line number Diff line number Diff line change
Expand Up @@ -101,32 +101,3 @@ awslocal apigateway create-deployment \
--rest-api-id $REST_API_ID \
--stage-name dev \
--region=us-east-1

awslocal sns create-topic --name ProductEventsTopic

awslocal sqs create-queue --queue-name ProductEventsQueue

awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn

awslocal sns subscribe \
--topic-arn arn:aws:sns:us-east-1:000000000000:ProductEventsTopic \
--protocol sqs \
--notification-endpoint arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue

awslocal lambda create-function \
--function-name process-product-events \
--runtime java17 \
--handler lambda.DynamoDBWriterLambda::handleRequest \
--memory-size 1024 \
--zip-file fileb:///etc/localstack/init/ready.d/target/product-lambda.jar \
--region us-east-1 \
--role arn:aws:iam::000000000000:role/productRole

awslocal lambda create-event-source-mapping \
--function-name process-product-events \
--batch-size 10 \
--event-source-arn arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue

awslocal sqs set-queue-attributes \
--queue-url http://localhost:4566/000000000000/ProductEventsQueue \
--attributes VisibilityTimeout=10
30 changes: 30 additions & 0 deletions setup-solution.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/bin/sh

awslocal sns create-topic --name ProductEventsTopic

awslocal sqs create-queue --queue-name ProductEventsQueue

awslocal sqs get-queue-attributes --queue-url http://localhost:4566/000000000000/ProductEventsQueue --attribute-names QueueArn

awslocal sns subscribe \
--topic-arn arn:aws:sns:us-east-1:000000000000:ProductEventsTopic \
--protocol sqs \
--notification-endpoint arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue

awslocal lambda create-function \
--function-name process-product-events \
--runtime java17 \
--handler lambda.DynamoDBWriterLambda::handleRequest \
--memory-size 1024 \
--zip-file fileb://lambda-functions/target/product-lambda.jar \
--region us-east-1 \
--role arn:aws:iam::000000000000:role/productRole

awslocal lambda create-event-source-mapping \
--function-name process-product-events \
--batch-size 10 \
--event-source-arn arn:aws:sqs:us-east-1:000000000000:ProductEventsQueue

awslocal sqs set-queue-attributes \
--queue-url http://localhost:4566/000000000000/ProductEventsQueue \
--attributes VisibilityTimeout=10
9 changes: 5 additions & 4 deletions tests/test_outage.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,17 @@ def test_lambda_functions_exist(lambda_client):

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

def check_outage_status(expected_status):
outage_status = requests.get(CHAOS_ENDPOINT).json()
assert outage_status == expected_status

def stop_dynamodb_outage():
requests.post(CHAOS_ENDPOINT, json=[])
response = requests.post(CHAOS_ENDPOINT, json=[])
assert response.ok
check_outage_status([])

def test_dynamodb_outage(dynamodb_resource):
Expand All @@ -68,11 +70,10 @@ def test_dynamodb_outage(dynamodb_resource):
stop_dynamodb_outage()

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

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

0 comments on commit 0b7bd25

Please sign in to comment.