Azure / Azure/bicep-types-az

Mysql Flexible servers with replication are not idempotent

Open
#2,106 1 comment 0 reactions 0 assignees View on GitHub
Needs: Triage :mag:
Dominant language
TypeScript
Stars
108
Forks
44
Avg merge
18h 53m
Merged PRs (30d)
29

Description

**Bicep version**

```
❯ az bicep version
Bicep CLI version 0.26.54 (5e20b29b58)
```

**Describe the bug**

When deploying a MySQL Flexible server with another server as replication, the template is not idempotent. It complaints that the database name is already in use.

**To Reproduce**

Apply the following template using stacks:

Template:

```bicep
@export()
type DatabaseSKU =
| 'Standard_D2ads_v5'
| 'Standard_D4ads_v5'
| 'Standard_D8ads_v5'
| 'Standard_D16ads_v5'
| 'Standard_D32ads_v5'
| 'Standard_D48ads_v5'
| 'Standard_D64ads_v5'
| 'Standard_D96ads_v5'

param name string
param location string = resourceGroup().location
param tags { *: string } = {}
param sku DatabaseSKU = 'Standard_D2ads_v5'
param administratorLogin string
@secure()
param administratorLoginPassword string
param storageSizeGB int = 64
param iops int = 1000
param autoGrow 'Enabled' | 'Disabled' = 'Enabled'
param autoIoScaling 'Enabled' | 'Disabled' = 'Enabled'
param logOnDisk 'Enabled' | 'Disabled' = 'Disabled'
param publicNetworkAccess 'Enabled' | 'Disabled' = 'Disabled'
param backupRetentionDays int = 10
param geoRedundantBackup bool = false
param highlyAvailable bool = false

resource sourceServer 'Microsoft.DBforMySQL/flexibleServers@2023-06-30' = {
name: guid(name)
location: location
tags: tags
sku: { name: sku, tier: 'GeneralPurpose' }
properties: {
administratorLogin: administratorLogin
administratorLoginPassword: administratorLoginPassword

storage: {
storageSizeGB: storageSizeGB
iops: iops
autoGrow: autoGrow
autoIoScaling: autoIoScaling
logOnDisk: logOnDisk
}

replicationRole: 'Source'

backup: {
backupRetentionDays: backupRetentionDays
geoRedundantBackup: geoRedundantBackup ? 'Enabled' : 'Disabled'
}
highAvailability: { mode: highlyAvailable ? 'ZoneRedundant' : 'Disabled' }
network: { publicNetworkAccess: publicNetworkAccess }

version: '8.0.21'
}
}

resource relicaServer 'Microsoft.DBforMySQL/flexibleServers@2023-06-30' = {
name: guid('${name}-replica')
location: location
tags: tags
sku: { name: sku, tier: 'GeneralPurpose' }
properties: {
administratorLogin: administratorLogin
administratorLoginPassword: administratorLoginPassword

storage: {
storageSizeGB: storageSizeGB
iops: iops
autoGrow: autoGrow
autoIoScaling: autoIoScaling
logOnDisk: logOnDisk
}

replicationRole: 'Replica'
createMode: 'Replica'
sourceServerResourceId: sourceServer.id

backup: {
backupRetentionDays: backupRetentionDays
geoRedundantBackup: geoRedundantBackup ? 'Enabled' : 'Disabled'
}
highAvailability: { mode: highlyAvailable ? 'ZoneRedundant' : 'Disabled' }
network: { publicNetworkAccess: publicNetworkAccess }

version: '8.0.21'
}
}

```

Deploy command:

```sh
az stack group create -g MyResourceGroup -n DatabaseStack --dm none --delete-all --yes -f ./database-with-replica.bicep -p database-with-replica.bicepparam
```

After a successful deployment, try to redeploy the same stack with the same command:

```sh
az stack group create -g MyResourceGroup -n DatabaseStack --dm none --delete-all --yes -f ./database-with-replica.bicep -p database-with-replica.bicepparam
```

You will receive an error similar to this:

```
(DeploymentStackDeploymentFailed) One or more resources could not be deployed. Correlation id: '.......................'.
Code: DeploymentStackDeploymentFailed
Message: One or more resources could not be deployed. Correlation id: '.......................'.
Exception Details: (DeploymentFailed) At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.
Code: DeploymentFailed
Message: At least one resource deployment operation failed. Please list deployment operations for details. Please see https://aka.ms/arm-deployment-operations for usage details.
Target: /subscriptions/................./resourceGroups/........./providers/Microsoft.Resources/deployments/DatabaseStack..........
Exception Details: (ServerNameAlreadyExists) Specified server name is already used.
Code: ServerNameAlreadyExists
Message: Specified server name is already used.
```

**Additional context**

According to the [Bicep documentation](https://learn.microsoft.com/en-us/azure/azure-resource-manager/bicep/overview?tabs=bicep), all the templates should be idempotent.

> Repeatable results: Repeatedly deploy your infrastructure throughout the development lifecycle and have confidence your resources are deployed in a consistent manner. Bicep files are idempotent, which means you can deploy the same file many times and get the same resource types in the same state. You can develop one file that represents the desired state, rather than developing lots of separate files to represent updates.

This template is not idempotent by the fact that I cannot deploy them twice. This is crucial because I have other resources in the same stack and my deployment would always fail if I change other resources that are not the database and try to redeploy the whole stack.

Contributor guide

No contributing guide indexed for this repository

Research direction

Start by deploying the template in the issue with az stack group create, then repeat the same command and capture the deployment operations for ServerNameAlreadyExists. Investigate how Microsoft.DBforMySQL/flexibleServers@2023-06-30 handles createMode: 'Replica' and sourceServerResourceId during repeated stack deployments. Done means the unchanged stack can be deployed again without attempting to recreate an existing server.

Written by the indexing model from the issue text.

Assessment

Tech stack
azure, mysql
Domain
cloud, databases
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.