Module aws.sns

ballerinax/aws.sns Ballerina library

3.2.0

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

Step 1: Create an AWS account

  • If you don't already have an AWS account, you need to create one. Go to the AWS Management Console, click on "Create a new AWS Account," and follow the instructions.

Step 2: Get the access key ID and the secret access key

Once you log in to your AWS account, you need to create a user group and a user with the necessary permissions to access SNS. To do this, follow the steps below:

  1. Create an AWS user group
  • Navigate to the Identity and Access Management (IAM) service. Click on "Groups" and then "Create New Group."

    Create user group
  • Enter a group name and attach the necessary policies to the group. For example, you can attach the "AmazonSNSFullAccess" policy to provide full access to SNS.

    Attach policy
  1. Create an IAM user
  • In the IAM console, navigate to "Users" and click on "Add user."

    Add user
  • Enter a username, tick the "Provide user access to the AWS Management Console - optional" checkbox, and click "I want to create an IAM user". This will enable programmatic access through access keys.

    Create IAM user
  • Click through the permission setup, and add the user to the user group we previously created.

    Attach user group
  • Review the details and click "Create user."

    Review user
  1. Generate access key ID and secret access key
  • Once the user is created, you will see a success message. Navigate to the "Users" tab, and select the user you created.

    View User
  • Click on the "Create access key" button to generate the access key ID and secret access key.

    Create access key
  • Follow the steps and download the CSV file containing the credentials.

    Download credentials

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.sns package into your Ballerina project.

Copy
import ballerinax/aws.sns;

Step 2: Instantiate a new connector

The sns:Client accepts a ConnectionConfig with a credentials field that supports three authentication modes.

Option 1: Static credentials

Use explicit AWS credentials. Suitable for local development and environments where credentials are managed directly.

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

For temporary credentials (e.g., from aws sts get-session-token), include the session token:

Copy
sns:Client snsClient = check new ({
    credentials: {
        accessKeyId: "<AWS_ACCESS_KEY_ID>",
        secretAccessKey: "<AWS_SECRET_ACCESS_KEY>",
        sessionToken: "<AWS_SESSION_TOKEN>"
    },
    region: "<AWS_REGION>"
});

Option 2: AWS credentials file profile

Use a named profile from your ~/.aws/credentials file. Suitable for developer workstations with multiple AWS accounts.

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

Option 3: Default credential provider chain

Use sns:DEFAULT_CREDENTIALS to let the connector automatically resolve credentials from the environment. This is the recommended approach for AWS-managed environments.

Copy
sns:Client snsClient = check new ({
    credentials: sns:DEFAULT_CREDENTIALS,
    region: "<AWS_REGION>"
});

The credential resolution order is:

  1. Environment variables (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN)
  2. Container credentials endpoint — EKS Pod Identity (AWS_CONTAINER_CREDENTIALS_FULL_URI + AWS_CONTAINER_AUTHORIZATION_TOKEN_FILE) and ECS task roles
  3. EC2 instance metadata service (IMDS / instance profiles)
  4. AWS credentials file default profile (~/.aws/credentials)

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

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: 5 days ago

Version: 3.2.0

License: Apache-2.0


Compatibility

Platform: java21

Ballerina version: 2201.11.0

GraalVM compatible: Yes


Pull count

Total: 2717

Current verison: 4


Weekly downloads


Source repository


Keywords

Communication/Push Notifications

Cost/Paid

Vendor/Amazon

Area/Communication

Type/Connector


Contributors