Home
Available on AWS Marketplace

NATS with JetStream

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.

NATS Server 2.15.x JetStream Enabled Amazon Linux 2023

Quick Start Guide

Follow these steps to access your NATS server and start using JetStream.

1

SSH to Your Instance

Connect to your EC2 instance using SSH with your key pair.

ssh -i your-key.pem ec2-user@<your-instance-ip>
2

Find Your Credentials

View the auto-generated NATS authentication credentials.

sudo cat /etc/nats/credentials.txt
3

Connect to NATS

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
4

Verify JetStream

Confirm JetStream is enabled and check account info.

# Check JetStream status: nats account info # List existing streams: nats stream list

Credentials & Connection Details

All credentials are automatically generated and stored securely on your instance.

NATS Server

  • Client Port: 4222
  • Monitoring Port: 8222
  • Cluster Port: 6222
  • Authentication: See credentials file

JetStream

  • Status: Enabled by default
  • Storage: /var/lib/nats/jetstream
  • Persistence: File-based

Credentials File Location

sudo cat /etc/nats/credentials.txt

Connection URL Format

nats://<username>:<password>@<your-instance-ip>:4222

Working with JetStream

JetStream provides persistence, replay, and exactly-once delivery for NATS messaging.

1

Create a Stream

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
2

Create a Consumer

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
3

Publish Messages

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"}'
4

Consume Messages

Read and acknowledge messages from consumers.

# Pull next message: nats consumer next ORDERS ORDER_PROCESSOR # Subscribe continuously: nats consumer sub ORDERS ORDER_PROCESSOR

Common JetStream Operations

List Streams

View all configured streams and their status.
nats stream list

Stream Info

Get detailed stream statistics.
nats stream info ORDERS

View Messages

Browse messages in a stream.
nats stream view ORDERS

Purge Stream

Remove all messages from a stream.
nats stream purge ORDERS

Delete Stream

Remove a stream entirely.
nats stream rm ORDERS

Consumer Info

Check consumer state and pending messages.
nats consumer info ORDERS ORDER_PROCESSOR

JetStream Tips

  • Use wildcard subjects like orders.> to capture all order-related messages.
  • Choose file storage for durability or memory storage for speed.
  • Set appropriate retention policies (limits, interest, workqueue) based on your use case.
  • Use durable consumers for persistent subscriptions that survive restarts.

Monitoring & Health Checks

Monitor your NATS server health and performance.

Monitoring Endpoints

Access real-time server statistics via HTTP on port 8222.

  • Health: http://<ip>:8222/healthz
  • General: http://<ip>:8222/varz
  • Connections: http://<ip>:8222/connz
  • JetStream: http://<ip>:8222/jsz

CLI Monitoring

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

Check Server Health

curl http://localhost:8222/healthz

Security Recommendations

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.

Update Credentials

Change default username/password in the NATS configuration file.

Security Groups

Restrict port 4222, 6222, and 8222 access to trusted IPs only.

TLS Encryption

Enable TLS for encrypted client connections in production.

NATS Configuration File

sudo nano /etc/nats/nats-server.conf # After editing, restart NATS: sudo systemctl restart nats-server

Troubleshooting

Common issues and how to resolve them.

Can't Connect Remotely

Symptom: Connection refused or timeout when connecting from your machine.

Solution:

  • 1. Check your EC2 Security Group allows inbound on port 4222
  • 2. Verify your IP is whitelisted in the security group
  • 3. Ensure NATS is running: sudo systemctl status nats-server

Authentication Failed

Symptom: "Authorization Violation" error when connecting.

Solution:

  • 1. Verify credentials: sudo cat /etc/nats/credentials.txt
  • 2. Check NATS config for correct auth settings
  • 3. Ensure you're using the correct username and password

JetStream Unavailable

Symptom: JetStream operations fail or return errors.

Solution:

  • 1. Check JetStream status: nats account info
  • 2. Verify storage directory permissions
  • 3. Check disk space: df -h /var/lib/nats

Server Won't Start

Symptom: NATS service fails to start.

Solution:

  • 1. Check logs: sudo journalctl -u nats-server -n 50
  • 2. Validate config: nats-server -c /etc/nats/nats-server.conf -t
  • 3. Check for port conflicts: sudo netstat -tlnp | grep 4222

Frequently Asked Questions

Common questions and quick solutions.

How do I restart NATS?

sudo systemctl restart nats-server

How do I check NATS logs?

sudo journalctl -u nats-server -f

Where is JetStream data stored?

/var/lib/nats/jetstream

How do I check service status?

sudo systemctl status nats-server

Where is the NATS config file?

/etc/nats/nats-server.conf

How do I backup JetStream data?

Use EBS snapshots or backup the JetStream directory:

sudo tar -czf nats-backup.tar.gz /var/lib/nats/jetstream

Instance Sizing Guide

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

Sizing Tips

  • NATS is CPU-efficient; focus on network throughput for high-volume messaging.
  • JetStream persistence benefits from fast SSD storage (gp3 or io2 EBS).
  • For production, use c6i or m6i instances for consistent performance.

Need Help?

Our team is available 24/7 to assist you with any questions or issues.

support@flowopsconsulting.co.uk
24/7 Support Available