Skip to content

Add tutorial to create a resource type in Radius #1409

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 85 commits into
base: edge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
35b542e
Add udt tutorial
Reshrahim Mar 28, 2025
959073a
Updates
Reshrahim Mar 28, 2025
e65c08a
Updates
Reshrahim Mar 31, 2025
f38cc7d
Fix rad file
Reshrahim Mar 31, 2025
fcbb511
Add Bicep outputs
Reshrahim Mar 31, 2025
21d4b7f
Refine steps
Reshrahim Apr 1, 2025
1144242
Merge branch 'edge' into re/udt
Reshrahim Apr 1, 2025
eb99e9a
Fix bicep config
Reshrahim Apr 1, 2025
5b8832b
Merge branch 're/udt' of https://github.com/radius-project/docs into …
Reshrahim Apr 1, 2025
1f86c1b
Fix formatting
Reshrahim Apr 1, 2025
f068aa3
Fixes to docs and bicep files
Reshrahim Apr 4, 2025
e2312b3
Fix Recipes reference
Reshrahim Apr 4, 2025
e674ccc
Fix naming
Reshrahim Apr 4, 2025
90d4145
Fix errors
Reshrahim Apr 4, 2025
6612df4
Updates
Reshrahim Apr 4, 2025
2424eb8
Fix code snippet formatting
Reshrahim Apr 4, 2025
5ce2d2d
Fix code snippet formatting
Reshrahim Apr 4, 2025
9fe4076
Fix code snippet formatting
Reshrahim Apr 4, 2025
e7eaf4c
Address feedback
Reshrahim Apr 4, 2025
1ae227a
Fix refernce
Reshrahim Apr 4, 2025
bbf436a
Fix refernce
Reshrahim Apr 4, 2025
3f48129
Fix refernce
Reshrahim Apr 4, 2025
395daf3
Fix refernce
Reshrahim Apr 4, 2025
4333f91
Fix refernce to guides
Reshrahim Apr 4, 2025
975b59e
Address feddback
Reshrahim Apr 4, 2025
b4e40ec
Fix todoapp image
Reshrahim Apr 4, 2025
f97622a
Fix snippets
Reshrahim Apr 4, 2025
9bb65e5
Updates
Reshrahim Apr 4, 2025
25aa751
Update docs/content/tutorials/tutorial-resource-type/index.md
Reshrahim Apr 7, 2025
e63dc84
Update docs/content/tutorials/tutorial-resource-type/index.md
Reshrahim Apr 7, 2025
10e935e
Update docs/content/tutorials/tutorial-resource-type/index.md
Reshrahim Apr 7, 2025
7bf98cb
Address feedback
Reshrahim Apr 7, 2025
dfb81b6
Merge branch 're/udt' of https://github.com/radius-project/docs into …
Reshrahim Apr 7, 2025
ff22421
Address feedback
Reshrahim Apr 7, 2025
411530b
Address feedback
Reshrahim Apr 7, 2025
3952365
fix image
Reshrahim Apr 7, 2025
73f9f83
fix image
Reshrahim Apr 7, 2025
597388b
Apply suggestions from code review
Reshrahim Apr 8, 2025
5d160ca
Apply suggestions from code review
Reshrahim Apr 8, 2025
01beeb2
Apply suggestions from code review
Reshrahim Apr 9, 2025
02eba8c
Address PR review feedback
Reshrahim Apr 9, 2025
07b72cc
Fix bicep validation error
Reshrahim Apr 9, 2025
c93d797
Merge branch 'edge' into re/udt
Reshrahim Apr 9, 2025
da0f460
Update docs/content/tutorials/tutorial-resource-type/snippets/app.bicep
Reshrahim Apr 9, 2025
b97ee0d
Add caution on secrets
Reshrahim Apr 10, 2025
989cf94
Updates
Reshrahim Apr 10, 2025
ec75bf5
Updates
Reshrahim Apr 10, 2025
8ed29b3
Updates
Reshrahim Apr 10, 2025
9784700
Add local registry
Reshrahim Apr 11, 2025
8fbe694
fix tabs
Reshrahim Apr 11, 2025
00b72c6
Nits
Reshrahim Apr 11, 2025
34b9820
Trigger Build
Reshrahim Apr 11, 2025
6a309de
Fixes
Reshrahim Apr 14, 2025
3bfa9ee
Fixes
Reshrahim Apr 14, 2025
a3b68a3
Fixes
Reshrahim Apr 14, 2025
2699f36
Fixes
Reshrahim Apr 14, 2025
a35a37b
Add terraform recipes
Reshrahim Apr 22, 2025
fca31af
Spell check
Reshrahim Apr 22, 2025
3bf8b15
Fix pre-reqs
Reshrahim Apr 22, 2025
78813cf
Fix pre-reqs
Reshrahim Apr 22, 2025
7d1543e
Fix order
Reshrahim Apr 22, 2025
3c7e7e5
Fix env variables
Reshrahim Apr 23, 2025
427f2c1
Fix env variables
Reshrahim Apr 23, 2025
a5b140d
Add app
Reshrahim Apr 23, 2025
603f930
Update tf Recipe
Reshrahim Apr 23, 2025
8d62a89
Fix typo
Reshrahim Apr 24, 2025
64ddd92
Fix typo
Reshrahim Apr 24, 2025
e71d1de
Add tf example
Reshrahim Apr 24, 2025
9c56a7e
Update docs/content/tutorials/tutorial-resource-type/index.md
Reshrahim Apr 24, 2025
ed3045e
Add tf example
Reshrahim Apr 24, 2025
a4cbc99
tf module sources
Reshrahim Apr 24, 2025
9e5d3ff
update tf
Reshrahim Apr 24, 2025
6ca5293
Update index.md
jonvsm Apr 25, 2025
39b7434
Add application flag
Reshrahim Apr 25, 2025
6e529cc
Merge branch 're/udt' of https://github.com/radius-project/docs into …
Reshrahim Apr 25, 2025
d24985b
Add customizable property size to the definitions
Reshrahim May 6, 2025
55d7638
Merge branch 'edge' into re/udt
Reshrahim May 6, 2025
0af86b3
Add recipe outputs changes
Reshrahim May 7, 2025
ceea7a1
Merge branch 're/udt' of https://github.com/radius-project/docs into …
Reshrahim May 7, 2025
4c8530a
Add warning
Reshrahim May 8, 2025
1c57ed8
Add app graph
Reshrahim May 8, 2025
42f46b8
rename namespace and app defn
Reshrahim May 12, 2025
00024be
Rename to types.yaml
Reshrahim May 12, 2025
27e88c3
Reove mycompany refernce
Reshrahim May 12, 2025
506eebe
Merge branch 'edge' into re/udt
Reshrahim May 21, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions .github/config/en-custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1301,12 +1301,13 @@ DeploymentTemplate
gitops
auditable
bicepparam
cleartext
MyCompany
backendRequest
GatewayRouteTimeoutPolicy
timeoutPolicy
ACI
aci
managedIdentity
gatewayID
Ngroup
dns
Ngroup
11 changes: 11 additions & 0 deletions docs/content/tutorials/tutorial-resource-type/bicepconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// The bicepconfig.json file is needed so the bicep files for this tutorial can be compiled with the correct setup
{
"experimentalFeaturesEnabled": {
"extensibility": true
},
"extensions": {
"radius": "br:biceptypes.azurecr.io/radius:latest",
"aws": "br:biceptypes.azurecr.io/aws:latest",
"radiusResources": "./radiusResources.tgz"
}
}
240 changes: 240 additions & 0 deletions docs/content/tutorials/tutorial-resource-type/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
---
type: docs
title: "Tutorial: Create a Resource Type in Radius"
linkTitle: "Create resource type"
description: "Learn how to define and deploy a resource type in your Radius application"
weight: 200
categories: ["resource-types"]
---

