Getting started with EventRouter
Note
EventRouter is at the Preview stage. To gain access, submit a request
In this tutorial, you will use an EventRouter bus to forward a message from a Message Queue to a workflow in Workflows.
If it matches the filter defined in the rule inside the bus, a message entering the queue will be forwarded to the workflow. The workflow will start automatically. Before being forwarded, the message will be converted according to the template defined in the same rule as the filter.
Getting started
To get started in Yandex Cloud:
- Log in to the management console
. If not signed up yet, navigate to the management console and follow the on-screen instructions. - On the Billing
page, make sure you have a billing account linked and its status isACTIVE
orTRIAL_ACTIVE
. If you do not have a billing account yet, create one. - If you do not have a folder yet, create one.
Create a service account
- In the management console
, select the appropriate folder. - From the list of services, select Identity and Access Management.
- Click Create service account.
- Enter a name for the service account:
sa-for-eventrouter
. - Click
Add role and select theserverless.workflows.executor
, ymq.reader, and ymq.writer roles. - Click Create.
Create a queue
- In the management console
, select Message Queue. - Click Create queue.
- Enter a name for the queue:
sample-queue
. - Select the
Standard
type. Do not change other settings. - Click Create.
- Open the queue you created.
- In the Overview tab, under General information, copy the queue URL as you will need it later.
Create a workflow
Note
Workflows is at the Preview stage. To gain access, submit a request
- In the management console
, select Serverless Integrations from the list of services. - In the left-hand panel, select
Workflows. - In the top-right corner, click Create workflow.
- In the YaML specification field, add the specification as follows:
yawl: "0.1" start: noopstep steps: noopstep: noOp: output: |- \(.)
- Expand the Additional parameters section.
- In the Name field, enter the workflow name:
sample-workflow
. - In the Service account field, select the
sa-for-eventrouter
service account. - Click Create.
Create a bus
- In the management console
, select Serverless Integrations from the list of services. - In the left-hand panel, select
EventRouter. - In the top-right corner, click Create bus.
- Enter the bus name:
sample-bus
. - Click Create.
Create a connector
- Select the
sample-bus
bus. - Navigate to the Connectors tab.
- In the top-right corner, click Create connector.
- In the Source field, select
Yandex Message Queue
. - Specify
sample-queue
in the Message queue field. - In the Service account field, specify
sa-for-eventrouter
. - Click Create.
Create a rule
- Select the
sample-bus
bus. - Navigate to the Rules tab.
- In the top-right corner, click Create rule.
- Expand the Filter section and specify the following jq expression to filter messages:
.httpMethod == "GET" and (.headers.Host | test("^d5dm"))
- Under Targets, click Add.
- In the Type field, select
Yandex Workflows
. - In the Workflow field, specify
sample-workflow
. - In the Service account field, specify
sa-for-eventrouter
. - Expand the Transformer section and specify the jq expression to convert messages:
{ "message": "API gateway host is \(.headers.Host)." }
- Click Create.
Send a message to the queue
Make sure the messages matching the created rule are converted and forwarded to the workflow.
-
Install
the AWS CLI. -
Create static access keys for the
sa-for-eventrouter
service account. Save the ID and secret key to a secure location. You will not be able to view the secret key parameters again after you close the window. -
Configure the AWS CLI:
-
Launch the interactive profile setup:
aws configure
-
Specify the service account key ID you obtained earlier:
AWS Access Key ID [****************ver_]: <service_account_key_ID>
-
Specify the service account secret key you obtained earlier:
AWS Secret Access Key [****************w5lb]: <service_account_secret_key>
-
Specify the
ru-central1
default region name:Default region name [ru-central1]: ru-central1
-
Specify
json
as the default format for output data:Default output format [None]: json
-
To view the current profile settings, run this command:
aws configure list
Result:
Name Value Type Location ---- ----- ---- -------- profile <not set> None None access_key ****************aBc1 shared-credentials-file secret_key ****************DeF2 shared-credentials-file region ru-central1 config-file ~/.aws/config
-
-
Send the first message to
sample-queue
using the previously saved queue URL:aws sqs send-message \ --message-body '{"httpMethod":"GET","headers":{"Host":"h6ds1lb3s0df********.k7******.apigw.yandexcloud.net"}}' \ --endpoint https://message-queue.api.cloud.yandex.net/ \ --queue-url <queue_URL>
Result:
{ "MD5OfMessageBody": "c37859ba35a9958be041467c********", "MessageId": "f845****-9260****-7527****-f4b0****", "SequenceNumber": "0" }
-
Make sure the filter you specified in the rule stopped the message from going through the bus:
- In the management console
, select Serverless Integrations from the list of services. - In the left-hand panel, select
Workflows. - Navigate to the Executions tab. You should not see any completed runs on the page.
- In the management console
-
Send the second message to
sample-queue
using the previously saved queue URL:aws sqs send-message \ --message-body '{"httpMethod":"GET","headers":{"Host":"d5dm1lba80md********.i9******.apigw.yandexcloud.net"}}' \ --endpoint https://message-queue.api.cloud.yandex.net/ \ --queue-url <queue_URL>
Result:
{ "MD5OfMessageBody": "c37859ba35a9958be041467c********", "MessageId": "f845****-9260****-7527****-f4b0****", "SequenceNumber": "0" }
-
Make sure the filter you specified in the rule allowed the message to go through the bus and then the message was converted according to the template and forwarded to the workflow:
- In the management console
, select Serverless Integrations from the list of services. - In the left-hand panel, select
Workflows. - Navigate to the Executions tab. You should now see a completed run on the page.
- Select the completed run.
- Make sure the Input data section displays the converted message:
{ "message": "API gateway host is d5dm1lba80md********.i9******.apigw.yandexcloud.net." }
- In the management console