Búsqueda de sitios web

typescript - Implementación de ECS ApplicationLoadBalancedFargateService atascada [Posiblemente al cargar Secrets]


Al inyectar secretos en el objeto Secrets de las taskImageOptions de ApplicationLoadBalancedFargateService importadas desde sm.Secret.fromSecretCompleteArn (incluido el guion de 6 dígitos), el despliegue se queda atascado y no funciona.

También he intentado importar los secretos mediante:

sm.Secret.fromSecretNameV2 y el comportamiento es el mismo.

Comportamiento esperado

ECS para desplegar con secretos personalizados

Comportamiento Actual

El despliegue de CDK se congeló atascado en el despliegue de ECS apiService

Pasos de reproducción

const importedSecrets = SECRET_NAMES.reduce((acc, key) => {
    const secret = sm.Secret.fromSecretCompleteArn(
      scope,
      `${config.ENVIRONMENT}/${key}`,
      secretFullArns[key], // full ARN of the secret identical as AWS
    );
    return {...acc, [key]: secret};
  }, {} as {[key: string]: sm.ISecret});

Donde SECRET_NAMES es un arreglo de nombres secretos Entonces,

  const secretsJson = SECRET_NAMES.reduce(
    (acc, key) => ({
      ...acc,
      [key]: ecs.Secret.fromSecretsManager(importedSecrets[key]),
    }),
    {},
  );

y luego en el creador de servicios de Fargate:

// Fargate service
this.backendService = new ecsPatterns.ApplicationLoadBalancedFargateService(
  scope,
  'apiService',
  {
    serviceName: 'apiService',
    cluster: this.ecsCluster,
    taskSubnets: {
      subnetType: ec2.SubnetType.PRIVATE_WITH_NAT,
    },
    memoryLimitMiB: 2048,
    cpu: 1024,
    desiredCount: 1,
    taskImageOptions: {
      containerName: 'apiContainer',
      image: ecs.ContainerImage.fromAsset('../api-backend/', {
        followSymlinks: SymlinkFollowMode.ALWAYS,
      }),
      containerPort: config.PORT,
      environment: {
        NODE_ENV: 'development',
        AWS_DEFAULT_REGION: config.AWS_DEFAULT_REGION,
      },
      logDriver: ecs.LogDrivers.awsLogs({
        streamPrefix: `${config.PROJECT_NAME}-logStream`,
        logGroup: fargateLog,
      }),
      secrets: {
        DB_BUSINESS_PASSWORD: ecs.Secret.fromSecretsManager(
          dbBusinessPasswordSecret,
          'password',
        ),
        DB_BUSINESS_HOST: ecs.Secret.fromSecretsManager(
          dbBusinessPasswordSecret,
          'host',
        ),


        ...secretsJson. 
        // ===========>>> If i comment this line here, the deploy is successfull, but without the secrets i want.
        


      },
    },
  },
);

En la plantilla de CDK veo el nombre correcto del secreto PERO SIN EL GUION

PLANTILLA CDK Secretos de ECS:

....
{
  "Name": "X_API_KEY",
  "ValueFrom": {
    "Fn::Join": [
      "",
      [
        "arn:",
        {
          "Ref": "AWS::Partition"
        },
        "secret-arn-as-it-is-in-aws-console/co****/staging/X_API_KEY"
// The secret arn is exactly equal as it is in aws but without the 6 char hyphen auto-generated when the secret was uploaded.
      ]
    ]
  }
},
...

El nombre del parámetro es: /co****/staging/X_API_KEY Pero en la consola de AWS el ARN del secreto es exactamente el mismo que el de la plantilla de CDK pero sin el guion. Estoy bastante seguro de que esa es la causa del bloqueo en el despliegue.

Además, los permisos de IAM de la tarea de imagen están bien, por ejemplo para el secreto cuyo nombre es: /co***/staging/CIPHER_SECRET-?????? donde los ? representan el guion automático de 6 dígitos

Actualizar :

He intentado usar fromSecretAttributes como se sugiere en https://docs.aws.amazon.com/cdk/v2/guide/get_secrets_manager_value.html

y el código es:

const importedSecrets = SECRET_NAMES.reduce((acc, key) => {
  const secret = sm.Secret.fromSecretAttributes(
    scope,
    `/cobuy/${config.ENVIRONMENT}/${key}`,
    {secretCompleteArn: secretFullArns[key]}, // full ARN with 6 digit hyphen
  );
  return {...acc, [key]: secret};
}, {} as {[key: string]: sm.ISecret});

const secretsJson = SECRET_NAMES.reduce(
  (acc, key) => ({
    ...acc,
    [key]: ecs.Secret.fromSecretsManager(importedSecrets[key]),
  }),
  {} as {[key: string]: ecs.Secret},
);

Todavía no funciona, pero ahora la síntesis de CDK lanza un error:

debe usar solo uno de secretCompleteArn o secretPartialArn

Datos de interés:

"aws-cdk-lib": "^2.27.0"

node -v => 14.19

Typescript "^4.7.2"

Otra información

CDK JSON

{
  "app": "npx ts-node --prefer-ts-exts bin/CdkStarter.ts",
  "context": {
    "@aws-cdk/aws-apigateway:usagePlanKeyOrderInsensitiveId": true,
    "@aws-cdk/aws-rds:lowercaseDbIdentifier": true,
    "@aws-cdk/aws-efs:defaultEncryptionAtRest": true,
    "@aws-cdk/aws-cloudfront:defaultSecurityPolicyTLSv1.2_2021": false,
    "@aws-cdk/core:stackRelativeExports": false
  }
}