Amazon Linux 2023 | High-Performance Messaging | Persistence
Complete AMI guide for your NATS with JetStream deployment. Learn how to connect, configure streams, create consumers, and manage your messaging infrastructure.
Follow these steps to access your NATS server and start using JetStream.
Connect to your EC2 instance using SSH with your key pair.
ssh -i your-key.pem ec2-user@<your-instance-ip>
View the auto-generated NATS authentication credentials.
sudo cat /etc/nats/credentials.txt
Connect to NATS using the NATS CLI or client libraries.
# On the instance (localhost):
nats context select local
nats server info
# From remote:
nats -s nats://<your-instance-ip>:4222 server info
Confirm JetStream is enabled and check account info.
# Check JetStream status:
nats account info
# List existing streams:
nats stream list
All credentials are automatically generated and stored securely on your instance.
sudo cat /etc/nats/credentials.txt
nats://<username>:<password>@<your-instance-ip>:4222
JetStream provides persistence, replay, and exactly-once delivery for NATS messaging.
Streams capture and store messages for subjects.
# Create a stream interactively:
nats stream add ORDERS
# Or with flags:
nats stream add ORDERS \
--subjects "orders.>" \
--retention limits \
--max-msgs 1000000 \
--max-bytes 1GB \
--storage file
Consumers read messages from streams.
# Create a durable consumer:
nats consumer add ORDERS ORDER_PROCESSOR \
--filter "orders.created" \
--ack explicit \
--deliver all \
--replay instant \
--max-deliver 3
Send messages to your stream subjects.
# Publish a message:
nats pub orders.created '{"id": 1, "item": "widget"}'
# Publish with headers:
nats pub orders.created \
--header "priority:high" \
'{"id": 2, "item": "gadget"}'
Read and acknowledge messages from consumers.
# Pull next message:
nats consumer next ORDERS ORDER_PROCESSOR
# Subscribe continuously:
nats consumer sub ORDERS ORDER_PROCESSOR
View all configured streams and their status.nats stream list
Get detailed stream statistics.nats stream info ORDERS
Browse messages in a stream.nats stream view ORDERS
Remove all messages from a stream.nats stream purge ORDERS
Remove a stream entirely.nats stream rm ORDERS
Check consumer state and pending messages.nats consumer info ORDERS ORDER_PROCESSOR
orders.> to capture all order-related messages.
Monitor your NATS server health and performance.
Access real-time server statistics via HTTP on port 8222.
Use the NATS CLI for detailed server insights.
# Server info:
nats server info
# Account info:
nats account info
# Active connections:
nats server report connections
# JetStream report:
nats server report jetstream
curl http://localhost:8222/healthz
Follow these steps to secure your NATS deployment.
Important: Change the default credentials immediately after your first login. The pre-generated passwords should only be used for initial access.
Change default username/password in the NATS configuration file.
Restrict port 4222, 6222, and 8222 access to trusted IPs only.
Enable TLS for encrypted client connections in production.
sudo nano /etc/nats/nats-server.conf
# After editing, restart NATS:
sudo systemctl restart nats-server
Common issues and how to resolve them.
Symptom: Connection refused or timeout when connecting from your machine.
Solution:
4222sudo systemctl status nats-serverSymptom: "Authorization Violation" error when connecting.
Solution:
sudo cat /etc/nats/credentials.txtSymptom: JetStream operations fail or return errors.
Solution:
nats account infodf -h /var/lib/natsSymptom: NATS service fails to start.
Solution:
sudo journalctl -u nats-server -n 50nats-server -c /etc/nats/nats-server.conf -tsudo netstat -tlnp | grep 4222Common questions and quick solutions.
sudo systemctl restart nats-server
sudo journalctl -u nats-server -f
/var/lib/nats/jetstream
sudo systemctl status nats-server
/etc/nats/nats-server.conf
Use EBS snapshots or backup the JetStream directory:
sudo tar -czf nats-backup.tar.gz /var/lib/nats/jetstream
Choose the right EC2 instance type for your workload.
| Use Case | Instance Type | vCPUs | RAM | Recommended For |
|---|---|---|---|---|
| Development | t3.small |
2 | 2 GB | Testing, development, low-volume messaging |
| Small Production | t3.medium |
2 | 4 GB | Small apps, moderate message throughput |
| Medium Production | m6i.large |
2 | 8 GB | Business applications, consistent load |
| Large Production | m6i.xlarge |
4 | 16 GB | High throughput, many connections |
| High Performance | c6i.xlarge |
4 | 8 GB | Ultra-low latency, high message rates |
Our team is available 24/7 to assist you with any questions or issues.
support@flowopsconsulting.co.uk