[Yandex Cloud documentation](../../index.md) > [Yandex Cloud Notification Service](../index.md) > [Tools](index.md) > AWS CLI

# Getting started with Yandex Cloud Notification Service using AWS CLI

{% note info %}

The service is at the [preview stage](../../overview/concepts/launch-stages.md).

{% endnote %}

To get started with the AWS CLI:
1. [Get your cloud ready](#before-you-begin).
1. [Get a static access key](#static-key).
1. [Configure the AWS CLI](#aws-cli).
1. [Create a notification channel](#create-channel).
1. [Get a list of channels](#list-channel).
1. [Create an endpoint](#create-endpoint).
1. [Send a notification](#publish).

## Get your cloud ready {#before-you-begin}

Sign up for Yandex Cloud and create a [billing account](../../billing/concepts/billing-account.md):
1. Navigate to the [management console](https://console.yandex.cloud) and log in to Yandex Cloud or create a new account.
1. On the **[Yandex Cloud Billing](https://center.yandex.cloud/billing/accounts)** page, make sure you have a billing account linked and it has the `ACTIVE` or `TRIAL_ACTIVE` [status](../../billing/concepts/billing-account-statuses.md). If you do not have a billing account, [create one](../../billing/quickstart/index.md) and [link](../../billing/operations/pin-cloud.md) a cloud to it.

If you have an active billing account, you can create or select a [folder](../../resource-manager/concepts/resources-hierarchy.md#folder) for your infrastructure on the [cloud page](https://console.yandex.cloud/cloud).

[Learn more about clouds and folders here](../../resource-manager/concepts/resources-hierarchy.md).

## Get a static access key {#static-key}

For authentication in Cloud Notification Service, use a [static access key](../../iam/concepts/authorization/access-key.md). The key is issued for the [service account](../../iam/concepts/users/service-accounts.md), and all actions are performed on behalf of that service account.

To get a static access key:
1. [Create](../../iam/operations/sa/create.md) a service account.
1. [Assign](../../iam/operations/sa/assign-role-for-sa.md) the `editor` [role](../../iam/roles-reference.md#editor) for the folder to the service account.
1. [Create](../../iam/operations/authentication/manage-access-keys.md#create-access-key) a static access key for the service account.

    Save the ID and secret key.

## Configure the AWS CLI {#aws-cli}

The [AWS CLI](https://aws.amazon.com/cli/) is a command line interface designed for AWS services. The Cloud Notification Service HTTP API is compatible with the [Amazon SNS API](https://docs.aws.amazon.com/sns/latest/api/welcome.html). To learn [how to run commands](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/index.html), see the official Amazon documentation.

To configure the AWS CLI:
1. [Install](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) the AWS CLI.
1. Enter the command to configure the AWS CLI:

   ```bash
   aws configure
   ```

1. Enter the values for these parameters:
    * `AWS Access Key ID`: Static key ID
    * `AWS Secret Access Key`: Secret key
    * `Default region name`: `ru-central1`

      {% note info %}

      To work with Cloud Notification Service, always specify the `ru-central1`. A different region value may lead to an authorization error.

      {% endnote %}

1. Leave the other parameters unchanged.
1. Set the Cloud Notification Service endpoint:

    ```bash
    aws configure set endpoint_url https://notifications.yandexcloud.net/
    ```
   To check the endpoint value in the settings, use this command:

    ```bash
    aws configure get endpoint_url
    ```
   
   By using `configure set` and `configure get`, you can manage settings in the `~/.aws/config` configuration file. You can also configure parameters via [environment variables](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-endpoints.html#endpoints-global).

    {% note warning %}

     Since there are multiple ways to set an endpoint, each of these ways has a defined [priority](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-envvars.html#envvars-list-AWS_ENDPOINT_URL). Environment variables override configuration file settings.
     
     To prevent conflicts, use `echo $AWS_ENDPOINT_URL` to check the environment variable and `aws configure get endpoint_url` to verify the configuration file parameter.

     Also, make sure there are no conflicts between the configuration (the `~/.aws/credentials` file) and the `AWS_DEFAULT_REGION`, `AWS_ACCESS_KEY_ID`, and `AWS_SECRET_ACCESS_KEY` environment variables.
     
     {% endnote %}

{% cut "Example of configuration files" %}

* `~/.aws/config`:

  ```text
  [default]
  region = ru-central1
  endpoint_url = https://notifications.yandexcloud.net/
  ```

* `~/.aws/credentials`:

  ```text
  [default]
  aws_access_key_id = <static_key_ID>
  aws_secret_access_key = <secret_key>
  ```

{% endcut %}

For more information about setting up the AWS CLI, see [this AWS guide](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-quickstart.html).

## Create a notification channel {#create-channel}

To create a [notification channel](../concepts/index.md#channels), run this command:

```bash
aws sns create-platform-application \
  --name <channel_name> \
  --platform <platform_type> \
  --attributes <parameter1>=<value1>, ... <parameterN>=<valueN>
```

Where:

* `--name`: Notification channel name, user-defined.
  
  The name must be unique throughout CNS. Once the channel is created, you will not be able to change the name. The name may contain lowercase and uppercase Latin letters, numbers, underscores, hyphens, and periods. It must be from 1 to 256 characters long. For APNs channels, we recommend specifying the bundle ID in the name; for FCM and HMS, the full package name; for RuStore, `packageName`.

* `--platform`: Mobile platform type:

  * `APNS` and `APNS_SANDBOX`: Apple Push Notification service (APNs). Use `APNS_SANDBOX` to test the application.
  * `GCM`: Firebase Cloud Messaging (FCM).
  * `HMS`: Huawei Mobile Services (HMS).
  * `RUSTORE`: RuStore Android.

* `--attributes`: Mobile platform authentication parameters in `<parameter>=<value>` format, comma-separated. The values depend on the platform:

  * APNs:

    * Token-based authentication:

      * `PlatformPrincipal`: Path to the signature key file from Apple.
      * `PlatformCredential`: Key ID.
      * `ApplePlatformTeamID`: Team ID.
      * `ApplePlatformBundleID`: Bundle ID.

    * Certificate-based authentication:

      * `PlatformPrincipal`: SSL certificate in `.pem` format.
      * `PlatformCredential`: Certificate private key in `.pem` format.

        To save the certificate and the private key in individual `.pem` files, use the [openssl](https://docs.openssl.org/1.1.1/man1/pkcs12) Linux utility:
        
        ```bash
        openssl pkcs12 -in Certificates.p12 -nokeys -nodes -out certificate.pem
        openssl pkcs12 -in Certificates.p12 -nocerts -nodes -out privatekey.pem
        ```

    Token-based authentication is preferred as a more modern option.

  * FCM: `PlatformCredential` is the Google Cloud service account key in JSON format for authentication with the HTTP v1 API or API key (server key) for authentication with the legacy API.

    We recommend escaping the file contents using the `jq @json <<< cat private_key.json` command, as the AWS CLI accepts this parameter in string format.

    The HTTP v1 API is preferred as [FCM will no longer support](https://firebase.google.com/docs/cloud-messaging/migrate-v1) the legacy API starting from June 2024.

  * HMS:

    * `PlatformPrincipal`: Key ID.
    * `PlatformCredential`: API key.

  * RuStore:
  
    * `PlatformPrincipal`: Project ID (ProjectID).
    * `PlatformCredential`: Service token (ServiceToken).

As a result, you will get a notification channel ID (ARN). Save it for future use.

Learn more about the `aws sns create-platform-application` command in [this AWS guide](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/create-platform-application.html).


## Get a list of notification channels {#list-channel}

```bash
aws sns list-platform-applications
```

You will get the list of notification channels located in the same folder as the service account.

Learn more about the `aws sns list-platform-applications` command in [this AWS guide](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/list-platform-applications.html).

## Create an endpoint {#create-endpoint}

To create a [mobile endpoint](../concepts/index.md#mobile-endpoints), run the following command:

```bash
aws sns create-platform-endpoint \
  --platform-application-arn <notification_channel_ARN> \
  --token <push_token>
```

Where:

* `--platform-application-arn`: Notification channel ID (ARN).
* `--token`: Unique push token for the application on the user’s device.

As a result, you will get a mobile endpoint ID (ARN). Save it for future use.

Learn more about the `aws sns create-platform-endpoint` command in [this AWS guide](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/create-platform-endpoint.html).


## Send a notification {#publish}

### Explicit notifications (Bright Push) {#bright-push}

{% list tabs %}

- Apple iOS (APNs)

  ```bash
  aws sns publish \
    --target-arn "<endpoint_ARN>" \
    --message-structure json \
    --message '{"default": "<notification_text>", "APNS": "{ \"aps\": { \"alert\": \"<notification_text>\"} }" }'
  ```

- Google Android (FCM)

  ```bash
  aws sns publish \
    --target-arn "<endpoint_ARN>" \
    --message-structure json \
    --message '{"default": "<notification_text>", "GCM": "{ \"notification\": { \"body\": \"<notification_text>\"} }" }'
  ```

{% endlist %}

Where:
* `--target-arn`: Mobile endpoint ID (ARN).
* `--message-structure`: Message format.
* `--message`: Message.

### Silent notifications (Silent Push) {#silent-push}

```bash
aws sns publish \
  --target-arn <endpoint_ARN> \
  --message-structure json \
  --message '{"data": { "key": "value" } }'
```

Where:
* `--target-arn`: Mobile endpoint ID (ARN).
* `--message-structure`: Message format.
* `--message`: Message.

Learn more about the `aws sns publish` command in [this AWS guide](https://awscli.amazonaws.com/v2/documentation/api/latest/reference/sns/publish.html).

## Examples {#examples}

### Sending SMS {#sms-sending}

```bash
aws sns publish \
  --phone-number "<recipient_phone_number>" \
  --message "<notification_text>" \
  --message-attributes '{"AWS.SNS.SMS.SenderID": {"DataType": "String", "StringValue": "<sender's_text_name>"} }'
```

Where:
* `--phone-number`: Russian phone number in [E.164](https://en.wikipedia.org/wiki/E.164) format.
* `--message`: SMS text.
* `--message-attributes`: SMS attributes, where `AWS.SNS.SMS.SenderID.StringValue` is the sender's text name.

#### Useful links {#see-also}

* [Getting started](../quickstart.md)
* [AWS SDK for Python (boto3)](sdk-python.md)
* [Concepts](../concepts/index.md)