Skip to content

Commit

Permalink
Azure Container Registry Example (#543)
Browse files Browse the repository at this point in the history
  • Loading branch information
ExchMaster authored Nov 30, 2021
1 parent 5a89168 commit 4467261
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 0 deletions.
63 changes: 63 additions & 0 deletions src/bicep/examples/containerRegistry/README.md
Original file line number Diff line number Diff line change
@@ -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: <https://docs.microsoft.com/en-us/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)
31 changes: 31 additions & 0 deletions src/bicep/examples/containerRegistry/contRegistry.bicep
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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'
}
}
}
}

0 comments on commit 4467261

Please sign in to comment.