|
| 1 | +// Copyright (c) Microsoft Corporation. |
| 2 | +// Licensed under the MIT License. |
| 3 | + |
| 4 | +// Note: |
| 5 | +// This Azure Bicep code demonstrates a deployment of a VM that uses a password with two common |
| 6 | +// deployment options. 1. Using a password from the pipeline. 2. Using a password from a Key Vault secret. |
| 7 | + |
| 8 | +// --------------------------------------------------------------- |
| 9 | +// OPTION 1: A VM deployment using the password from the pipeline. |
| 10 | +// --------------------------------------------------------------- |
| 11 | + |
| 12 | +// If your pipeline passes a password in as a parameter to the deployment script, use this option. |
| 13 | +// For expansion with PSRule, a dummy value for `adminPassword` is used set in `ps-rule.yaml` with |
| 14 | +// the `AZURE_PARAMETER_DEFAULTS` configuration option. This allows PSRule to expand the deployment, |
| 15 | +// without exposing your secret in the code or PSRule. |
| 16 | + |
| 17 | +@secure() |
| 18 | +@description('Load the admin password from the pipeline.') |
| 19 | +param adminPassword string |
| 20 | + |
| 21 | +@description('A VM deployment using a password from the pipeline.') |
| 22 | +module vm001 '../../../../../modules/virtual-machine-windows/v1/main.bicep' = { |
| 23 | + params: { |
| 24 | + name: 'vm-001' |
| 25 | + adminPassword: adminPassword |
| 26 | + adminUsername: 'vm-admin' |
| 27 | + imageSKU: '2022-Datacenter' |
| 28 | + size: 'Standard_D4ds_v4' |
| 29 | + subnetId: vnet.id |
| 30 | + tags: { |
| 31 | + env: 'dev' |
| 32 | + } |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// --------------------------------------------------------------------- |
| 37 | +// OPTION 2: A VM deployment using the password from a Key Vault secret. |
| 38 | +// --------------------------------------------------------------------- |
| 39 | + |
| 40 | +// If your VM deployment is able to use a Key Vault secret that is already deployed to Azure, use this option. |
| 41 | +// When you reference a Key Vault secret, PSRule will automatically substitute a placeholder for the secret value |
| 42 | +// during expansion. So you can use the secret in your deployment without exposing it as a deployment parameter. |
| 43 | + |
| 44 | +// NB: PSRule never actually attempts to retrieve the secret value, so it does not need access to the secret. |
| 45 | + |
| 46 | +@description('An existing Key Vault to use for the VM deployment.') |
| 47 | +resource vault 'Microsoft.KeyVault/vaults@2023-07-01' existing = { |
| 48 | + name: 'kv-001' |
| 49 | +} |
| 50 | + |
| 51 | +@description('Load the admin password from a Key Vault secret.') |
| 52 | +module vm002 '../../../../../modules/virtual-machine-windows/v1/main.bicep' = { |
| 53 | + params: { |
| 54 | + name: 'vm-002' |
| 55 | + adminPassword: vault.getSecret('vm002-admin-password') |
| 56 | + adminUsername: 'vm-admin' |
| 57 | + imageSKU: '2022-Datacenter' |
| 58 | + size: 'Standard_D4ds_v4' |
| 59 | + subnetId: vnet.id |
| 60 | + tags: { |
| 61 | + env: 'dev' |
| 62 | + } |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +// --------------- |
| 67 | +// Other resources |
| 68 | +// --------------- |
| 69 | + |
| 70 | +// An existing virtual network and subnet to connect the VM. |
| 71 | +resource vnet 'Microsoft.Network/virtualNetworks/subnets@2023-05-01' existing = { |
| 72 | + name: 'vnet-001/subnet-001' |
| 73 | +} |
0 commit comments