Skip to content
Latchkey

aws sns publish: Send Notifications in CI

aws sns publish --topic-arn <arn> --message <body> sends a message to an SNS topic, and --endpoint-url aims it at LocalStack for offline CI.

SNS fans a message out to subscribers such as SQS queues or HTTP endpoints. In tests you usually publish to a topic and assert the wired SQS queue received it, all against a local emulator.

What it does

aws sns publish posts a message to a topic identified by its ARN; SNS then delivers to every subscription. create-topic returns the ARN, and subscribe wires a protocol/endpoint (for example an SQS queue) to it. --message-attributes carries typed metadata for filtering.

Common usage

Terminal
# create a topic (LocalStack) and capture the ARN
aws --endpoint-url=http://localhost:4566 sns create-topic \
  --name orders
# publish to it
aws --endpoint-url=http://localhost:4566 sns publish \
  --topic-arn arn:aws:sns:us-east-1:000000000000:orders \
  --message '{"id":1}'
# with a message attribute
aws --endpoint-url=http://localhost:4566 sns publish \
  --topic-arn arn:aws:sns:us-east-1:000000000000:orders \
  --message 'hi' \
  --message-attributes '{"kind":{"DataType":"String","StringValue":"order"}}'

Options

FlagWhat it does
--topic-arn <arn>ARN of the topic to publish to
--message <body>Message body
--subject <text>Subject (used for email delivery)
--message-attributes <json>Typed attributes for filtering
--message-group-id <id>Required for FIFO topics
--endpoint-url <url>Target LocalStack instead of AWS

In CI

Capture the ARN from create-topic output rather than hardcoding it, since the account id and region are part of it. For end-to-end tests, subscribe an SQS queue to the topic, publish, then receive from the queue to assert delivery. Export dummy AWS credentials for LocalStack.

Common errors in CI

"NotFound: Topic does not exist" (InvalidParameter) means the --topic-arn is wrong or the topic was never created. "Could not connect to the endpoint URL" means LocalStack is not up on that port. "Unable to locate credentials" means AWS_ACCESS_KEY_ID/SECRET are unset. FIFO topics error without --message-group-id.

Related guides

Run this faster and cheaper on Latchkey managed runners. Start free →