This is a simple project that demonstrates how to use LocalStack with Golang It shows a fan-out messaging pattern using S3, SNS, and SQS.
-
When LocalStack starts (
make start-deps), the script./aws-init/init.shis executed. It does the following:- Creates an S3 bucket called
awesome-bucket - Creates an SNS topic called
awesome-sns-topic - Configures the SNS topic to allow S3 to publish messages to it
- Adds a notification configuration so that any file uploaded to the S3 bucket with the prefix
new-files/and suffix.txttriggers a message to the SNS topic - Creates three SQS queues, with
VisibilityTimeoutset to 30 andReceiveMessageWaitTimeSecondsset to 20 (for long polling) - Subscribes the three queues to the SNS topic (the famous fan-out pattern)
- Creates an S3 bucket called
-
Once the infrastructure is set up, you can run the server, This is a simple app with two endpoints to send files to S3:
- Run the app:
make start-server
-
Send one file:
make create-file
Can send custom text too:
make create-file text="Hello World" -
Send multiple files: (It creates 10 files by default)
make create-files
Or can specify a number of files:
make create-files n=20
-
Start the poller:
make start-poller
-
The poller executes
ReceiveMessageon every queue configured and prints the file key. (Each queue has a different configuration so you can see how they behave. Nothing fancy)
To sum up:
Each uploaded file triggers an S3 event → SNS → SQS → Poller prints the S3 file key.
Note:
The commands make start-server and make start-poller run the services directly on your machine. To run everything inside Docker, use:
make start- A couple of tests wouldn’t hurt, lol.