📝

SNS から Lambda 関数 URL にメッセージを送信してみた

に公開

Fanout Amazon SNS notifications to HTTPS endpoints - Amazon Simple Notification Service

You can use Amazon SNS to send notification messages to one or more HTTP or HTTPS endpoints.

HTTPS エンドポイントに Lambda 関数 URL を指定してみました。

1. Lambda 関数の作成

以下の設定で作成しました。

  • ランタイム: Python 3.13
  • 関数 URL: 認証タイプ NONE
  • コード: 以下の通り
import json

def lambda_handler(event, context):
    # TODO implement
    print(json.dumps(event))
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

2. SNS トピックの作成

デフォルト設定で SNS トピックを作成します。
トピックのサブスクリプションは以下の設定で作成します。

  • プロトコル: HTTPS
  • エンドポイント: 手順 1 で作成した Lambda 関数の URL

サブスクリプションを作成すると以下のような確認メッセージが Lambda に送信されます。

{
    "version": "2.0",
    "routeKey": "$default",
    "rawPath": "/",
    "rawQueryString": "",
    "headers": {
        "content-length": "1524",
        "x-amzn-tls-version": "TLSv1.3",
        "x-amz-sns-topic-arn": "arn:aws:sns:ap-northeast-1:012345678901:test",
        "x-forwarded-proto": "https",
        "x-amz-sns-message-type": "SubscriptionConfirmation",
        "x-forwarded-port": "443",
        "x-forwarded-for": "136.18.20.63",
        "x-amz-sns-message-id": "7845ac13-439c-41d2-95db-d5dc63ac3ac8",
        "x-amzn-tls-cipher-suite": "TLS_AES_128_GCM_SHA256",
        "x-amzn-trace-id": "Root=1-68e5ed33-520a00911e6084286e5e37a5",
        "host": "memxkxe2zvt4lbkzeypppdragi0anmbr.lambda-url.ap-northeast-1.on.aws",
        "content-type": "text/plain; charset=UTF-8",
        "accept-encoding": "gzip,deflate",
        "user-agent": "Amazon Simple Notification Service Agent"
    },
    "requestContext": {
        "accountId": "anonymous",
        "apiId": "memxkxe2zvt4lbkzeypppdragi0anmbr",
        "domainName": "memxkxe2zvt4lbkzeypppdragi0anmbr.lambda-url.ap-northeast-1.on.aws",
        "domainPrefix": "memxkxe2zvt4lbkzeypppdragi0anmbr",
        "http": {
            "method": "POST",
            "path": "/",
            "protocol": "HTTP/1.1",
            "sourceIp": "136.18.20.63",
            "userAgent": "Amazon Simple Notification Service Agent"
        },
        "requestId": "412f1cec-a63c-4024-93c1-cc298f62d5a5",
        "routeKey": "$default",
        "stage": "$default",
        "time": "08/Oct/2025:04:48:51 +0000",
        "timeEpoch": 1759898931427
    },
    "body": "{\n  \"Type\" : \"SubscriptionConfirmation\",\n  \"MessageId\" : \"7845ac13-439c-41d2-95db-d5dc63ac3ac8\",\n  \"Token\" : \"xxx\",\n  \"TopicArn\" : \"arn:aws:sns:ap-northeast-1:012345678901:test\",\n  \"Message\" : \"You have chosen to subscribe to the topic arn:aws:sns:ap-northeast-1:012345678901:test.\\nTo confirm the subscription, visit the SubscribeURL included in this message.\",\n  \"SubscribeURL\" : \"https://sns.ap-northeast-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:ap-northeast-1:012345678901:test&Token=xxx\",\n  \"Timestamp\" : \"2025-10-08T04:48:51.359Z\",\n  \"SignatureVersion\" : \"1\",\n  \"Signature\" : \"xxx\",\n  \"SigningCertURL\" : \"https://sns.ap-northeast-1.amazonaws.com/SimpleNotificationService-6209c161c6221fdf56ec1eb5c821d112.pem\"\n}",
    "isBase64Encoded": false
}