## Overview

Radius includes several built-in resource types which developers can use to build applications. These include core resource types such as containers, gateways, and secrets. You can also create your own resource types. This tutorial guides you through creating a PostgreSQL resource and deploying the sample Todo List application with PostgreSQL.

{{< image src="todolist.png" alt="Diagram of the Todo List with PostgreSQL" width=600px >}}

## Prerequisites

- [A Kubernetes cluster]({{< ref "/guides/operations/kubernetes/overview" >}}) to host Radius and the Todo List application. Make sure to follow the instructions under the [Supported Kubernetes clusters]({{< ref "/guides/operations/kubernetes/overview#supported-kubernetes-clusters" >}}).
- [rad CLI]({{< ref "installation#step-1-install-the-rad-cli" >}})
- Store your Recipe at a location.
- If you are using Terraform, Radius supports pulling Terraform modules from the following [module sources](https://developer.hashicorp.com/terraform/language/modules/sources): A generic Git repository including GitHub and Bitbucket and Terraform Registry
- If you are using Bicep, you must store your Recipe at an [OCI compliant container registry](https://oras.land/docs/compatible_oci_registries/). Make sure the container registry is set up with appropriate permissions to publish and pull Recipes.
- The [Bicep extension]({{< ref "installation#step-2-install-the-vs-code-extension" >}}) for VS Code is recommended for Bicep language support
- [Node.js](https://nodejs.org/en/download) is required to generate the Bicep extension to deploy the new resource type.

## Step 1: Install Radius and initialize a new environment

1. Begin in a new directory for your application:

```bash
mkdir todolist
cd todolist
```

1. Initialize a new Radius environment:

*Select 'No' when prompted to setup application in the current directory?*

```bash
rad init
```

## Step 2: Create a PostgreSQL resource type in Radius

To create a PostgreSQL resource type in Radius, first create the resource type definition then add the resource type to Radius.

1. Create a new file called `types.yaml` and add the following:

{{% rad file="snippets/types.yaml" lang=YAML embed=true %}}

The PostgreSQL resource type definition includes:

- `name`: The namespace of the resource type used to group resource types; must be in the form PrimaryName.SecondaryName
- `types`: The resource type name
- `apiVersions`: The version of the schema defined below; currently must be `2023-10-01-preview`
- `schema`: The schema defines the properties of the resource type.
- `environment`: The Radius environment in which the resource type is deployed; this property is set by Radius when the resource is deployed
- `application`: The application to which the resource belongs to
- `size`: The size of the PostgreSQL resource type.
- `host`: The host name of the PostgreSQL resource type; this is a read only property set by Recipe
- `port`: The port of the PostgreSQL resource type; this is a read only property set by Recipe
- `username`: The username of the PostgreSQL resource type; this is a read only property set by Recipe
- `password`: The password of the PostgreSQL resource type; this is a read only property set by Recipe
- `capabilities`: This specifies features of the resource type. The only available option is `SupportsRecipes` which indicates that the resource type can be deployed via a Recipe.
{{% alert title="Warning" color="warning" %}} `environment`, `application`, `status` are system reserved resource properties updated by Radius after deployment.
{{% /alert %}}

1. Create the resource type using the [rad resource-type]({{< ref rad_resource-type_create >}}) command:

```bash
rad resource-type create postgreSQL -f types.yaml
```

## Step 3: Register a Recipe for the PostgreSQL resource type

[Recipes]({{< ref "/guides/recipes/overview" >}}) define how resource types are deployed. To deploy the PostgreSQL resource type, you must create a Bicep Template or a Terraform module and publish it to a location. Then register the template or module as a Recipe in the Radius environment.

{{< tabs Terraform Bicep >}}{{% codetab %}}

1. Radius supports pulling Terraform modules from a generic [Git repository](https://developer.hashicorp.com/terraform/language/modules/sources#generic-git-repository), including GitHub. Create a new directory in your Git repository for the PostgreSQL Terraform module and navigate to it:

Create a new file called `main.tf` and add the following:

{{% rad file="snippets/recipes/terraform/main.tf" embed=true %}}

Learn more about Authoring Terraform Modules as Recipes in this [how-to-guide]({{< ref "/guides/recipes/howto-author-recipes" >}}). If you want to pull Terraform modules from a private registry, follow the how-to-guide on [pulling Terraform modules from a private registry](https://docs.radapp.io/guides/recipes/terraform/howto-private-registry/)

1. Register the Terraform module as the `default` Recipe in the `default` environment (the default environment was created when `rad init` was run)

```bash
rad recipe register default --environment default --resource-type Radius.Resources/postgreSQL --template-kind terraform --template-path git::<path to your tf module>
```
For example, if you have the terraform module in your git repository named `terraform-recipes/kubernetes/postgres`, the command would look like this:

```bash
rad recipe register default --environment default --resource-type Radius.Resources/postgreSQL --template-kind terraform --template-path git::https://github.com/<org-name>/terraform-recipes.git//kubernetes/postgres
```

1. Verify the Recipe is registered to the `default` environment

```bash
rad recipe list
```
You should see the Recipe for the PostgreSQL resource type listed in the output (along with several Bicep recipes that were installed by the rad init command).

```bash
RECIPE TYPE TEMPLATE KIND TEMPLATE VERSION TEMPLATE
default Radius.Resources/postgreSQL terraform git::<path to your tf module>
...
```
{{% /codetab %}}
{{% codetab %}}
1. Create a new file called `postgreSQL.bicep` and add the following:

{{% rad file="snippets/recipes/bicep/postgreSQL.bicep" embed=true %}}

1. Make sure your preferred OCI-compliant container is set up with appropriate permissions to publish and pull Recipes. For example, if you are using GitHub container registry, follow the instructions [here](https://docs.github.com/en/packages/working-with-a-github-packages-registry/working-with-the-container-registry). The easiest option to authenticate is to generate a Personal Access token (PAT) with read, write and delete access to the package. Follow this [how-to-guide]({{< ref "/guides/recipes/howto-private-bicep-registry" >}}) if you want to publish to a private registry.

1. Publish the Recipe to the container registry using the below command. Make sure to replace `host` and `repository` with your container registry.

```bash
rad bicep publish --file postgreSQL.bicep --target br:<host>/<repository>/postgresql:latest
```
1. Register the Bicep template as the `default` Recipe in the `default` environment (the default environment was created when `rad init` was run)

```bash
rad recipe register default --environment default --resource-type Radius.Resources/postgreSQL --template-kind bicep --template-path <host>/<repository>/postgresql:latest
```
1. Verify the Recipe is registered to the `default` environment

```bash
rad recipe list
```
You should see the Recipe for the PostgreSQL resource type listed in the output.

```bash
RECIPE TYPE TEMPLATE KIND TEMPLATE VERSION TEMPLATE
default Radius.Resources/postgreSQL bicep <host>/<repository>/postgresql:latest
...
```
{{% /codetab %}}
{{< /tabs >}}

## Step 4: Generate a Bicep extension

{{% alert title="Info" color="info" %}}
This step is required even if you use Terraform Recipes to deploy the PostgreSQL resource type as part of the application.
{{% /alert %}}

For the rad CLI and VS Code to recognize the PostgreSQL resource type, a [Bicep extension](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/bicep-extension) must be generated and added to the [`bicepconfig.json`]({{< ref "/guides/tooling/bicepconfig/overview" >}}) file.

1. Generate the Bicep extension using the [rad bicep publish-extension]({{< ref rad_bicep_publish-extension >}}) command:

```bash
rad bicep publish-extension -f types.yaml --target ./radiusResources.tgz
```
The Bicep extension `radiusResources` is generated and saved to the `radiusResources.tgz` file.

1. Open the `bicepconfig.json` file and replace it with the below config.

```bash
{
"experimentalFeaturesEnabled": {
"extensibility": true
},
"extensions": {
"radius": "br:biceptypes.azurecr.io/radius:latest",
"aws": "br:biceptypes.azurecr.io/aws:latest",
"radiusResources": "./radiusResources.tgz"
}
}
```

## Step 5: Author the Todo List application with PostgreSQL

1. Create `todolist.bicep` and add the Todo List application.

```bicep
extension radius
```
{{% rad file="snippets/todolist.bicep" embed=true marker="//APP" %}}

1. Add the `radiusResources` extension and the PostgreSQL resource type

```bicep
extension radiusResources
```
{{% rad file="snippets/todolist.bicep" embed=true marker="//POSTGRESQL" %}}

1. Add the `demo` container definition along with the connection to the PostgreSQL resource type and environment variables.

{{% rad file="snippets/todolist.bicep" embed=true marker="//CONTAINER" %}}

{{% alert title="Caution" color="warning" %}}
In this example the POSTGRESQL_PASSWORD is stored as a cleartext property for demo purposes. In production environments, always use secrets to store and reference sensitive information like passwords.
{{% /alert %}}

1. Your final `todolist.bicep` file should look like this:

{{% rad file="snippets/todolist.bicep" embed=true %}}

## Step 5: Run the application

Run the application using `rad run`. The `rad run` command sets up port forwarding to the application. .

```sh
rad run todolist.bicep --application todolist
```
Visit the application at [http://localhost:3000](http://localhost:3000). You should see the Radius Connections section with new environment variables added. The `demo` container now has connection information for PostgreSQL (`CONNECTION_POSTGRESQL_HOST`, `CONNECTION_POSTGRESQL_PORT`, etc.)

{{< image src=todolist_postgresql.png" alt="Todo List with PostgreSQL connection" width=800px >}}

Navigate to the Radius Dashboard at [http://localhost:7007](http://localhost:7007/resources/default/Applications.Core/applications/todolist/application), You should see a visualization of the application graph for your `todolist` app, including the connection from the `demo` container to the `postgreSQL` db

{{< image src="todolist_dashboard.png" alt="screenshot of Radius Dashboard with the new resource type" width=800 >}}

## Step 6: Clean up

To clean up the resources created in this tutorial, run the following commands

1. Delete the application and all resources created by the application

```bash
rad app delete --application todolist
```
2. Delete the environment

```bash
rad env delete --environment default
```
3. Delete the PostgreSQL resource type

```bash
rad resource-type delete Radius.Resources/postgreSQL
```
4. Uninstall Radius

```bash
rad uninstall kubernetes
```
Binary file not shown.
Loading
Loading