Azure / azure-dev

A developer CLI that reduces the time it takes for you to get started on Azure. The Azure Developer CLI (azd) provides a set of developer-friendly commands that map to key stages in your workflow - code, build, deploy, monitor, repeat.
https://aka.ms/azd
MIT License
365 stars 163 forks source link

Container resources cannot use reference expressions #3732

Closed davidfowl closed 3 days ago

davidfowl commented 2 weeks ago

Aspire Container resources end up being generated in the provision phases of azd deployments. This causes problems when environment variables refer to project urls. We end up with go templates in the bicep definition which fail to be evaluated. We should move container resources to yaml the docker files and projects

var builder = DistributedApplication.CreateBuilder(args);

var c = builder.AddRedis("cache");

c.WithEnvironment(context =>
{
    context.EnvironmentVariables["REDIS_EP"] = c.GetEndpoint("tcp");
});

builder.Build().Run();
resource cache 'Microsoft.App/containerApps@2023-05-02-preview' = {
  name: 'cache'
  location: location
  properties: {
    environmentId: containerAppEnvironment.id
    configuration: {
      activeRevisionsMode: 'Single'
      ingress: {
        external: false
        targetPort: 6379
        transport: 'tcp'
      }
    }
    template: {
      containers: [
        {
          image: 'docker.io/library/redis:7.2.4'
          name: 'cache'
          env: [
            {
              name: 'REDIS_EP'
              value: 'tcp://cache.internal.{{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN }}:6379'
            }
          ]
        }
      ]
      scale: {
        minReplicas: 1
      }
    }
  }
  tags: union(tags, {'aspire-resource-name': 'cache'})
}
ellismg commented 2 weeks ago

We should move container resources to yaml the docker files and projects

We'll need to place these in the same directory as the app host when we infra synth them.

We'll also need to update the project importer logic so that these are pushed up during azd deploy, by creating synthetic projects for them.

davidfowl commented 2 weeks ago

https://github.com/Azure/azure-dev/issues/3409 😄 ??????

ellismg commented 3 days ago

Will be fixed by #3820. The generated cache.tmpl.yaml file looks like this now:

location: {{ .Env.AZURE_LOCATION }}
identity:
  type: UserAssigned
  userAssignedIdentities:
    ? "{{ .Env.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID }}"
    : {}
properties:
  environmentId: {{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_ID }}
  configuration:
    activeRevisionsMode: single
    ingress:
      external: false
      targetPort: {{ targetPortOrDefault 6379 }}
      transport: tcp
      allowInsecure: false
    registries:
      - server: {{ .Env.AZURE_CONTAINER_REGISTRY_ENDPOINT }}
        identity: {{ .Env.AZURE_CONTAINER_REGISTRY_MANAGED_IDENTITY_ID }}
  template:
    containers:
      - image: {{ .Image }}
        name: cache
        env:
          - name: AZURE_CLIENT_ID
            value: {{ .Env.MANAGED_IDENTITY_CLIENT_ID }}
          - name: REDIS_EP
            value: tcp://cache.internal.{{ .Env.AZURE_CONTAINER_APPS_ENVIRONMENT_DEFAULT_DOMAIN }}:6379
    scale:
      minReplicas: 1
tags:
  azd-service-name: cache
  aspire-resource-name: cache
davidfowl commented 2 days ago

@ellismg I'd like to only emit the managed identity good if we need to. That might be a longer-term fix though as it requires knowing a if any bicep based resources are referenced.