简体   繁体   English

如果不存在,CDK 创建资源 - typescript

[英]CDK create resource if does not exist - typescript

Created a dynamoDB table in my CDK project.在我的 CDK 项目中创建了一个 dynamoDB 表。 this is fine it is used by lambdas created in the project.这很好,它由项目中创建的 lambda 使用。 We needed to delete the stack which is also fine as we have retain resource set to true on the table.我们需要删除堆栈,这也很好,因为我们在表上将 retain resource 设置为 true。

Now when I try a fresh deploy we get table already exists error and stack rolls back.现在,当我尝试全新部署时,我们得到表已存在的错误并且堆栈回滚。 I need code that will create the table only if it does not exist.我需要仅在表不存在时才创建表的代码。

Here is basic creation of a table, i cannot find any documentation anywhere on this issue or even an exception that can be caught or where i can see the type of exception that gets thrown to catch as we only see logs in the cloudformation console on AWS console.这是表的基本创建,我找不到任何关于这个问题的文档,甚至找不到可以捕获的异常,也找不到抛出的异常类型,因为我们只在 AWS 上的 cloudformation 控制台中看到日志安慰。

 const dynamoTable = new Table(this, "my-table", {
      tableName: StackConfiguration.tableName,
      partitionKey: { name: "id", type: AttributeType.STRING },
    });

Unfortunately you can't do that in CDK, because CDK generates CloudFormation template at compile time, not at runtime.不幸的是,您不能在 CDK 中执行此操作,因为 CDK 在编译时而非运行时生成 CloudFormation 模板。 I see several options here:我在这里看到几个选项:

  1. Use CloudFormation Resource Import to import existing table into your stack使用CloudFormation Resource Import将现有表导入您的堆栈
  2. Use Custom resource lambda to do an AWS API call to check if table exists.使用自定义资源 lambda 执行 AWS API 调用以检查表是否存在。 Use Custom resource output in Fn.conditionEquals in CDK code to create table conditionally使用CDK代码中Fn.conditionEquals中的Custom resource output有条件地建表

I would recommend going with first option if it's a one-off thing you need to do, and option 2 if you expect this to happen regularly.如果这是您需要做的一次性事情,我建议您选择第一个选项;如果您希望这种情况经常发生,我建议您使用选项 2。

This isn't a great answer but a workaround, I will leave it here incase it might be of use to someone but we can add the table creation into a try catch in our code, I just caught a general exception rather than a specific one i would be interested if anyone had the correct exception to catch here.这不是一个很好的答案,而是一种解决方法,我将它留在这里以防它可能对某人有用,但我们可以将表创建添加到我们代码中的 try catch 中,我只是捕获了一般异常而不是特定异常如果有人在这里捕获正确的异常,我会很感兴趣。 This means the stack will deploy.这意味着堆栈将部署。

 try {
     const dynamoTable = new Table(this, "my-table", {
      tableName: StackConfiguration.tableName,
      partitionKey: { name: "id", type: AttributeType.STRING },
    });
    
      return dynamoReplayTable;
    } catch (e) {
      return;
    }

If you want to use the table then in your code you will need to reference the ARN rather than the table variable name or else you could do some import from name thing in the catch block.如果您想使用该表,那么在您的代码中,您将需要引用 ARN 而不是表变量名称,否则您可以在 catch 块中从名称进行一些导入。 But the best solution I have found is keep the tables in a separate stack.但我发现的最佳解决方案是将表放在单独的堆栈中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM