3. Create Recipes

Create Bicep or Terraform Recipes that implement the Resource Type

Recipes define how a resource is deployed. In part three of this tutorial, you will create a Terraform or Bicep Recipe for the PostgreSQL Resource Type.

Create a Recipe for the PostgreSQL Resource Type

Recipes can be either Terraform configurations or Bicep templates. Select the tab for the IaC language you prefer.


Terraform configuration are stored in a Git repository for Radius to access the Recipe. For this tutorial, the Terraform Recipe has already been created and stored in the Radius resource-types-contrib repository. Hence, there are no actual steps to complete for this section of the tutorial. What follows is a walkthrough of the Radius-specific aspects of the Terraform configuration.

  1. Radius metadata via context variable

    Radius automatically injects a variable called context. The context variable contains the Resource Type’s and the Environment’s properties. To use this metadata within the Terraform configuration, a variable must be defined within the main.tf or variables.tf file.

      variable "context" {  
        description = "Radius-provided object containing information about the resource calling the Recipe."  
        type        = any  
      }  
    

    Refer to the context schema for the available properties.

  2. Variables for Recipe customization

    The memory variable allows customizing the memory request for the PostgreSQL container based on the size property defined in the Resource Type.

    variable "memory" {
      description = "Memory limits for the PostgreSQL container"
      type = map(object({
        memoryRequest = string
      }))
      default = {
        S = {
          memoryRequest = "512Mi"
        },
        M = {
          memoryRequest = "1Gi"
        }
        L = {
          memoryRequest = "2Gi"
        }
    
      }
    

    It is then used in the Kubernetes Deployment:

     resources {
        requests = {
          memory = var.memory[var.context.resource.properties.size].memoryRequest
        }
      }
    
  3. Result output

    Radius requires Terraform configuration to have a result output defined which provides Radius with the values for each read-only property on the Resource Type. Since the postgreSqlDatabases Resource Type has five read-only properties, each are specified within the result.

      output "result" {
        value = {
          values = {
            host = "${kubernetes_service.postgres.metadata[0].name}.${kubernetes_service.postgres.metadata[0].namespace}.svc.cluster.local"
            port = local.port
            database = "postgres_db"
            username = "postgres"
            password = random_password.password.result
          }       
        }
      }
    

Bicep templates are stored in an OCI registry for Radius to access the Recipe. For this tutorial, the Bicep Recipe has already been created and stored in the Radius OCI registry. Hence, there are no actual steps to complete for this section of the tutorial. What follows is a walkthrough of the Radius-specific aspects of the Bicep template.

  1. Radius metadata via context variable

    Radius automatically injects a context parameter. The context parameter contains the Resource Type’s and the Environment’s properties. To use this metadata within the Bicep template, a context parameter must be defined.

      @description('Information about what resource is calling this Recipe. Generated by Radius.')
      param context object
    

    Refer to the context schema for the available properties.

  2. Variables for Recipe customization

    The memory variable allows customizing the memory request for the PostgreSQL container based on the size property defined in the Resource Type.

    @description('Memory limits for the PostgreSQL container')
    var memory ={
      S: {
        memoryRequest: '512Mi'
      } 
      M: {
        memoryRequest: '1Gi'
      }
      L: {
        memoryRequest: '2Gi'
      }
    } 
    

    It is then used in the Kubernetes Deployment:

    resources: {
      requests: {
        memory: memory[context.resource.properties.size].memoryRequest
      }
    }
    
  3. Result output

    Radius requires Bicep templates to have a result output defined which provides Radius with the values for each read-only property on the Resource Type. Since the postgreSqlDatabases Resource Type has five read-only properties, each are specified within the result.

    output result object = {
      values: {
        host: '${svc.metadata.name}.${svc.metadata.namespace}.svc.cluster.local'
        port: port
        database: database
        username: user
        password: password
      } 
    }
    

In part four, you will create an Environment which uses the new Recipe.

Next step: Create Environment