Ballerina Amazon SNS Connector

March 31, 2026 ยท View on GitHub

Build codecov GitHub Last Commit GraalVM Check License

Amazon SNS is a message notification service provided by Amazon.com Inc., enabling users to publish messages to topics, which are then delivered to subscribing endpoints or clients.

Overview

The ballerinax/aws.sns package offers APIs to connect and interact with AWS SNS API endpoints.

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
  1. Create an IAM user
  1. Generate access key ID and secret access key

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.

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.

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:

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.

sns:Client snsClient = check new ({
    credentials: {
        profileName: "my-profile",
        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.

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

Credentials are resolved using the AWS SDK for Java 2.x DefaultCredentialsProvider.create() chain. Refer to the AWS SDK documentation for the current resolution order.

Step 3: Invoke the connector operation

Now, utilize the available connector operations.

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

Step 4: Run the Ballerina application

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

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.

  3. Pod Identity / default credentials This example shows how to use DEFAULT_CREDENTIALS to run the connector without explicit credentials โ€” works with EKS Pod Identity, ECS task roles, EC2 instance profiles, and AWS environment variables.

Issues and projects

The Issues and Projects tabs are disabled for this repository as this is part of the Ballerina library. To report bugs, request new features, start new discussions, view project boards, etc., visit the Ballerina library parent repository.

This repository only contains the source code for the package.

Building from the source

Prerequisites

  1. Download and install Java SE Development Kit (JDK) version 21. You can download it from either of the following sources:

    Note: After installation, remember to set the JAVA_HOME environment variable to the directory where JDK was installed.

  2. Download and install Ballerina Swan Lake.

  3. Download and install Docker.

    Note: Ensure that the Docker daemon is running before executing any tests.

  4. Generate a Github access token with read package permissions, then set the following env variables:

    export packageUser=<Your GitHub Username>
    export packagePAT=<GitHub Personal Access Token>
    

Build options

Execute the commands below to build from the source.

  1. To build the package:

    ./gradlew clean build
    
  2. To run the tests:

    ./gradlew clean test
    
  3. To build the without the tests:

    ./gradlew clean build -x test
    
  4. To debug package with a remote debugger:

    ./gradlew clean build -Pdebug=<port>
    
  5. To debug with Ballerina language:

    ./gradlew clean build -PbalJavaDebug=<port>
    
  6. Publish the generated artifacts to the local Ballerina central repository:

    ./gradlew clean build -PpublishToLocalCentral=true
    
  7. Publish the generated artifacts to the Ballerina central repository:

    ./gradlew clean build -PpublishToCentral=true
    

Contributing to Ballerina

As an open source project, Ballerina welcomes contributions from the community.

For more information, go to the contribution guidelines.

Code of conduct

All contributors are encouraged to read the Ballerina Code of Conduct.