aspire icon indicating copy to clipboard operation
aspire copied to clipboard

Allow configuration of Azure container app resource allocation in code

Open AlpineJBoehnen opened this issue 1 year ago • 11 comments

Is there an existing issue for this?

  • [x] I have searched the existing issues

Is your feature request related to a problem? Please describe the problem.

Based on discussion https://github.com/dotnet/aspire/discussions/2069

I would like to configure CPU and memory usage in code (e.g., in Program.cs) for Aspire projects, without relying solely on Azure-specific or YAML-based configuration. Currently, there appears to be no built-in way to do this within Aspire itself.

Describe the solution you'd like

Introduce an API or extension methods in Aspire that let developers specify CPU and memory limits directly in code. This approach would allow local or code-based configuration that mimics production-like settings.

Additional context

I initially thought this might be possible using Azure.Provisioning, but after looking more closely, it seems Aspire does not expose CPU/memory configuration in code. Adding such functionality would greatly improve our development workflow.

AlpineJBoehnen avatar Apr 11 '25 14:04 AlpineJBoehnen

I initially thought this might be possible using Azure.Provisioning, but after looking more closely, it seems Aspire does not expose CPU/memory configuration in code. Adding such functionality would greatly improve our development workflow.

https://github.com/dotnet/aspire/issues/8000#issuecomment-2724307587

davidfowl avatar Apr 15 '25 04:04 davidfowl

@karolz-ms @danegsta Do we have an api in dcp to set container limits?

davidfowl avatar May 14 '25 08:05 davidfowl

This has been working for me

var redisCache = builder.AddRedis("redis-cache")
    .WithLifetime(ContainerLifetime.Persistent)
    .PublishAsAzureContainerApp((module, app) =>
    {
        app.Template.Scale.MinReplicas = 1; // minReplicas.AsProvisioningParameter(module);
        app.Template.Scale.MaxReplicas = maxReplicas.AsProvisioningParameter(module);
        var c = app.Template.Containers.FirstOrDefault()?.Value;
        if (c != null)
        {
            c.Resources.Cpu = 0.25;
            c.Resources.Memory = "0.5Gi";
        }
    });

Lindsay-Mathieson avatar May 14 '25 10:05 Lindsay-Mathieson

We haven’t exposed any resource limit settings for containers, but it’d be straightforward to add them if there’s interest.

danegsta avatar May 14 '25 15:05 danegsta

We haven’t exposed any resource limit settings for containers, but it’d be straightforward to add them if there’s interest.

Its pretty important for cost control on Azure.

Is the way I'm doing above incorrect? it seems to work.

Lindsay-Mathieson avatar May 14 '25 16:05 Lindsay-Mathieson

We haven’t exposed any resource limit settings for containers, but it’d be straightforward to add them if there’s interest.

Yes, lets do that and we'll expose the same in the app model.

Is the way I'm doing above incorrect? it seems to work.

Yes, it's correct. We're discussing if we should also have the same for local development.

davidfowl avatar May 14 '25 17:05 davidfowl

Yes, it's correct. We're discussing if we should also have the same for local development.

D'oh, I'm an idiot, sorry!

Lindsay-Mathieson avatar May 14 '25 20:05 Lindsay-Mathieson

Would be a nice feature, I was running into the issue that my SQL-server instance was failing to start since it had not enough ram allocation.

brumbrum05 avatar Oct 29 '25 11:10 brumbrum05

Any reason why the container resource wouldn't expose everything that would be available through regular old docker run? I'm very interested in being able to limit cpu/memory usage in local environments.

danspark avatar Nov 04 '25 15:11 danspark

this works for me:


var acr = builder.AddAzureContainerRegistry("acr");
var acaEnv = azure.AddAzureContainerAppEnvironment("app-environment")
                  .WithAzureContainerRegistry(acr);


var sql = builder
.AddSqlServer("sql")
.WithHostPort(1433)
.PublishAsAzureContainerApp((infra, app) =>
 {
     // Grab the single container in this app
     ContainerAppContainer? c = app.Template.Containers.Single().Value;

     if (c == null)
     {
         throw new InvalidOperationException("No container found in the Container App template.");
     }

     c.Resources ??= new AppContainerResources();
     c.Resources.Cpu = 1.5;
     c.Resources.Memory = "3Gi";
 });

var db = sql.AddDatabase("DbContext");

brumbrum05 avatar Nov 05 '25 09:11 brumbrum05

@danspark We just haven't prioritized it over other features.

davidfowl avatar Nov 05 '25 16:11 davidfowl