3

When inejcting secrets to the Secrets object of the ApplicationLoadBalancedFargateService's taskImageOptions imported from sm.Secret.fromSecretCompleteArn (including 6 digit hypen), the deploy gets stuck and does not work.

I have also tried importing the secrets by:

sm.Secret.fromSecretNameV2 and the behaviour is the same.

Expected Behavior

ECS to deploy with custom secrets

Current Behavior

CDK deploy freezed stuck at ECS apiService deploy

Reproduction Steps

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});

Where SECRET_NAMES is an array of secret names Then,

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

and then in the fargate service creator:

// 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.
        


      },
    },
  },
);

In the CDK template i see the correct secret name BUT WITHOUT THE HYPHEN

CDK TEMPLATE Secrets from 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.
      ]
    ]
  }
},
...

Parameter name is: /co****/staging/X_API_KEY But in the aws console the arn of the secret is exactly the same as the one in the cdk template but without the hyphen. Im pretty sure that's the cause of the deploy stuck freeze.

Also, IAM Permissions from the image task are fine, for example for the secret who's name is: /co***/staging/CIPHER_SECRET-?????? where the ? represent the auto hyphen 6 digit

Console Synth CDK template

Update:

I've tried to use fromSecretAttributes as suggested per https://docs.aws.amazon.com/cdk/v2/guide/get_secrets_manager_value.html

and the code is:

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},
);

Still not working but now the CDK Synthesis throws an error:

must use only one of secretCompleteArn or secretPartialArn

Interest data:

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

node -v => 14.19

Typescript "^4.7.2"

Other information

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
  }
}
Jp98
  • 63
  • 2
  • 5
  • Where are the imported "secrets" stored in AWS? `SECURE_STRING_SSM_PARAMETERS` suggests the Parameter Store, but the code treats them as Secret Manager secrets. – fedonev Jun 15 '22 at 15:20
  • Thks for the answe @fedonev, The imported secrets are stored as Secrets in the AWS console, the array suggests parameter store but that's just because i have tried also with param store. But trust me, secrets are there in the AWS console and in the same region. That's from where i took the full ARN of each secret. – Jp98 Jun 15 '22 at 16:51
  • Got it. You write _"In the CDK template i see the correct secret name..."_, but the OP template snippet has a SSM Parameter ARN, not a Secret ARN. Please update the OP. Also, OP also says _"Parameter name is..."_. But you are not using parameters, right? – fedonev Jun 15 '22 at 17:26
  • I just updated it, thanks @fedonev, sorry – Jp98 Jun 15 '22 at 18:09
  • Let's confirm IAM permissions. In the synthed template, look for the Task Execution role policy (`AWS::IAM::Policy`). What secret ARN resources are granted the `GetSecretValue` and `DescribeSecretValue` action? We're looking for a secret ARN ending with `-??????` (literally a dash and 6 question marks, which in IAM-speak means all secret versions). – fedonev Jun 15 '22 at 19:08
  • Permissions look fine, i've edited the post and attached proofs that the task execution role has the correct permissions. @fedonev, thanks for help btw. – Jp98 Jun 15 '22 at 20:01
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/245647/discussion-between-fedonev-and-jp98). – fedonev Jun 15 '22 at 20:34
  • @Jp98 were you able to resolve this? I am having the exact same issue right now-my fargate task does not start up, because I am getting: no identity-based policy allows the secretsmanager:GetSecretValue action status code: 400 – Capaj Aug 16 '22 at 13:15

0 Answers0