Allow configuration of Azure container app resource allocation in code
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.
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
@karolz-ms @danegsta Do we have an api in dcp to set container limits?
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";
}
});
We haven’t exposed any resource limit settings for containers, but it’d be straightforward to add them if there’s interest.
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.
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.
Yes, it's correct. We're discussing if we should also have the same for local development.
D'oh, I'm an idiot, sorry!
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.
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.
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");
@danspark We just haven't prioritized it over other features.