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 75 commits into
base: edge
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
75 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
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
2 changes: 2 additions & 0 deletions .github/config/en-custom.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1301,3 +1301,5 @@ DeploymentTemplate
gitops
auditable
bicepparam
cleartext
MyCompany
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",
"mycompany": "./mycompany.tgz"
}
}
231 changes: 231 additions & 0 deletions docs/content/tutorials/tutorial-resource-type/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,231 @@
---
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 `postgreSQL.yaml` and add the following:

{{% rad file="snippets/postgreSQL.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
- `status`: This is a read-only property that is set by the Recipe that includes connection information to the resource type.
- `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.
<br></br>

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

```bash
rad resource-type create postgreSQL -f postgreSQL.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 MyCompany.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 MyCompany.Resources/postgreSQL --template-kind terraform --template-path 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 MyCompany.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 MyCompany.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 MyCompany.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 postgreSQL.yaml --target ./mycompany.tgz
```
The Bicep extension `mycompany` is generated and saved to the `mycompany.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",
"mycompany": "./mycompany.tgz"
}
}
```

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

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

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

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

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

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

{{% rad file="snippets/app.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 `app.bicep` file should look like this:

{{% rad file="snippets/app.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 app.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 >}}

## 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 MyCompany.Resources/postgreSQL
```
4. Uninstall Radius

```bash
rad uninstall kubernetes
```
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Import the set of Radius resources (Applications.*) into Bicep
extension radius
// Import the set of MyCompany resources (MyCompany.*) into Bicep
extension mycompany

//APP
resource todolist 'Applications.Core/applications@2023-10-01-preview' = {
name: 'todolist'
properties: {
environment: environment
}
}
//APP

//POSTGRESQL
param environment string
resource postgresql 'MyCompany.Resources/postgreSQL@2023-10-01-preview' = {
name: 'postgresql'
location: 'global'
properties: {
application: todolist.id
environment: environment
}
}
//POSTGRESQL

//CONTAINER
resource demo 'Applications.Core/containers@2023-10-01-preview' = {
name: 'demo'
properties: {
application: todolist.id
container: {
image: 'ghcr.io/radius-project/samples/demo:latest'
ports: {
web: {
containerPort: 3000
}
}
//CONNECTION
env: {
CONNECTION_POSTGRES_HOST: {
value: postgresql.properties.status.binding.host
}
CONNECTION_POSTGRES_PORT: {
value: string(postgresql.properties.status.binding.port)
}
CONNECTION_POSTGRES_USERNAME: {
value: postgresql.properties.status.binding.username
}
CONNECTION_POSTGRES_DATABASE: {
value: postgresql.properties.status.binding.database
}
//This is stored and passed as cleartext for demo purposes. In production, use a secret store.
CONNECTION_POSTGRES_PASSWORD: {
value: postgresql.properties.status.binding.password
}
}
}
}
}
//CONTAINER


Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: MyCompany.Resources
types:
postgreSQL:
apiVersions:
'2023-10-01-preview':
schema:
type: object
properties:
environment:
type: string
application:
type: string
status:
type: object
properties:
binding:
type: object
properties:
database:
type: string
description: The name of the database.
host:
type: string
description: The host name of the database.
port:
type: string
description: The port number of the database.
username:
type: string
description: The username for the database.
password:
type: string
description: The password for the database.
capabilities: ["SupportsRecipes"]
Loading
Loading