From 4467261f0387af6bace29f451011147e75229d18 Mon Sep 17 00:00:00 2001 From: Jason Ingram <13699472+ExchMaster@users.noreply.github.com> Date: Tue, 30 Nov 2021 13:15:20 -0600 Subject: [PATCH] Azure Container Registry Example (#543) --- .../examples/containerRegistry/README.md | 63 +++++++++++++++++++ .../containerRegistry/contRegistry.bicep | 31 +++++++++ .../modules/containerRegistry.bicep | 28 +++++++++ 3 files changed, 122 insertions(+) create mode 100644 src/bicep/examples/containerRegistry/README.md create mode 100644 src/bicep/examples/containerRegistry/contRegistry.bicep create mode 100644 src/bicep/examples/containerRegistry/modules/containerRegistry.bicep diff --git a/src/bicep/examples/containerRegistry/README.md b/src/bicep/examples/containerRegistry/README.md new file mode 100644 index 000000000..976574e57 --- /dev/null +++ b/src/bicep/examples/containerRegistry/README.md @@ -0,0 +1,63 @@ +# Azure Container Registry Example + +This example deploys a premium Azure Container Registry suitable for hosting docker containers. The registry will be deployed to the MLZ shared services resource group using default naming unless alternative values are provided at run time. + +Read on to understand what this example does, and when you're ready, collect all of the pre-requisites, then deploy the example. + +## What this example does + +### Deploys an Azure Container Registry + +The docs on Azure Container Registry: . This sample shows how to deploy using Bicep and utilizes the shared file variable pattern to support the deployment. By default, this template will deploy resources into standard default MLZ subscriptions and resource groups. + +The subscription and resource group can be changed by providing the resource group name (Param: targetResourceGroup) and ensuring that the Azure context is set the proper subscription. + +## Pre-requisites + +1. A Mission LZ deployment (a deployment of mlz.bicep) +2. The outputs from a deployment of mlz.bicep (./src/bicep/examples/deploymentVariables.json). + +See below for information on how to create the appropriate deployment variables file for use with this template. + +### Template Parameters + +Template Parameters Name | Description +-----------------------| ----------- +contRegistryName | The name of the Container Registry. If not specified, the name will default to the MLZ default naming pattern. +targetResourceGroup | The name of the resource group where the Container Registry will be deployed. If not specified, the resource group name will default to the shared services MLZ resource group name and subscription. + +### Generate MLZ VAriable File (deploymentVariables.json) + +For instructions on generating 'deploymentVariables.json' using both Azure PowerShell and Azure CLI, please see the [README at the root of the examples folder](../README.md). + +Place the resulting 'deploymentVariables.json' file within the ./src/bicep/examples folder. + +### Deploying an Container Registry + +Connect to the appropriate Azure Environment and set appropriate context, see getting started with Azure PowerShell or Azure CLI for help if needed. The commands below assume you are deploying in Azure Commercial and show the entire process from deploying MLZ and then adding an Azure Container Registry post-deployment. + +```PowerShell +cd .\src\bicep +Connect-AzAccount +New-AzSubscriptionDeployment -Name contoso -TemplateFile .\mlz.bicep -resourcePrefix 'contoso' -Location 'eastus' +cd .\examples +(Get-AzSubscriptionDeployment -Name contoso).outputs | ConvertTo-Json | Out-File -FilePath .\deploymentVariables.json +cd .\containerRegistry +New-AzSubscriptionDeployment -DeploymentName deployContainerRegistry -TemplateFile .\contRegistry.bicep -Location 'eastus' +``` + +```Azure CLI +az login +cd src/bicep +az deployment sub create -n contoso -f mlz.bicep -l eastus --parameters resourcePrefix=contoso +cd examples +az deployment sub show -n contoso --query properties.outputs > ./deploymentVariables.json +cd containerRegistry +az deployment sub create -n deployContainerRegistry -f contRegistry.bicep -l eastus +``` + +### References + +* [Introduction to private Docker container registries in Azure](https://docs.microsoft.com/en-us/azure/app-service/overview-hosting-plans) +* [Bicep Shared Variable File Pattern](https://docs.microsoft.com/en-us/azure/azure-resource-manager/bicep/patterns-shared-variable-file) +* [Azure Container Registry service tiers(Sku's)](https://docs.microsoft.com/en-us/azure/container-registry/container-registry-skus) diff --git a/src/bicep/examples/containerRegistry/contRegistry.bicep b/src/bicep/examples/containerRegistry/contRegistry.bicep new file mode 100644 index 000000000..66d63ebdf --- /dev/null +++ b/src/bicep/examples/containerRegistry/contRegistry.bicep @@ -0,0 +1,31 @@ +/* +Deployes a premium Azure Container Registry suitable for hosting docker containers. +*/ +targetScope = 'subscription' + +param mlzDeploymentVariables object = json(loadTextContent('../deploymentVariables.json')) + +@description('The name of the container registry which will be created. Must be globaly unique. No hyphens allowed, must be alpha numeric only, and between 5-50 characters. If unchanged or not specified, the MLZ resource prefix + "acr" will be utilized.') +param contRegistryName string = replace('${mlzDeploymentVariables.mlzResourcePrefix.Value}${deployment().location}acr','-','') + +@description('The name of the resource group in which the container registry will be deployed. If unchanged or not specified, the MLZ shared services resource group is used.') +param targetResourceGroup string = '${mlzDeploymentVariables.spokes.Value[2].resourceGroupName}' + +var targetSubscriptionId_Var = targetResourceGroup == '${mlzDeploymentVariables.spokes.Value[2].resourceGroupName}' ? '${mlzDeploymentVariables.spokes.Value[2].subscriptionId}' : subscription().subscriptionId +var location = deployment().location + +resource targetACRResourceGroup 'Microsoft.Resources/resourceGroups@2020-10-01' = { + name: targetResourceGroup + location: location +} + +module containerRegistry 'modules/containerRegistry.bicep' = { + scope: resourceGroup(targetSubscriptionId_Var, targetACRResourceGroup.name) + name: contRegistryName + params: { + registryName: contRegistryName + } +} + +output azureContainerRegistryName string = contRegistryName +output azureContainerRegistryResourceGroup string = targetACRResourceGroup.name diff --git a/src/bicep/examples/containerRegistry/modules/containerRegistry.bicep b/src/bicep/examples/containerRegistry/modules/containerRegistry.bicep new file mode 100644 index 000000000..b0fc1eff0 --- /dev/null +++ b/src/bicep/examples/containerRegistry/modules/containerRegistry.bicep @@ -0,0 +1,28 @@ +@minLength(5) +@maxLength(50) +param registryName string +param location string = resourceGroup().location +param registrySku string = 'premium' +param publicNetworkAccess string = 'enabled' + +resource registryName_resource 'Microsoft.ContainerRegistry/registries@2020-11-01-preview' = { + name: registryName + location: location + sku: { + name: registrySku + } + properties: { + publicNetworkAccess: publicNetworkAccess + adminUserEnabled: true + policies: { + trustPolicy: { + type: 'Notary' + status: 'enabled' + } + retentionPolicy: { + days: 7 + status: 'enabled' + } + } + } +}