【问题标题】:Deploy ECS ApplicationLoadBalancedFargateService isn't loading Secrets部署 ECS ApplicationLoadBalancedFargateService 未加载 Secret
【发布时间】:2022-06-16 02:09:16
【问题描述】:

当向从导入的ApplicationLoadBalancedFargateServicetaskImageOptions 的 Secrets 对象注入秘密时 sm.Secret.fromSecretCompleteArn(包括6位hypen),部署卡住了,不起作用。

我也尝试过通过以下方式导入机密:

sm.Secret.fromSecretNameV2 和行为是一样的。

预期行为

使用自定义密钥部署 ECS

当前行为

CDK 部署冻结卡在 ECS apiService 部署

复制步骤

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

其中SECRET_NAMES 是一组秘密名称 那么,

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

然后在 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.
        


      },
    },
  },
);

在 CDK 模板中,我看到了正确的秘密名称,但没有连字符

来自 ECS 的 CDK 模板秘密:

....
{
  "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.
      ]
    ]
  }
},
...

参数名称为:/co****/staging/X_API_KEY 但在 aws 控制台中,秘密的 arn 与 cdk 模板中的 arn 完全相同,但没有连字符。 我很确定这是部署卡住冻结的原因。

可能的解决方案

没有回应

其他信息/上下文

没有回应

CDK CLI 版本

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

Node.js 版本

14.19

操作系统

马科斯蒙特雷

语言

打字稿

语言版本

“打字稿”:“^4.7.2”

其他信息

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

【问题讨论】:

  • 导入的“秘密”存储在 AWS 的什么位置? SECURE_STRING_SSM_PARAMETERS 建议使用 Parameter Store,但代码将它们视为 Secret Manager 机密。
  • 感谢@fedonev 的回答,导入的机密作为机密存储在AWS 控制台中,该数组建议使用参数存储,但这只是因为我也尝试过使用参数存储。但请相信我,AWS 控制台和同一区域中存在秘密。这就是我获取每个秘密的完整 ARN 的地方。
  • 知道了。你写了 “在 CDK 模板中我看到了正确的秘密名称......”,但是 OP 模板 sn-p 有一个 SSM 参数 ARN,而不是一个秘密 ARN。请更新 OP。此外,OP 还说 “参数名称是...”。但是你没有使用参数,对吧?
  • 我刚刚更新了,谢谢@fedonev,对不起

标签: typescript amazon-web-services amazon-ecs aws-cdk aws-secrets-manager


猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2021-06-10
  • 1970-01-01
  • 2021-11-02
  • 2020-01-25
  • 1970-01-01
  • 2019-05-13
相关资源
最近更新 更多