Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MQ broker example #153

Merged
merged 2 commits into from
Oct 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions mq-broker/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
export AWS_ACCESS_KEY_ID ?= test
export AWS_SECRET_ACCESS_KEY ?= test
export AWS_DEFAULT_REGION = us-east-1

usage: ## Show this help
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'

install: ## Install dependencies
@which localstack > /dev/null || pip install localstack
@which awslocal > /dev/null || pip install awscli-local

run: ## Run test creating a broker and sending a message
./test.sh

start:
localstack start -d

stop:
@echo
localstack stop
ready:
@echo Waiting on the LocalStack container...
@localstack wait -t 30 && echo Localstack is ready to use! || (echo Gave up waiting on LocalStack, exiting. && exit 1)

logs:
@localstack logs > logs.txt

test-ci:
make start install ready run; return_code=`echo $$?`;\
make logs; make stop; exit $$return_code;

.PHONY: usage install start run stop ready logs test-ci

45 changes: 45 additions & 0 deletions mq-broker/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# LocalStack Demo: MQ Broker

Simple demo application illustrating the use of MQ using LocalStack.

## Prerequisites

* LocalStack
* Docker
* `make`
* [`awslocal`](https://github.com/localstack/awscli-local)

## Installing

To install the dependencies:
```
make install
```

## Running

Make sure that LocalStack is started:
```
LOCALSTACK_API_KEY=... DEBUG=1 localstack start
```

The following command runs the example, which starts up a broker and sends a message to a queue:
```
make run
```

After the test script completes, the logs in your terminal should look similar to the output below:
```
$ make run
Creating MQ broker in LocalStack ...
Created MQ broker with id: b-7dc2ba4a-53a0-41ef-a2ad-92eac3ad879d
Describe broker to get the endpoint
Broker endpoint on http://localhost:4510
Sending message to broker
Message sentCleaning up - deleting broker
Deleted Broker b-7dc2ba4a-53a0-41ef-a2ad-92eac3ad879d
```

## License

This code is available under the Apache 2.0 license.
20 changes: 20 additions & 0 deletions mq-broker/test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash

echo "Creating MQ broker in LocalStack ..."
UUID=$(echo $RANDOM | md5sum | head -c 20)
broker_name="broker_${UUID}"
broker_id=$(awslocal mq create-broker --broker-name $broker_name --deployment-mode SINGLE_INSTANCE --engine-type ACTIVEMQ --engine-version='5.16.5' --host-instance-type 'mq.t2.micro' --auto-minor-version-upgrade --publicly-accessible --users='{"ConsoleAccess": true, "Groups": ["testgroup"],"Password": "QXwV*$iUM9USHnVv&!^7s3c@", "Username": "admin"}' | jq -r '.BrokerId')
echo "Created MQ broker with id: ${broker_id}"

# let broker fully start up
sleep 1
echo "Describe broker to get the endpoint"
broker_endpoint=$(awslocal mq describe-broker --broker-id $broker_id | jq -r '.BrokerInstances[0].ConsoleURL')
echo "Broker endpoint on ${broker_endpoint}"

echo "Sending message to broker"
curl -XPOST -d "body=message" http://admin:admin@${broker_endpoint:7}/api/message\?destination\=queue://orders.input

echo $"Cleaning up - deleting broker"
broker_id=$(awslocal mq delete-broker --broker-id $broker_id | jq -r '.BrokerId')
echo "Deleted Broker ${broker_id}"