Module aws.sns

ballerinax/aws.sns Ballerina library

4.0.2

Overview

Amazon SNS (Simple Notification Service) is a fully managed messaging service for both application-to-application (A2A) and application-to-person (A2P) communication. It enables developers to send notifications from their applications to various endpoints, including email, SMS, and mobile push, as well as to other AWS services like SQS, Lambda, and HTTP/S webhooks.

The Amazon SNS connector offers APIs to connect and interact with Amazon SNS REST API endpoints.

Key Features

  • Create, list, and delete topics
  • Manage subscriptions and permissions
  • Publish messages to topics or directly to endpoints
  • Support for SMS and mobile push notifications
  • Flexible credential configuration: static keys, AWS credentials file profiles, or the default AWS credential provider chain (EKS Pod Identity, ECS task roles, EC2 instance profiles, environment variables)
  • GraalVM compatible for native image builds

Setup guide

Obtain IAM user credentials

To create an IAM user and generate an access key, follow the obtaining IAM user credentials guide.

Attach the SNS permissions your application needs to the user — the AWS managed AmazonSNSFullAccess policy grants full access, or scope a custom policy to only the SNS actions you call (for example, sns:CreateTopic, sns:Publish, and sns:Subscribe).

Quickstart

To use the aws.sns connector in your Ballerina project, modify the .bal file as follows:

Step 1: Import the connector

Import the ballerinax/aws and ballerinax/aws.sns packages into your Ballerina project.

Copy
import ballerinax/aws;
import ballerinax/aws.sns;

Step 2: Instantiate a new connector

Create a new sns:Client by providing the region and authentication configurations.

Copy
sns:Client snsClient = check new ({
   auth: {
      accessKeyId: "<AWS_ACCESS_KEY_ID>",
      secretAccessKey: "<AWS_SECRET_ACCESS_KEY>"
   },
   region: aws:US_EAST_1
});

Step 3: Invoke the connector operation

Now, utilize the available connector operations.

Copy
string topicArn = check snsClient->createTopic("FirstTopic");

Step 4: Run the Ballerina application

Use the following command to compile and run the Ballerina program.

Copy
bal run

Alternative authentication methods

Profile-based authentication

You can use AWS profile-based authentication as an alternative to static credentials.

Copy
sns:Client snsClient = check new ({
   auth: {
      profileName: "<PROFILE_NAME>",
      credentialsFilePath: "~/.aws/credentials"
   },
   region: aws:US_EAST_1
});

Default credential provider chain

The standard default credential provider chain, trying each of the following in order and taking the first source that yields credentials:

  1. Environment variables (AWS_ACCESS_KEY_ID/AWS_SECRET_ACCESS_KEY, and AWS_WEB_IDENTITY_TOKEN_FILE if set)
  2. The shared config/credentials file's active profile (AWS_PROFILE, or default if unset) — which may itself resolve via SSO, an external process, or a chained AssumeRole call, depending on that profile's configuration
  3. Container credentials (ECS/EKS)
  4. EC2 instance profile (IMDS)

This is the recommended option when the application runs on AWS infrastructure, since no long-lived credentials need to be stored with the application.

Copy
sns:Client snsClient = check new ({
   auth: auth:DEFAULT_CREDENTIALS,
   region: aws:US_EAST_1
});

Note: Beyond the three options above, the auth field also accepts auth:AssumeRoleConfig (STS assume-role), auth:WebIdentityConfig (web identity / OIDC), auth:SsoAuthConfig (IAM Identity Center), and auth:ProcessAuthConfig (external credential process). See the Ballerina AWS documentation for details.

Examples

The sns connector provides practical examples illustrating usage in various scenarios. Explore these examples.

  1. Football scores This example shows how to use SNS to implement an application to subscribe to receive football game scores.

  2. Weather alert service This example shows how to use SNS to send weather alerts for multiple cities. Users can subscribe to different cities to receive alerts for their city only.

Import

import ballerinax/aws.sns;Copy

Other versions

See more...

Metadata

Released date: about 12 hours ago

Version: 4.0.2

License: Apache-2.0


Compatibility

Platform: any

Ballerina version: 2201.12.0

GraalVM compatible: Yes


Pull count

Total: 3051

Current verison: 0


Weekly downloads


Source repository


Keywords

Communication/Push Notifications

Cost/Paid

Vendor/Amazon

Area/Communication

Type/Connector


Contributors