From f1b79094e13491520ed59c7a4ffee511adfd3863 Mon Sep 17 00:00:00 2001 From: Dan Rios <36534747+riosengineer@users.noreply.github.com> Date: Sun, 10 Aug 2025 20:09:10 +0100 Subject: [PATCH 1/4] Add README and example files for Azure Bicep Resource Derived Types --- .../resource-derived-types/README.md | 53 +++++++++++++++++++ .../resource-derived-types/main.bicep | 18 +++++++ .../resource-derived-types/main.bicepparam | 4 ++ 3 files changed, 75 insertions(+) create mode 100644 bicep-examples/resource-derived-types/README.md create mode 100644 bicep-examples/resource-derived-types/main.bicep create mode 100644 bicep-examples/resource-derived-types/main.bicepparam diff --git a/bicep-examples/resource-derived-types/README.md b/bicep-examples/resource-derived-types/README.md new file mode 100644 index 0000000..ce8dc7a --- /dev/null +++ b/bicep-examples/resource-derived-types/README.md @@ -0,0 +1,53 @@ +# Azure Bicep - Resource Derived Types + +## Introduction + +> [!TIP] +> Check this example out on the Bicep Users Group on LinkedIn to see a code snippet example here. + +Resource-derived types in Azure Bicep allow you to define the shape of an object or parameter based on the schema of an existing Azure resource. This enables you to ensure your parameters or variables match the exact structure expected by Azure resources, improving type safety and maintainability. + +This can be useful when you ***don't*** need a custom type enforced and instead can rely on the resource built-in type instead. This will reduce code making sure you only use a user defined type for custom data structures. + +## 📃 Benefits of Resource Derived Types + +✅ Type Safety: Resource-derived types ensure your objects match the schema of Azure resources, reducing misconfiguration risk. + +✅ Maintainability: If the resource schema changes, your Bicep code can automatically stay in sync by referencing the resource type. + +✅ Intellisense: VS Code Bicep IntelliSense provides completions and validation based on the derived type, making authoring easier. + +## Resource Derived Type Example + +In this example, you’ll use resource-derived types with the `resourceInput<>` helper to define parameters that match the allowed values for a Storage Accounts `SKU` and `kind`. + +```bicep +targetScope = 'resourceGroup' + +// Parameters +@description('The SKU type for the storage account using the resource providers derived type.') +param storageSkuType resourceInput<'Microsoft.Storage/storageAccounts@2024-01-01'>.sku.name + +@description('The kind of the storage account using the resource providers derived type.') +param storageKindType resourceInput<'Microsoft.Storage/storageAccounts@2024-01-01'>.kind + +// Resources +resource storageAccount 'Microsoft.Storage/storageAccounts@2024-01-01' = { + name: 'st${uniqueString(resourceGroup().id)}' + location: resourceGroup().location + sku: { + name: storageSkuType + } + kind: storageKindType +} +``` + +By using `resourceInput<>`, the parameters `storageSkuType` and `storageKindType` are always validated against the allowed values for the specified Storage Account API version. This provides strong type safety. If you go to the `main.bicepparam` file in VS Code (with the Bicep extension) you will see you get autocompletions for the types taken directly from the resource provider. Very cool! + +## 🚀 Test + +> [!NOTE] +> This example does not require a deployment. Rather showing that the intellisense now will autocomplete based on the derived type resource input. + +- Go to the `main.bicepparam` file +- Select SKU param and press `cntrl+enter` to bring up the Bicep extensions autocomplete list. This list is being pulled from the resourceInput by the resource provider property. diff --git a/bicep-examples/resource-derived-types/main.bicep b/bicep-examples/resource-derived-types/main.bicep new file mode 100644 index 0000000..08eab53 --- /dev/null +++ b/bicep-examples/resource-derived-types/main.bicep @@ -0,0 +1,18 @@ +targetScope = 'resourceGroup' + +// Parameters +@description('The SKU type for the storage account using the resource providers derived type.') +param storageSkuType resourceInput<'Microsoft.Storage/storageAccounts@2024-01-01'>.sku.name + +@description('The kind of the storage account using the resource providers derived type.') +param storageKindType resourceInput<'Microsoft.Storage/storageAccounts@2024-01-01'>.kind + +// Resources +resource storageAccount 'Microsoft.Storage/storageAccounts@2024-01-01' = { + name: 'st${uniqueString(resourceGroup().id)}' + location: resourceGroup().location + sku: { + name: storageSkuType + } + kind: storageKindType +} diff --git a/bicep-examples/resource-derived-types/main.bicepparam b/bicep-examples/resource-derived-types/main.bicepparam new file mode 100644 index 0000000..6d11014 --- /dev/null +++ b/bicep-examples/resource-derived-types/main.bicepparam @@ -0,0 +1,4 @@ +using 'main.bicep' + +param storageKindType = 'StorageV2' +param storageSkuType = 'Standard_LRS' From 327815bd25285bf46f7d910c82f2fe7c2e27204a Mon Sep 17 00:00:00 2001 From: Dan Rios <36534747+riosengineer@users.noreply.github.com> Date: Tue, 12 Aug 2025 12:27:10 +0100 Subject: [PATCH 2/4] Update README.md with LinkedIn Bicep Did You Know --- bicep-examples/resource-derived-types/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bicep-examples/resource-derived-types/README.md b/bicep-examples/resource-derived-types/README.md index ce8dc7a..aa10741 100644 --- a/bicep-examples/resource-derived-types/README.md +++ b/bicep-examples/resource-derived-types/README.md @@ -3,7 +3,7 @@ ## Introduction > [!TIP] -> Check this example out on the Bicep Users Group on LinkedIn to see a code snippet example here. +> Check this example out on the Bicep Users Group on LinkedIn to see a code snippet example [here](https://www.linkedin.com/feed/update/urn:li:activity:7360935609323958272?utm_source=share&utm_medium=member_ios&rcm=ACoAABQc0g0BE6cF8NVeQPgDN4PQqnuxftB0rTE) Resource-derived types in Azure Bicep allow you to define the shape of an object or parameter based on the schema of an existing Azure resource. This enables you to ensure your parameters or variables match the exact structure expected by Azure resources, improving type safety and maintainability. From 38cf80ad31bcecfba9a5540e6f3cae5812a156da Mon Sep 17 00:00:00 2001 From: Dan Rios <36534747+riosengineer@users.noreply.github.com> Date: Tue, 12 Aug 2025 15:09:19 +0100 Subject: [PATCH 3/4] Fix formatting of LinkedIn example link in README.md --- bicep-examples/resource-derived-types/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bicep-examples/resource-derived-types/README.md b/bicep-examples/resource-derived-types/README.md index aa10741..dee69e9 100644 --- a/bicep-examples/resource-derived-types/README.md +++ b/bicep-examples/resource-derived-types/README.md @@ -3,7 +3,7 @@ ## Introduction > [!TIP] -> Check this example out on the Bicep Users Group on LinkedIn to see a code snippet example [here](https://www.linkedin.com/feed/update/urn:li:activity:7360935609323958272?utm_source=share&utm_medium=member_ios&rcm=ACoAABQc0g0BE6cF8NVeQPgDN4PQqnuxftB0rTE) +> Check this example out on the [Bicep Users Group on LinkedIn to see a code snippet example](https://www.linkedin.com/feed/update/urn:li:activity:7360935609323958272?utm_source=share&utm_medium=member_ios&rcm=ACoAABQc0g0BE6cF8NVeQPgDN4PQqnuxftB0rTE) Resource-derived types in Azure Bicep allow you to define the shape of an object or parameter based on the schema of an existing Azure resource. This enables you to ensure your parameters or variables match the exact structure expected by Azure resources, improving type safety and maintainability. From 1756b09ce19f119be7b6873fb20eeee582257bdd Mon Sep 17 00:00:00 2001 From: Dan Rios <36534747+riosengineer@users.noreply.github.com> Date: Tue, 12 Aug 2025 15:14:54 +0100 Subject: [PATCH 4/4] Add REPOSITORY_TRIVY to disabled linters in MegaLinter configuration --- .github/workflows/mega-linter.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/mega-linter.yml b/.github/workflows/mega-linter.yml index 8b8080f..bafe362 100644 --- a/.github/workflows/mega-linter.yml +++ b/.github/workflows/mega-linter.yml @@ -49,7 +49,7 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # ADD YOUR CUSTOM ENV VARIABLES HERE OR DEFINE THEM IN A FILE .mega-linter.yml AT THE ROOT OF YOUR REPOSITORY DISABLE: COPYPASTE,SPELL # Uncomment to disable copy-paste and spell checks - DISABLE_LINTERS: YAML_V8R,YAML_YAMLLINT,YAML_PRETTIER,REPOSITORY_CHECKOV,POWERSHELL_POWERSHELL,ACTION_ACTIONLINT,REPOSITORY_GITLEAKS,REPOSITORY_GRYPE,REPOSITORY_KICS + DISABLE_LINTERS: YAML_V8R,YAML_YAMLLINT,YAML_PRETTIER,REPOSITORY_CHECKOV,POWERSHELL_POWERSHELL,ACTION_ACTIONLINT,REPOSITORY_GITLEAKS,REPOSITORY_GRYPE,REPOSITORY_KICS,REPOSITORY_TRIVY # Upload MegaLinter artifacts - name: Archive production artifacts