3. サブスクリプションの確認

手順 2 で出力されたメッセージの中から SubscribeURL の値を取得します。

"SubscribeURL" : "https://sns.ap-northeast-1.amazonaws.com/?Action=ConfirmSubscription&TopicArn=arn:aws:sns:ap-northeast-1:012345678901:test&Token=xxx

上記 URL を SNS コンソールのサブスクリプションの確認に入力します。

サブスクリプションのステータスが確認済みになれば OK です。

4. 動作確認

SNS コンソールからテストメッセージを送信してみます。

Lambda の実行ログにメッセージが出力されていれば正常に動作しています。

{
    "version": "2.0",
    "routeKey": "$default",
    "rawPath": "/",
    "rawQueryString": "",
    "headers": {
        "content-length": "947",
        "x-amzn-tls-version": "TLSv1.3",
        "x-amz-sns-topic-arn": "arn:aws:sns:ap-northeast-1:012345678901:test",
        "x-forwarded-proto": "https",
        "x-amz-sns-message-type": "Notification",
        "x-forwarded-port": "443",
        "x-forwarded-for": "136.18.20.63",
        "x-amz-sns-message-id": "96758077-06dd-52ac-8e15-e128f67bfe90",
        "x-amzn-tls-cipher-suite": "TLS_AES_128_GCM_SHA256",
        "x-amzn-trace-id": "Root=1-68e5ee66-219a9332162e10e312a85085",
        "host": "memxkxe2zvt4lbkzeypppdragi0anmbr.lambda-url.ap-northeast-1.on.aws",
        "x-amz-sns-subscription-arn": "arn:aws:sns:ap-northeast-1:172454386805:test:afde9a2a-a822-4f91-87b5-0895da4ef13b",
        "content-type": "text/plain; charset=UTF-8",
        "accept-encoding": "gzip,deflate",
        "user-agent": "Amazon Simple Notification Service Agent"
    },
    "requestContext": {
        "accountId": "anonymous",
        "apiId": "memxkxe2zvt4lbkzeypppdragi0anmbr",
        "domainName": "memxkxe2zvt4lbkzeypppdragi0anmbr.lambda-url.ap-northeast-1.on.aws",
        "domainPrefix": "memxkxe2zvt4lbkzeypppdragi0anmbr",
        "http": {
            "method": "POST",
            "path": "/",
            "protocol": "HTTP/1.1",
            "sourceIp": "136.18.20.63",
            "userAgent": "Amazon Simple Notification Service Agent"
        },
        "requestId": "d1773b95-868c-47f7-baa6-437af20ea864",
        "routeKey": "$default",
        "stage": "$default",
        "time": "08/Oct/2025:04:53:58 +0000",
        "timeEpoch": 1759899238565
    },
    "body": "{\n  \"Type\" : \"Notification\",\n  \"MessageId\" : \"96758077-06dd-52ac-8e15-e128f67bfe90\",\n  \"TopicArn\" : \"arn:aws:sns:ap-northeast-1:012345678901:test\",\n  \"Subject\" : \"test\",\n  \"Message\" : \"test message\",\n  \"Timestamp\" : \"2025-10-08T04:53:58.500Z\",\n  \"SignatureVersion\" : \"1\",\n  \"Signature\" : \"xxx\",\n  \"SigningCertURL\" : \"https://sns.ap-northeast-1.amazonaws.com/SimpleNotificationService-6209c161c6221fdf56ec1eb5c821d112.pem\",\n  \"UnsubscribeURL\" : \"https://sns.ap-northeast-1.amazonaws.com/?Action=Unsubscribe&SubscriptionArn=arn:aws:sns:ap-northeast-1:012345678901:test:afde9a2a-a822-4f91-87b5-0895da4ef13b\"\n}",
    "isBase64Encoded": false
}

まとめ

今回は SNS から Lambda 関数 URL にメッセージを送信してみました。
どなたかの参考になれば幸いです。

参考資料

Discussion