0Pricing
Serverless Backend with AWS Lambda & API Gateway · レッスン

Pub/Subメッセージングに使うSNS

Amazon SNS(Simple Notification Service)を使い、Pub/Subモデルで複数の購読者にメッセージを発行する方法を学びます。

「Pub/Subメッセージングに使うSNS」はCoddyKit上の無料Serverless Backend with AWS Lambda & API Gatewayレッスンです。 これはレッスン2/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはServerless Backend with AWS Lambda & API Gateway学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

Pub/Sub Messaging Explained

Imagine you want to send information to many different recipients, but you don't want to know who they are or how many there are. That's where the Publish/Subscribe (Pub/Sub) messaging pattern comes in!

It's like a newspaper: the publisher creates content, and anyone interested can subscribe to receive it, without the publisher knowing each individual reader.

Meet Amazon SNS

Amazon Simple Notification Service (SNS) is a fully managed Pub/Sub messaging service provided by AWS.

SNS makes it easy to send messages to a large number of subscribers simultaneously, enabling you to build highly scalable and decoupled applications.

SNS Topics: The Central Hub

The core component of SNS is a Topic. Think of an SNS Topic as a communication channel or a category for messages.

  • Publishers send messages to a specific Topic.
  • Subscribers register their interest in receiving messages from that Topic.
  • SNS handles the delivery of messages from the Topic to all its subscribers.

Subscribers & Endpoints

A Subscriber is an endpoint that receives messages from an SNS Topic. SNS supports various types of endpoints, allowing great flexibility:

  • AWS Lambda functions: For processing events with serverless code.
  • Amazon SQS queues: For reliable message storage and processing.
  • HTTP/S endpoints: For sending notifications to web servers.
  • Email/SMS: For direct human notifications.

How SNS Works: The Flow

Here's a simplified flow of how SNS delivers messages:

  1. A Publisher sends a message to an SNS Topic.
  2. The SNS Topic receives the message.
  3. SNS filters the message (if configured) and delivers it to all registered Subscribers.
  4. Each Subscriber's Endpoint receives the message.

This decouples the publisher from the subscribers, improving system flexibility.

Common Use Cases for SNS

SNS is ideal for scenarios requiring 'fan-out' messaging, where one event triggers multiple actions. Common uses include:

  • Event notifications: Alerting multiple systems or users about changes.
  • Application integration: Decoupling microservices that need to react to shared events.
  • Data processing pipelines: Notifying different stages of a pipeline when new data arrives.
  • Monitoring & Alerts: Sending alerts to ops teams based on metrics.

Creating an SNS Topic

To start using SNS, you first need to create a Topic. This can be done through the AWS Management Console, AWS CLI, or Infrastructure as Code tools like AWS CloudFormation or SAM.

When creating a topic, you'll give it a name and specify its type (Standard or FIFO). For most general-purpose use cases, a Standard Topic is sufficient.

Publishing with Python

You can publish messages to an SNS Topic using the AWS SDKs. Here's a simple Python example using boto3 to publish a message. Remember, you'd need valid AWS credentials and a real Topic ARN for it to work.

import boto3

# This is a placeholder for an SNS Topic ARN.
# In a real application, replace it with your Topic's ARN.
TOPIC_ARN = "arn:aws:sns:us-east-1:123456789012:MyExampleTopic"

def main():
    print("Initializing SNS client...")
    sns_client = boto3.client('sns')

    message_to_send = "Hello CoddyKit! This is an SNS test message."

    try:
        print(f"Attempting to publish: '{message_to_send}'")
        response = sns_client.publish(
            TopicArn=TOPIC_ARN,
            Message=message_to_send
        )
        print(f"Published! Message ID: {response['MessageId']}")
    except Exception as e:
        print(f"Error publishing: {e}")
        print("Ensure AWS credentials & Topic ARN are configured.")

if __name__ == "__main__":
    main()

Benefits of Using SNS

SNS offers several advantages for building robust, scalable applications:

  • Decoupling: Publishers and subscribers don't need to know about each other.
  • Fan-out: One message can be sent to many different types of endpoints.
  • Reliability: SNS ensures message delivery to all subscribed endpoints.
  • Scalability: Handles high throughput and a large number of subscribers automatically.

SNS Quick Check

Which of the following are core concepts or components within Amazon SNS?

Recap: SNS, Your Event Hub

Congratulations! You've learned about Amazon SNS, AWS's powerful Pub/Sub messaging service.

  • SNS uses Topics as communication channels.
  • Publishers send messages to Topics.
  • Subscribers register to receive messages from Topics via various Endpoints (Lambda, SQS, HTTP/S, Email, SMS).
  • SNS enables decoupling and fan-out, making your architectures more scalable and resilient.

Next, we'll see how Lambda functions can be triggered by SQS queues and SNS topics to build powerful event-driven workflows!

よくある質問

「Pub/Subメッセージングに使うSNS」レッスンは無料ですか?

はい。「Pub/Subメッセージングに使うSNS」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Serverless Backend with AWS Lambda & API Gatewayコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Serverless Backend with AWS Lambda & API Gatewayコースには全4レッスンが含まれています。

「Pub/Subメッセージングに使うSNS」で何を学びますか?

Amazon SNS(Simple Notification Service)を使い、Pub/Subモデルで複数の購読者にメッセージを発行する方法を学びます。 ブラウザで直接実行するハンズオンコードでServerless Backend with AWS Lambda & API Gatewayを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Serverless Backend with AWS Lambda & API Gatewayを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのServerless Backend with AWS Lambda & API Gatewayは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン2/4です。

「Pub/Subメッセージングに使うSNS」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このServerless Backend with AWS Lambda & API Gatewayレッスンでコードを書いて実行できますか?

はい。すべてのServerless Backend with AWS Lambda & API Gatewayレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. サービスの疎結合化に使うSQS
  2. Pub/Subメッセージングに使うSNS
  3. SQS/SNSトリガーでLambdaを実行
  4. デッドレターキューと障害処理
← Serverless Backend with AWS Lambda & API Gatewayに戻る