概要
クラウドのメッセージングサービスはクラウド内リソースのアラート通知だけでなく、オンプレミスやクラウド上にあるサーバーの以下のような運用要件にも低コストでサービス利用することができます。
- バッチ等の実行結果を管理ユーザーにEmail、Slack等に通知
- 正常完了、異常終了、集計値 等
- イベントに連携してプログラム(例:Lambda)を起動
- 次ステップ起動、再処理等
今回はAWS SNSとOracle Cloud Infrastructure(OCI) ONSの機能比較と利用手順を検証しました。
サービス比較
| 項目 | AWS | OCI |
|---|---|---|
| サービス名 | Amazon Simple Notification Service | Oracle Notifications Service |
| 作成方法 | Topicを作成してSubscriptionで通知先を指定 | Topicを作成してSubscriptionで通知先を指定 |
| Subscription | HTTP/S, Email, SMS, Lambda, SQS など | Email, SMS, HTTPS, Functions, Slack など |
| 通知方法 | Console, CLI, SDK | Console, CLI, SDK |
| メッセージ形式 | JSON, Text | JSON, Text |
| 月額費用(Email) | 1,000件まで無料 以後USD2.00/10万通知 | 1,000通まで無料 以後3.10円/1,000回のメール送信 |
作成手順
AWS
事前作業
- AWS IAMユーザーにSNS権限付与(AmazonSNSFullAccess等)
- ~/.aws/credentialsおよびConfig設定
- トピック/サブスクリプションの作成
CLI
- サンプルコード
## Replace << >> with the appropriate value
aws sns publish \
--topic-arn <<topic-arn>> \
--message $SNSBODY \
--subject "sns_message"
- 実行例
$ SNSBODY="snsmessage"
$ aws sns publish \
> --topic-arn arn:aws:sns:ap-northeast-1:******:****** \
> --message $SNSBODY \
> --subject "sns_message"
Python
- 一回のみ実施
- boto3 ライブラリをインストール(例:pip install boto3)
- サンプルコード
snsmessage.py
## Replace << >> with the appropriate value
import sys
import boto3
def main():
try:
snsbody = sys.argv[1]
# Get the SNS client
sns = boto3.client('sns', region_name='ap-northeast-1')
# Topic ARN
topic_arn = '<<topic-arn>>'
# Send a message
response = sns.publish(
TopicArn=topic_arn,
Message=snsbody,
Subject="sns_message"
)
print("Message ID::", response['MessageId'])
except Exception as e:
print('ERROR: bad Event!')
if __name__ == "__main__":
main()
- 実行例
$ python3 snsmessage.py snsmessage
dotnet
- 一回のみ実施
$ dotnet new console -n sns
$ cd sns
$ dotnet add package AWSSDK.SimpleNotificationService
- サンプルコード
## Replace << >> with the appropriate value
using System;
using System.Threading.Tasks;
using Amazon;
using Amazon.SimpleNotificationService;
using Amazon.SimpleNotificationService.Model;
namespace AWS.Sns.Example
{
public class Program
{
public static async Task Main(string[] args)
{
string snsbody = args[1];
// Create an SNS client
var snsClient = new AmazonSimpleNotificationServiceClient(RegionEndpoint.APNortheast1);
// Topic ARN
var topicArn = "<<topic-arn>>";
// Send a message
var request = new PublishRequest
{
TopicArn = topicArn,
Message = snsbody,
Subject = "sns_message"
};
var response = await snsClient.PublishAsync(request);
Console.WriteLine("Message ID: " + response.MessageId);
}
}
}
- 実行例
dotnet run /path/to/sns snsmessage
OCI
事前作業
- OCIポリシーの付与
## Replace << >> with the appropriate value
Allow group <<groupname>> to use ons-topics in compartment <<compartmentname>>
または
Allow group <<groupname>> to use ons-topics in tenancy
- ~/.oci/config設定
- トピック/サブスクリプションの作成
CLI
- サンプルコード
## Replace << >> with the appropriate value
oci ons message publish \
--topic-id <<topic ocid>> \
--title "ons_message" \
--body $ONSBODY
- 実行例
$ ONSBODY="snsmessage"
$ oci ons message publish --topic-id ocid1.onstopic.oc1.***** "ons_message" --body $ONSBODY
Python
- 一回のみ実施
- ociライブラリをインストール(pip install oci)
- サンプルコード
onsmessage.py
## Replace << >> with the appropriate value
import sys
import oci
def main():
try:
onsbody = sys.argv[1]
# Initialize service client with config
from oci.config import from_file
config = from_file()
ons_client = oci.ons.NotificationDataPlaneClient(
config)
# Send the request to service
publish_message_response = ons_client.publish_message(
topic_id="<<topic ocid>>",
message_details=oci.ons.models.MessageDetails(
body= onsbody,
title="ons_message")
)
# Get the data from response
print(publish_message_response.data)
except Exception as e:
print('ERROR: bad Event!')
if __name__ == "__main__":
main()
- 実行例
$ python3 onsmessage.py snsmessage
dotnet
- 一回のみ実施
$ dotnet new console -n ons
$ cd ons
$ dotnet add package OCI.DotNetSDK.Common
$ dotnet add package OCI.DotNetSDK.Ons
- サンプルコード
Program.cs
## Replace << >> with the appropriate value
using System;
using System.Threading.Tasks;
using Oci.OnsService;
using Oci.Common;
using Oci.Common.Auth;
namespace Oci.Sdk.DotNet.Example.Ons
{
public class PublishMessageExample
{
public static async Task Main(string[] args)
{
string onsbody = args[1];
// Create a request and dependent object(s)
var messageDetails = new Oci.OnsService.Models.MessageDetails
{
Title = "ons_message",
Body = onsbody
};
var publishMessageRequest = new Oci.OnsService.Requests.PublishMessageRequest
{
TopicId = "<<topic ocid>>",
MessageDetails = messageDetails
};
// Create a default authentication provider that uses the DEFAULT
var provider = new ConfigFileAuthenticationDetailsProvider("DEFAULT");
try
{
// Create a service client and send the request.
using (var client = new NotificationDataPlaneClient(provider, new ClientConfiguration()))
{
var response = await client.PublishMessage(publishMessageRequest);
// Retrieve value from the response.
var messageIdValue = response.PublishResult.MessageId;
}
}
catch (Exception e)
{
Console.WriteLine($"PublishMessage Failed with {e.Message}");
throw;
}
}
}
}
- 実行例
$ dotnet run /path/to/ons snsmessage