How-To: Use bindings to interface with external resources

Invoke external systems with Dapr output bindings

Using bindings, it is possible to invoke external resources without tying in to special SDK or libraries. For a complete sample showing output bindings, visit this link.

Watch this video on how to use bi-directional output bindings.

1. Create a binding

An output binding represents a resource that Dapr will use invoke and send messages to.

For the purpose of this guide, you’ll use a Kafka binding. You can find a list of the different binding specs here.

Create the following YAML file, named binding.yaml, and save this to a components sub-folder in your application directory. (Use the --components-path flag with dapr run to point to your custom components dir)

Note: When running in Kubernetes, apply this file to your cluster using kubectl apply -f binding.yaml

  1. apiVersion: dapr.io/v1alpha1
  2. kind: Component
  3. metadata:
  4. name: myevent
  5. namespace: default
  6. spec:
  7. type: bindings.kafka
  8. version: v1
  9. metadata:
  10. - name: brokers
  11. value: localhost:9092
  12. - name: publishTopic
  13. value: topic1

Here, create a new binding component with the name of myevent.

Inside the metadata section, configure Kafka related properties such as the topic to publish the message to and the broker.

2. Send an event

All that’s left now is to invoke the bindings endpoint on a running Dapr instance.

You can do so using HTTP:

  1. curl -X POST -H http://localhost:3500/v1.0/bindings/myevent -d '{ "data": { "message": "Hi!" }, "operation": "create" }'

As seen above, you invoked the /binding endpoint with the name of the binding to invoke, in our case its myevent. The payload goes inside the mandatory data field, and can be any JSON serializable value.

You’ll also notice that there’s an operation field that tells the binding what you need it to do. You can check here which operations are supported for every output binding.

References

Last modified February 16, 2021: Merge pull request #1235 from dapr/update-v0.11 (b4e9fbb)