https://docs.aws.amazon.com/ja_jp/eventbridge/latest/userguide/eb-archive.html
イベントは、必ずしもアーカイブに追加された順序通りに再生されるわけではありません。
再生では、イベント内の時間に基づいて再生するイベントを処理し、1 分間隔で再生します。
-- 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. アーカイブの作成
aws events create-archive \
--archive-name archive01 \
--event-source-arn arn:aws:events:ap-northeast-1:999999999999:event-bus/default \
--description archive01 \
--event-pattern '{"source": ["hoge"]}' \
--retention-days 1
aws events describe-archive \
--archive-name archive01
-- 3. テストイベント作成
[
{
"Source": "hoge",
"Detail": "{ \"key1\": \"value1\", \"key2\": \"value2\" }",
"Resources": [
"resource1",
"resource2"
],
"DetailType": "myDetailType"
},
{
"Source": "fuga",
"Detail": "{ \"key1\": \"value3\", \"key2\": \"value4\" }",
"Resources": [
"resource1",
"resource2"
],
"DetailType": "myDetailType"
}
]
aws events put-events --entries file://a.json
aws events describe-archive \
--archive-name archive01
アーカイブされるまでタイムラグがある
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
aws iam create-role \
--role-name role01 \
--assume-role-policy-document file://role01.json
-- 5. ポリシーをロールにアタッチ
aws iam attach-role-policy \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole \
--role-name role01
-- 6. Lambda関数作成
vim test.py
import json
def lambda_handler(event,context):
print(json.dumps(event))
chmod 755 test.py
zip test.zip test.py
aws lambda create-function \
--function-name func01 \
--zip-file fileb://test.zip \
--handler test.lambda_handler \
--runtime python3.9 \
--role arn:aws:iam::999999999999:role/role01
aws lambda list-functions | grep func01
aws lambda get-function --function-name func01
-- 7. ルールの作成
aws events put-rule \
--name rule01 \
--event-pattern '{"source": ["hoge"]}' \
--state ENABLED \
--description rule01
aws events list-rules
aws events describe-rule --name rule01
-- 8. ターゲットの作成
aws events put-targets \
--rule rule01 \
--targets "Id"="1","Arn"="arn:aws:lambda:ap-northeast-1:999999999999:function:func01"
aws events list-targets-by-rule \
--rule rule01
-- 9. Lambda関数に権限を追加する
aws lambda add-permission \
--function-name func01 \
--statement-id events \
--action lambda:InvokeFunction \
--principal events.amazonaws.com \
--source-arn arn:aws:events:ap-northeast-1:999999999999:rule/rule01
aws lambda get-policy \
--function-name func01 | jq -r .Policy | jq .
-- 10. アーカイブの再生
aws events start-replay \
--replay-name replay01 \
--description replay01 \
--event-source-arn arn:aws:events:ap-northeast-1:999999999999:archive/archive01 \
--event-start-time "2022-03-08T06:13:00+00:00" \
--event-end-time "2022-03-08T07:09:00+00:00" \
--destination '{
"Arn":"arn:aws:events:ap-northeast-1:999999999999:event-bus/default",
"FilterArns": ["arn:aws:events:ap-northeast-1:999999999999:rule/rule01"]
}'
aws events describe-replay \
--replay-name replay01
Lambda関数が実行されたことをCloudWatch Logsで確認
-- 11. クリーンアップ
再生は 90 日間保持され、それ以降は削除されます。
-- ターゲットの削除
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
-- Lambda関数の削除
aws lambda get-function --function-name func01
aws lambda delete-function --function-name func01
-- ロールの削除
aws iam list-roles | grep role01
aws iam detach-role-policy \
--role-name role01 \
--policy-arn arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
aws iam delete-role --role-name role01
-- アーカイブの削除
aws events delete-archive \
--archive-name archive01
aws events describe-archive \
--archive-name archive01