3. Create Recipes
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.
-
Radius metadata via
contextvariableRadius 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 themain.tforvariables.tffile.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.
-
Variables for Recipe customization
The
memoryvariable allows customizing the memory request for the PostgreSQL container based on thesizeproperty 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 } } -
Result output
Radius requires Terraform configuration to have a
resultoutput 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 theresult.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.
-
Radius metadata via
contextvariableRadius automatically injects a
contextparameter. The context parameter contains the Resource Type’s and the Environment’s properties. To use this metadata within the Bicep template, acontextparameter must be defined.@description('Information about what resource is calling this Recipe. Generated by Radius.') param context objectRefer to the context schema for the available properties.
-
Variables for Recipe customization
The
memoryvariable allows customizing the memory request for the PostgreSQL container based on thesizeproperty 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 } } -
Result output
Radius requires Bicep templates to have a
resultoutput 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 theresult.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
Feedback
Was this page helpful?
Glad to hear it! Please feel free to star our repo and join our Discord server to stay up to date with the project.
Sorry to hear that. If you would like to also contribute a suggestion visit and tell us how we can improve.