Node.jsでSNSサブスクリプションを登録削除する方法
SNSのサブスクリプションをnode.jsで登録してみます。
Javaだとhttp,httpsプロトコルの場合は「保留中の確認」でフィルターポリシーを設定するとエラーとなるのですが、node.jsだとエラーにならずに無視されて正常終了(保留中の確認)しました。
サブスクリプション登録には、sns.subscribeメソッドを使用します。
引数に渡すパラメータは以下です。
1 2 3 4 5 6 7 8 9 |
const params = { Protocol: 'https', // http,sqs,lambda,...etc TopicArn: 'arn:aws:sns:ap-northeast-1:xxxxxxxxxxxxx:topicname', // SNSのトピックのARN Attributes: { 'FilterPolicy': '{"notifytype":["LIFE"]}' }, // 設定しても無視されてエラーとならない Endpoint: 'https://confrage-webhooktest.free.beeceptor.com/mockapi', // httpsプロトコルのエンドポイント ReturnSubscriptionArn: true // true or false }; |
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#subscribe-property
const aws = require('aws-sdk'); | |
const sns = new aws.SNS({ | |
apiVersion: '2010-03-31', | |
region: 'ap-northeast-1' | |
}); | |
exports.handler = async (event) => { | |
const params = { | |
Protocol: 'https', | |
TopicArn: 'arn:aws:sns:ap-northeast-1:xxxxxxxxxxxxx:topicname', | |
Attributes: { | |
'FilterPolicy': '{"notifytype":["LIFE"]}' | |
}, | |
Endpoint: 'https://confrage-webhooktest.free.beeceptor.com/mockapi', | |
ReturnSubscriptionArn: true | |
}; | |
const ret = await sns.subscribe(params).promise() // サブスクリプション登録 | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify(ret), | |
}; | |
return response; | |
}; |
httpsプロトコルなので、サブスクリプションは「保留中の確認」ステータスになります。
このタイミングでエンドポイントURLに送られるリクエストのヘッダ情報に’x-amz-sns-message-type’:’SubscriptionConfirmation’という情報が渡ってくるので、このヘッダ情報の場合に、リクエストボディ.SubscribeURLをクリックします。
エンドポイントがAPI Gateway(public)+Lambda(node.js)の場合は、sns.confirmSubscriptionメソッドに以下パラメータを渡せば「保留中の確認」→「確認済み」にすることが出来ます。
1 2 3 4 |
const params = { 'TopicArn':TopicArn, 'Token':Token } |
この2つはリクエストボディに含まれています。
以下、エンドポイントがLambdaの場合プログラムで「確認済み」にする例です。
const aws = require('aws-sdk'); | |
const sns = new aws.SNS({ | |
apiVersion: '2010-03-31', | |
region: 'ap-northeast-1' | |
}); | |
exports.handler = async (event) => { | |
if(event['headers']['x-amz-sns-message-type'] === 'SubscriptionConfirmation') { | |
const body = JSON.parse(event.body) | |
const ret = await sns.confirmSubscription({ | |
'TopicArn':body.TopicArn, | |
'Token':body.Token | |
}).promise() | |
} | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify('Hello from Lambda!'), | |
}; | |
return response; | |
}; |
SNSサブスクリプション削除
node.jsでSNSサブスクリプションを削除するには、unsubscribeメソッドを使用します。
メソッドに渡す引数は以下です。
1 2 3 |
const params = { 'SubscriptionArn':サブスクリプションのARN } |
https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/SNS.html#unsubscribe-property
削除するソース例です。
const aws = require('aws-sdk'); | |
const sns = new aws.SNS({ | |
apiVersion: '2010-03-31', | |
region: 'ap-northeast-1' | |
}) | |
exports.handler = async (event) => { | |
const ret = await sns.unsubscribe({ | |
'SubscriptionArn': 'サブスクリプションのARN' | |
}).promise() | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify(ret), | |
}; | |
return response; | |
}; |
SNSサブスクリプションのサブスクリプションフィルターポリシー設定
「確認済み」ステータスのサブスクリプションに対してサブスクリプションフィルターポリシーを設定します。setSubscriptionAttributesメソッドでフィルターポリシーを設定することが出来ます。
メソッドに渡す引数は以下です。
1 2 3 4 5 |
const params = { AttributeName: 'FilterPolicy', SubscriptionArn: 'サブスクリプションARN', AttributeValue: 'フィルターポリシー' } |
以下サブスクリプションフィルターポリシーを設定する例です。
const aws = require('aws-sdk'); | |
const sns = new aws.SNS({ | |
apiVersion: '2010-03-31', | |
region: 'ap-northeast-1' | |
}) | |
exports.handler = async (event) => { | |
const params = { | |
AttributeName: 'FilterPolicy', // 固定 | |
SubscriptionArn: 'サブスクリプションARN', | |
AttributeValue: '{"notifytype":["TOP","HOME"]}' // 適当なフィルターポリシー | |
} | |
await sns.setSubscriptionAttributes(params).promise() | |
const response = { | |
statusCode: 200, | |
body: JSON.stringify('Hello from Lambda!'), | |
}; | |
return response; | |
}; |
JavaでSNS登録削除を実装する場合は「JavaでSNSサブスクリプションを登録削除する方法」を参照ください。
KHI入社して退社。今はCONFRAGEで正社員です。関西で140-170/80~120万から受け付けております^^
得意技はJS(ES6),Java,AWSの大体のリソースです
コメントはやさしくお願いいたします^^
座右の銘は、「狭き門より入れ」「願わくは、我に七難八苦を与えたまえ」です^^