aspire icon indicating copy to clipboard operation
aspire copied to clipboard

PublishAsAzureContainerApp with Resources results in broken bicep

Open Fabian-Schmidt opened this issue 9 months ago • 13 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Describe the bug

When publishing an Azure Container App and configuring the resources results in not working bicep template.

The AppHost C# Code is:

    .PublishAsAzureContainerApp((module, app) =>
    {
        var container = app.Template.Containers.Single().Value!;
        container.Resources.Cpu = 0.25;
        container.Resources.Memory = "200Mi";
    });

Please note the Cpu field is of type BicepValue<double>.

The resulting bicep template:

resource frontend 'Microsoft.App/containerApps@2024-03-01' = {
  name: 'frontend'
  location: location
  properties: {
    configuration: {
      activeRevisionsMode: 'Single'
      ingress: { ... }
      registries: [ ... ]
    }
    environmentId: outputs_azure_container_apps_environment_id
    template: {
      containers: [
        {
          image: frontend_containerimage
          name: 'frontend'
          env: [...]
          resources: {
            cpu: '0,25'
            memory: '200Mi'
          }
        }

During azd up process the following is thrown:

{
  "error": {
    "code": "InvalidTemplateDeployment",
    "message": "The template deployment 'frontend-1741704029' is not valid according to the validation procedure. The tracking id is '40ca46c2-f9a0-4d8d-be3a-97023f6a8b6a'. See inner errors for details.",
    "details": [
      {
        "code": "ValidationForResourceFailed",
        "message": "Validation failed for a resource. Check 'Error.Details[0]' for more information.",
        "details": [
          {
            "code": "ContainerAppInvalidSchema",
            "message": "Invalid request body for container app. Path: $[0].resources.cpu. Does not conform to Container App schema, please visit for more information https://docs.microsoft.com/azure/container-apps/azure-resource-manager-api-spec?tabs=arm-template#container-app"
          }
        ]
      }
    ]
  }
}

Expected Behavior

The bicep template is working and the container is deployed with the configured cpu and memory settings.

Steps To Reproduce

No response

Exceptions (if any)

No response

.NET Version info

.NET SDK:
 Version:           9.0.200
 Commit:            90e8b202f2
 Workload version:  9.0.200-manifests.69179adf
 MSBuild version:   17.13.8+cbc39bea8

Runtime Environment:
 OS Name:     Windows
 OS Version:  10.0.19045
 OS Platform: Windows
 RID:         win-x64
 Base Path:   C:\Program Files\dotnet\sdk\9.0.200\

Anything else?

  • Aspire.Hosting.AppHost - 9.1.0
  • Aspire.Hosting.Azure.AppContainers - 9.1.0

Fabian-Schmidt avatar Mar 11 '25 15:03 Fabian-Schmidt

cc @tg-msft

davidfowl avatar Mar 11 '25 23:03 davidfowl

I'm having the same problem. Bicep expects an integer but got a string.

Image

However, this works for some reason, but when some other error occurs, this validation error is displayed. This will mislead people in their debugging process.

DanielToft avatar Mar 13 '25 08:03 DanielToft

I'm having the same problem. Bicep expects an integer but got a string.

Those are known warnings we need to fix, but they don't cause deployment errors. This is different to what @Fabian-Schmidt is running into

davidfowl avatar Mar 13 '25 14:03 davidfowl

I got the following workaround for the issue:

container.Resources.Cpu = Azure.Provisioning.Expressions.BicepFunction.ParseJson("0.25").Compile();
container.Resources.Memory = "0.5Gi";

This results in the following working bicep:

resources: {
  cpu: json('0.25')
  memory: '0.5Gi'
}

A bit bizarre to write it like this. Hopefully this can be fixed so I can remove this hack.

Fabian-Schmidt avatar Mar 14 '25 10:03 Fabian-Schmidt

Yes this is clearly a bug in the Azure.Provisioning libraries.

davidfowl avatar Mar 14 '25 14:03 davidfowl

is this somehow related to #7685 ?

Juulsn avatar Mar 14 '25 16:03 Juulsn

I don't know how they are related.

davidfowl avatar Mar 14 '25 18:03 davidfowl

Yes this is clearly a bug in the Azure.Provisioning libraries.

So must I raise this bug in another repository?

Fabian-Schmidt avatar Mar 29 '25 12:03 Fabian-Schmidt

@Fabian-Schmidt please file an issue in this repos https://github.com/azure/azure-sdk-for-net and link to here for visibility.

sebastienros avatar Apr 10 '25 19:04 sebastienros

We can file it.

davidfowl avatar Apr 10 '25 19:04 davidfowl

My workaround is adding this line to the app host:

CultureInfo.DefaultThreadCurrentCulture = CultureInfo.InvariantCulture;

jordy-bencom avatar Apr 17 '25 08:04 jordy-bencom

@jordy-bencom 's comment is interesting because the code serializing the cpu property seems correct:

https://github.com/Azure/azure-sdk-for-net/blob/main/sdk/containerapps/Azure.ResourceManager.AppContainers/src/Generated/Models/AppContainerResources.Serialization.cs#L38-L42

And the wrong format is not invariant serialization (comma as the decimal separator). I will try to repro.

Update:

  • this might not be the code used to serialized though
  • the culture only changes the format of the separator, not the fact it's a string. But it's still a problem, the serialization of the bicep should not be culture sensitive.

sebastienros avatar Apr 28 '25 19:04 sebastienros

https://github.com/Azure/azure-sdk-for-net/issues/49687

sebastienros avatar Apr 28 '25 19:04 sebastienros

This is now fixed with the latest nightly build (using Azure.Provisioning v1.0.1). One note is that the CPU and Memory configuration needs to match an available config in ACA. So the code I used was:

    .PublishAsAzureContainerApp((module, app) =>
    {
        var container = app.Template.Containers.Single().Value!;
        container.Resources.Cpu = 0.25;
        container.Resources.Memory = "0.5Gi";
    });

eerhardt avatar Jun 09 '25 17:06 eerhardt