How-To: Author portable resources

Learn how to author portable resources in Radius

This guide will teach you how to author a portable resource for your Radius Application.

Prerequisites

Before you get started, you’ll need to make sure you have the following tools and resources:

Step 1: Add a portable resource

Portable resources provide abstraction and portability to Radius Applications. Radius currently offers options to manually provision the resources or automatically provision them via Recipes:


Recipes handle infrastructure provisioning for you. You can use a Recipe to provision a Redis cache, using your environment’s default Recipe.

Create a file named app.bicep and paste the following:

import radius as radius

@description('Specifies the environment for resources.')
param environment string

@description('Specifies the application for resources.')
param application string

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

To learn more about Recipes visit the Recipes overview page.


You can also manually manage your infrastructure and use a portable resource to abstract the details. Create a file named app.bicep and paste the following:

import radius as radius

@description('Specifies the environment for resources.')
param environment string

@description('Specifies the application for resources.')
param application string

resource redis 'Applications.Datastores/redisCaches@2023-10-01-preview' = {
  name: 'myredis'
  properties: {
    environment: environment
    application: application
    resourceProvisioning: 'manual'
    username: 'myusername'
    host: 'mycache.contoso.com'
    port: 8080
    secrets: {
      password: '******'
    }
  }
}

Step 2: Add a container

In your Bicep file app.bicep, add a container resource that will connect to the Redis cache. Note the connection to the Redis cache automatically injects connection-related environment variables into the container. Optionally, you can also manually access properties and set environment variables based on your portable resource values.

resource container 'Applications.Core/containers@2023-10-01-preview' = {
  name: 'demo'
  properties: {
    application: application
    container: {
      image: 'ghcr.io/radius-project/samples/demo:latest'
      env: {
        // Manually access Redis connection information
        REDIS_CONNECTION: redis.connectionString()
      }
      ports: {
        web: {
          containerPort: 3000
        }
      }
    }
    connections: {
      // Automatically inject connection details
      redis: {
        source: redis.id
      }
    }
  }
}

Step 3: Deploy the 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:

Cleanup

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

rad app delete -a demo

Further reading