Overview: Dapr building blocks

Easily leverage Dapr building blocks in your application for code and infrastructure portability

Radius offers first-class support for the Dapr runtime and building blocks to make it easy to make your code fully portable across code and infrastructure. Simply drop in your Dapr building blocks as resources and Radius will automatically configure and apply the accompanying Dapr configuration.

Installation

Follow the Dapr installation instructions to install Dapr in your Kubernetes cluster. Once installed, you can begin adding Dapr sidecars and building blocks.

Setup Dapr

Sidecar

A Dapr sidecar allows your services to interact with Dapr building blocks. It is required if your service leverages Dapr.


You can easily add the Dapr sidecar to your Containers using a Dapr sidecar extension:


resource container 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'mycontainer'
  properties: {
    application: app.id
    container: {
      image: 'myimage'
    }
    extensions: [
      {
        kind: 'daprSidecar'
        appId: 'mycontainer'
        appPort: 3500
      }
    ]
  }
}

Your container can now interact with the sidecar using the Dapr building block APIs or the Dapr SDKs.

Building blocks

Dapr resources make it easy to model and configure Dapr building block APIs. Simply specify the building block and the backing resource, and Radius will apply the accompanying Dapr component configuration.


Model your building blocks as resources:


resource account 'Microsoft.Storage/storageAccounts@2019-06-01' existing = {
  name: 'myaccount'

  resource tableServices 'tableServices' existing = {
    name: 'default'

    resource table 'tables' existing = {
      name: 'mytable'
    }
  }
}

// The accompanying Dapr component resource is automatically created for you
resource stateStore 'Applications.Dapr/stateStores@2023-10-01-preview' = {
  name: 'mystatestore'
  properties: {
    environment: environment
    application: app.id
    resourceProvisioning: 'manual'
    resources: [
      { id: account.id }
      { id: account::tableServices::table.id }
    ]
    metadata: {
      accountName: {
        value: account.name
      }
      accountKey: {
        value: account.listKeys().keys[0].value
      }
      tableName: {
        value: account::tableServices::table.name
      }
    }
    type: 'state.azure.tablestorage'
    version: 'v1'
  }
}

Component naming

To interact with a Dapr building block, you need to know the name of the Dapr component. This name is the same as the name of the building block resource.

For example, if you have a Applications.Dapr/stateStores resource named mystatestore the Dapr component name will be mystatestore. Your code will then interact with this component via http://localhost:3500/v1.0/state/mystatestore, or via the Dapr SDKs through the mystatestore component name.

Connecting to Dapr building blocks

You can connect to a Dapr building block by manually referencing the resource name or by adding a connection. Connections automatically inject environment variables into your container with the resource name prefixed.

resource container 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'mycontainer'
  properties: {
    application: app.id
    container: {
      image: 'myimage'
      env: {
        // Option 1: Manually set component name as an environment variable
        DAPR_COMPONENTNAME: {
          value: statestore.name
        }
      }
    }
    connections: {
      // Option 2 (preferred): Automatically set environment variable with component name
      c1: {
        source: statestore.id // Results in CONNECTION_C1_COMPONENTNAME
      }
    }
  }
}

resource statestore 'Applications.Dapr/stateStores@2023-10-01-preview' = {...}

Service invocation

Dapr service invocation allows your services to discover and call each other.

One container in an application can invoke another using the AppId.


extension radius


resource app 'Applications.Core/applications@2023-10-01-preview' existing = {
  name: 'myapp'
}

// Backend is being invoked through service invocation
resource backend 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'backend'
  properties: {
    application: app.id
    container: {
      image: 'backend:latest'
    }
    extensions: [
      {
        kind: 'daprSidecar'
        appId: 'backend'
      }
    ]
  }
}


// Frontend invokes backend
resource frontend 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'frontend'
  properties: {
    application: app.id
    container: {
      image: 'frontend:latest'
      env: {
        // Configures the appID of the backend service.
        CONNECTION_BACKEND_APPID: {
          value: 'backend'
        }
      }
    }
    extensions: [
      {
        kind: 'daprSidecar'
        appId: 'frontend'
      }
    ]
  }
}

Resource schema

Refer to the schema reference docs for more information on how to model Dapr resources.