Bu örnekte, AWS EventBridge cron kuralını kullanarak bir HTTP uç noktası oluşturacağız ve ona her dakika POST istekleri göndereceğiz. İstekte parametreler, sorgu dizeleri, başlıklar ve gövde olabilir. Çakışan anahtarlar olması durumunda, Connection değerleri Target değerlerine göre önceliklidir.


Rule


$ aws --profile localstack --endpoint-url http://localhost:4566 events put-rule \
--name users-api-cron-rule \
--schedule-expression "cron(* * * * *)"

{
"RuleArn": "arn:aws:events:eu-west-1:000000000000:rule/users-api-cron-rule"
}

$ aws --profile localstack --endpoint-url http://localhost:4566 events list-rules

{
"Rules": [
{
"Name": "users-api-cron-rule",
"Arn": "arn:aws:events:eu-west-1:000000000000:rule/users-api-cron-rule",
"State": "ENABLED",
"ScheduleExpression": "cron(* * * * *)",
"EventBusName": "default"
}
]
}

Connection


$ aws --profile localstack --endpoint-url http://localhost:4566 events create-connection \
--name users-api-connection \
--authorization-type BASIC \
--auth-parameters "{\"BasicAuthParameters\":{\"Username\":\"hello\",\"Password\":\"world\"},\"InvocationHttpParameters\":{\"HeaderParameters\":[{\"Key\":\"hdr-c\",\"Value\":\"val-c\",\"IsValueSecret\":false},{\"Key\":\"hdr-x\",\"Value\":\"val-c\",\"IsValueSecret\":false}],\"QueryStringParameters\":[{\"Key\":\"qry-c\",\"Value\":\"val-c\",\"IsValueSecret\":false},{\"Key\":\"qry-x\",\"Value\":\"val-c\",\"IsValueSecret\":false}],\"BodyParameters\":[{\"Key\":\"bdy-c\",\"Value\":\"val-c\",\"IsValueSecret\":false}]}}"

{
"ConnectionArn": "arn:aws:events:eu-west-1:000000000000:connection/users-api-connection/314c648e-0905-4e3b-9731-5144e6c99616",
"ConnectionState": "AUTHORIZED",
"CreationTime": "2022-03-12T12:12:16.384584+00:00",
"LastModifiedTime": "2022-03-12T12:12:16.384584+00:00"
}

Destination


$ aws --profile localstack --endpoint-url http://localhost:4566 events create-api-destination \
--name users-api-destination \
--connection-arn "arn:aws:events:eu-west-1:000000000000:connection/users-api-connection/314c648e-0905-4e3b-9731-5144e6c99616" \
--http-method POST \
--invocation-endpoint "https://webhook.site/357c5571-fb00-4f48-bddc-9b03417a044e/api/v1/users"

{
"ApiDestinationArn": "arn:aws:events:eu-west-1:000000000000:api-destination/users-api-destination/448f2e65-222a-4dbf-8602-a5219dc9529b",
"ApiDestinationState": "ACTIVE",
"CreationTime": "2022-03-12T12:12:47.789677+00:00",
"LastModifiedTime": "2022-03-12T12:12:47.789677+00:00"
}

Target


$ aws --profile localstack --endpoint-url http://localhost:4566 events put-targets \
--rule users-api-cron-rule \
--targets '[{"Id":"1","Arn":"arn:aws:events:eu-west-1:000000000000:api-destination/users-api-destination/448f2e65-222a-4dbf-8602-a5219dc9529b","Input":"{\"bdy-t\":\"val-t\"}","HttpParameters":{"PathParameterValues":["par-t"],"HeaderParameters":{"hdr-t":"val-t","hdr-x":"val-t"},"QueryStringParameters":{"qry-t":"val-t","qry-x":"val-t"}}}]'

Kütükler



localstack | 2022-03-12T12:20:50.104:DEBUG:localstack.services.events.events_listener: Notifying 1 targets in response to triggered Events rule users-api-cron-rule
localstack | 2022-03-12T12:20:50.123:DEBUG:localstack.utils.aws.message_forwarding: Calling EventBridge API destination (state "ACTIVE"): POST https://webhook.site/357c5571-fb00-4f48-bddc-9b03417a044e/api/v1/users

İstek dökümü



curl \
-X 'POST' 'https://webhook.site/5c2b623f-a9d5-439a-833c-6ab632e57d9d/api/v1/users/par-t?qry-c=val-c&qry-x=val-c&qry-t=val-t' \
-H 'hdr-t: val-t' \
-H 'hdr-x: val-c' \
-H 'hdr-c: val-c' \
-H 'authorization: Basic aGVsbG86d29ybGQ=' \
-H 'content-type: application/json; charset=utf-8' \
-H 'php-auth-user: hello' \
-H 'php-auth-pw: world' \
-d $'{"bdy-t": "val-t", "bdy-c": "val-c"}'