Repository navigation
How to generate environment-dependent Bicep expressions from Aspire? #18927
Replies: 1 comment
|
Yes, this works, and you don't need to hand-write Bicep. using Azure.Provisioning;
using Azure.Provisioning.CosmosDB;
using Azure.Provisioning.Expressions;
var environmentName = builder.AddParameter("environmentName");
builder.AddAzureCosmosDB("cosmos")
.WithParameter("environmentName", environmentName)
.ConfigureInfrastructure(infra =>
{
var envName = new ProvisioningParameter("environmentName", typeof(string));
infra.Add(envName);
var account = infra.GetProvisionableResources().OfType<CosmosDBAccount>().Single();
account.BackupPolicy = new ContinuousModeBackupPolicy
{
ContinuousModeTier = new ConditionalExpression(
new BinaryExpression(envName.Value.Compile(), BinaryBicepOperator.Equal, new StringLiteralExpression("Production")),
new StringLiteralExpression("Continuous30Days"),
new StringLiteralExpression("Continuous7Days")),
};
});I ran this with Aspire 13.6.4 ( param environmentName string
...
backupPolicy: {
type: 'Continuous'
continuousModeProperties: {
tier: (environmentName == 'Production') ? 'Continuous30Days' : 'Continuous7Days'
}
}and the manifest wires the parameter through: "cosmos": {
"type": "azure.bicep.v0",
"path": "cosmos.module.bicep",
"params": { "environmentName": "{environmentName.value}" }
}That gives you what you're after on both sides:
One thing to avoid: branching in C# ( To compare on something other than equality, |
Uh oh!
There was an error while loading. Please reload this page.
Hi Aspire Team,
We're using Aspire to provision Azure resources, including Azure Cosmos DB.
For Cosmos DB we'd like to configure the backup policy dynamically based on the deployment environment:
Continuous30DaysContinuous7DaysIf deployment is performed exclusively through the Aspire CLI, handling this in C# is straightforward. However, we also generate Bicep locally and commit it as part of our workflow. During CI/CD, we regenerate the Bicep and compare it with the committed version to detect Aspire version mismatches (and then deploy via Aspire CLI).
While looking for a solution, I found the
BicepExpressiontype:https://learn.microsoft.com/en-us/dotnet/api/azure.provisioning.expressions.bicepexpression?view=azure-dotnet
However, I haven't been able to find much documentation or any practical examples showing how it can be used within Aspire/Azure Provisioning resources to emit conditional expressions into the generated Bicep and in a same time handle C# value assignment.
Is this a supported scenario? I'm starting to wonder whether Bicep generation is overengineering this use case.
Thanks!
All reactions