AmazonSNSClient is the class from the legacy AWS SDK for Java 1.x that a Spring Boot application used to call Amazon Simple Notification Service (SNS): publishing messages to topics, creating topics and managing subscriptions. It is a publisher-side client; SNS then pushes messages to subscribers such as SQS queues, Lambda functions, HTTP endpoints, email and SMS.
AWS SDK for Java 1.x is end-of-support, so new code should use SnsClient from AWS SDK for Java 2.x, or SnsTemplate from Spring Cloud AWS. This article explains the purpose of the old class and shows the current replacement.
SNS is a managed publish/subscribe service. A producer publishes one message to a topic, and SNS fans it out to every subscription. The client object is the thin wrapper around the SNS HTTP API. It signs requests with your AWS credentials and exposes operations such as publish, createTopic, subscribe and listTopics.
Note that the client does not receive messages. Receiving happens at the subscriber, for example a Spring application listening on an SQS queue that is subscribed to the topic.
In SDK v1 the class was com.amazonaws.services.sns.AmazonSNSClient, normally created through AmazonSNSClientBuilder:
The AmazonSNSClient constructors are deprecated in favour of the builder, and the whole v1 SDK is in end-of-support, so it receives no further fixes. Migrate existing code instead of extending it.
Add the SDK BOM and the SNS module:
Define the client as a bean. It is immutable, thread-safe and holds connections, so create one and share it. Spring calls its close() method when the context shuts down.
With no explicit credentials the SDK uses its default provider chain: environment variables, shared profile, container credentials, or an IAM role on EC2, ECS or EKS. Avoid hard-coding access keys.
The eventType attribute lets subscribers use filter policies to receive only the events they care about. For FIFO topics (names ending in .fifo) you must also set messageGroupId, and messageDeduplicationId unless content-based deduplication is enabled.
In production, create topics and subscriptions with infrastructure as code (CloudFormation, Terraform or CDK) and let the application only publish. The application role then needs just sns:Publish on that topic.
Spring Cloud AWS provides SnsTemplate, a higher-level abstraction that converts payload objects to JSON and sends them with headers:
Check the Spring Cloud AWS release notes for the version compatible with your Spring Boot line. It builds on SDK v2.
Use LocalStack with Testcontainers and set the client's endpointOverride to the container URL, so tests exercise real SNS API calls without an AWS account.
Its public constructors were deprecated in favour of the builder, and the AWS SDK for Java 1.x has reached end of support. Use SnsClient from SDK v2 for new work.
Subscribe an SQS queue (or an HTTPS endpoint) to the topic, then consume with an SQS listener such as Spring Cloud AWS @SqsListener.
From the default credentials provider chain. Prefer IAM roles over static keys.
No. Create one client bean and reuse it, because it is thread-safe and manages a connection pool.
AmazonSNSClient was the SDK v1 entry point for talking to Amazon SNS from Spring Boot. Today, use SnsClient from AWS SDK for Java 2.x, or Spring Cloud AWS SnsTemplate, as a shared bean; publish to topic ARNs with message attributes, let subscribers receive messages, and give the application a minimal IAM policy.