{EventBridge}チュートリアル: Amazon S3 オブジェクトが作成されたときに通知を送信する


S3 -> EventBridge -> SNS

※S3にイベント通知の設定追加が必要。S3から直接SNS送信も可能


-- 1. コマンド等のインストール

-- 1.1 aws cli version 2 インストール

curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
unzip awscliv2.zip
sudo ./aws/install

aws --version

-- 1.2 jqインストール
sudo yum -y install jq


-- 2. S3 バケットを作成する

aws s3 mb s3://bucket123

aws s3 ls

-- 3. イベント通知を作成

aws s3api get-bucket-notification-configuration \
--bucket bucket123

vim a.json
{
"EventBridgeConfiguration": {}
}


aws s3api put-bucket-notification-configuration \
--bucket bucket123 \
--notification-configuration file://a.json

 

-- 4. SNSトピック作成

aws sns list-topics
aws sns list-subscriptions

aws sns create-topic --name topic01

aws sns subscribe \
--topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01 \
--protocol email \
--notification-endpoint hoge@example.com

 


-- 5. ルールの作成

aws events put-rule \
--name rule01 \
--event-pattern '{
  "source": ["aws.s3"],
  "detail-type": ["Object Created"],
  "detail": {
    "bucket": {
      "name": ["bucket123"]
    }
  }
}' \
--state ENABLED \
--description rule01

aws events list-rules
aws events describe-rule --name rule01

 

-- 6. ターゲットの作成

aws events put-targets \
--rule rule01 \
--targets "Id"="1","Arn"="arn:aws:sns:ap-northeast-1:999999999999:topic01"

aws events list-targets-by-rule \
--rule rule01


-- 7. 動作確認

echo file01 > file01.txt

aws s3api put-object --bucket bucket123 --key file01.txt --body file01.txt

aws s3 ls s3://bucket123 --recursive

AWS 通知から E メールを受信するかどうかを確認


-- 8. クリーンアップ


-- ターゲットの削除
aws events list-targets-by-rule \
--rule rule01

aws events remove-targets \
--rule rule01 \
--ids 1

-- ルールの削除
aws events list-rules

aws events delete-rule \
--name rule01


-- SNSトピック削除

aws sns unsubscribe --subscription-arn arn:aws:sns:ap-northeast-1:999999999999:topic01:11111111-2222-3333-4444-555555555555
aws sns delete-topic --topic-arn arn:aws:sns:ap-northeast-1:999999999999:topic01


aws sns list-topics
aws sns list-subscriptions

-- バケットの削除

aws s3 ls
aws s3 rb s3://bucket123  --force