EventBridgeとは違うらしいので調べてみた。つーか違いがよくわかってなかった。
S3イベント通知とは
S3バケットで特定のイベントが発生したときに通知を受け取ることができる。
docs.aws.amazon.com
通知可能なS3イベント
・New object created events:オブジェクト作成
・Object removal events:オブジェクト削除
・Restore object events:オブジェクト復元
・Reduced Redundancy Storage(RRS) object lost events:オブジェクト損失
・Replication events:レプリケーション
・S3 Lifecycle expiration events:S3ライフサイクル有効期限
・S3 Lifecycle transition events:S3ライフサイクル移行
・S3 Intelligent-Tiering automatic archival events:自動アーカイブ
・Object tagging events:オブジェクトタグ付け
・Object ACL PUT events:オブジェクトACL PUT
通知可能な宛先
・Amazon SNS
・Amazon SQS
・AWS Lambda
・Amazon EventBridge
EventBridgeとの使い分け
S3のイベントを検知する場合、S3イベント通知は簡易な対応で設定可能。しかし通知内容が限られており、また通知処理をほかのサービスと一括して管理したい場合などは設定箇所が分かれてしますため管理がしずらくなるためその場合はEventBridgeを使用したほうがよい。
今回のやること
Terraformで”3バケットとSNSトピックを作成し、S3にファイルをアップロードしてS3イベント通知を発生させる。具体的には下記を実施する。
・SNSトピック作成
・SNSサブスクリプション作成
・S3イベント通知作成
・SNSアクセスポリシー作成
・動作確認
・後始末
実践!
1.tfファイル作成
1-1.tfファイル作成
provider "aws" { region = "ap-northeast-1" profile = "testvault" } resource "aws_sns_topic" "s3_notifications" { name = "s3-notifications" } resource "aws_sns_topic_subscription" "email_subscription" { topic_arn = aws_sns_topic.s3_notifications.arn protocol = "email" endpoint = "xxxxxxx@xxxxx.xxx" } resource "aws_s3_bucket" "my_bucket" { bucket = "test-s3-event-information-sns-123456789" } resource "aws_s3_bucket_notification" "bucket_notification" { bucket = aws_s3_bucket.my_bucket.id topic { topic_arn = aws_sns_topic.s3_notifications.arn events = ["s3:ObjectCreated:*"] filter_suffix = ".log" } } resource "aws_sns_topic_policy" "default" { arn = aws_sns_topic.s3_notifications.arn policy = jsonencode({ Version = "2012-10-17" Id = "default" Statement = [{ Sid = "AllowPublishFromS3" Effect = "Allow" Principal = { Service = "s3.amazonaws.com" } Action = "sns:Publish" Resource = aws_sns_topic.s3_notifications.arn Condition = { ArnLike = { "aws:SourceArn" = aws_s3_bucket.my_bucket.arn } } }] }) }
1-2.適用
# terraform plan # terraform apply
1-3.メール承諾
設定したメールアドレス宛にメールが届いているため、メール文の[Confirm subscription]をクリック
2.動作確認
2-1.作成したバケットにファイルをアップロード
※ファイル名は*.log形式のもを使う
※中身が空のファイルではアップロード失敗するのでファイル内に適当な文字を入れておくこと
2-2.メールが届いたことを確認
3.後始末
3-1.S3バケット内を空にする
3-2.下記を実行
# terraform destroy
感想
EventBridgeを使わないでも通知を出せるんですね。でもあっちこっちに設定があっても面倒なのでEventBridgeでいいのでは(´Д`)