How-To: Connect to dependencies

Learn how to connect to dependencies in your application via connections

This how-to guide will teach how to connect to your dependencies via connections

Prerequisites

Step 1: View the container definition

Open the app.bicep and view the container:

import radius as rad

@description('The app ID of your Radius application. Set automatically by the rad CLI.')
param application string

resource demo 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'demo'
  properties: {
    application: application
    container: {
      image: 'ghcr.io/radius-project/samples/demo:latest'
      ports: {
        web: {
          containerPort: 3000
        }
      }
    }
  }
}

Step 2: Add a Redis cache as a dependency

Next, add to app.bicep a Redis cache, leveraging the default “local-dev” Recipe:

@description('The env ID of your Radius Environment. Set automatically by the rad CLI.')
param environment string

resource db 'Applications.Datastores/redisCaches@2023-10-01-preview' = {
  name: 'db'
  properties: {
    application: application
    environment: environment
  }
}

Step 3: Connect to the Redis cache

Connections from a container to a resource result in environment variables for connection information automatically being set on the container. Update your container definition to add a connection to the new Redis cache:

 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
resource demo 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'demo'
  properties: {
    application: application
    container: {
      image: 'ghcr.io/radius-project/samples/demo:latest'
      ports: {
        web: {
          containerPort: 3000
        }
      }
    }
    connections: {
      redis: {
        source: db.id
      }
    }
  }
}

Step 4: Deploy your app

  1. Run your application in your environment:

    rad run ./app.bicep -a demo
    
  2. Visit localhost:3000 in your browser. You should see the following page, now showing injected environment variables:

Step 5: View the Application Connections

Radius Connections are more than just environment variables and configuration. You can also access the “application graph” and understand the connections within your application with the following command:

rad app connections -a demo

You should see the following output, detailing the connections between the demo container and the db Redis cache, along with information about the underlying Kubernetes resources running the app:

Displaying application: demo

Name: demo (Applications.Core/containers)
Connections:
  demo -> db (Applications.Datastores/redisCaches)
Resources:
  demo (kubernetes: apps/Deployment)
  demo (kubernetes: core/Secret)
  demo (kubernetes: core/Service)
  demo (kubernetes: core/ServiceAccount)
  demo (kubernetes: rbac.authorization.k8s.io/Role)
  demo (kubernetes: rbac.authorization.k8s.io/RoleBinding)

Name: db (Applications.Datastores/redisCaches)
Connections:
  demo (Applications.Core/containers) -> db
Resources:
  redis-r5tcrra3d7uh6 (kubernetes: apps/Deployment)
  redis-r5tcrra3d7uh6 (kubernetes: core/Service)

Cleanup

Run rad app delete to cleanup your Radius application, container, and Redis cache:

rad app delete -a demo

Further reading