代码之家  ›  专栏  ›  技术社区  ›  spitfiredd

向EcsJobDefinition添加机密值

  •  0
  • spitfiredd  · 技术社区  · 1 年前

    我正在尝试为我的ECS作业定义添加一个秘密值,

            secret_id = f"mysecretid"
            secret = secretsmanager.Secret.from_secret_name_v2(
                self,
                secret_id,
                secret_name=secret_id,
            )
    
            # Mongo DB URI
            mongodb_uri = ecs.Secret.from_secrets_manager(secret, "MONGODB_URI")
    
            job_definition = batch.EcsJobDefinition(self, f"{stage}{NAME}JobDefinition",
                container=batch.EcsEc2ContainerDefinition(self, "Container",
                    image=image,
                    memory=Size.mebibytes(4096),
                    cpu=2,
                    secrets={"MONGO_DB_URI": mongodb_uri},
                    command=["npm run crawl"],
                )
            )      
    

    我遇到了错误,

    RuntimeError: Passed to parameter props of new aws-cdk-lib.aws_batch.EcsEc2ContainerDefinition: Unable to deserialize value as aws-cdk-lib.aws_batch.EcsEc2ContainerDefinitionProps
    ├── 🛑 Failing value is an object
    │      { '$jsii.struct': [Object] }
    ╰── 🔍 Failure reason(s):
        ╰─ Key 'secrets': Unable to deserialize value as map<aws-cdk-lib.aws_batch.Secret> | undefined
            ├── 🛑 Failing value is an object
            │      { '$jsii.map': [Object] }
            ╰── 🔍 Failure reason(s):
                ╰─ Key 'MONGO_DB_URI': Unable to deserialize value as aws-cdk-lib.aws_batch.Secret
                    ├── 🛑 Failing value is an object
                    │      { '$jsii.byref': 'aws-cdk-lib.aws_ecs.Secret@10003' }
                    ╰── 🔍 Failure reason(s):
                        ╰─ Object of type 'aws-cdk-lib.aws_ecs.Secret' is not convertible to aws-cdk-lib.aws_batch.Secret
    
    1 回复  |  直到 1 年前
        1
  •  0
  •   Mark B    1 年前

    错误消息相当清楚:

    Object of type 'aws-cdk-lib.aws_ecs.Secret' is not convertible to aws-cdk-lib.aws_batch.Secret
    

    由于您创建的是Batch Job而不是ECS Task,因此它需要的是Batch机密而不是ECS机密。您需要使用 batch version 的秘密引用。

    将您的代码更改为:

    # Mongo DB URI
    mongodb_uri = batch.Secret.from_secrets_manager(secret, "MONGODB_URI")