简体   繁体   English

如何将已创建(通过 web 控制台)服务导出到本地 CDK 并能够部署/更新它们?

[英]How to export already created (via web console) services to CDK locally and be able to deploy/update them?

Current situation:现在的情况:

I have an AWS API Gateway referencing some AWS Lambdas, and some Lambdas querying a DynamoDB instance.我有一个 AWS API 网关引用一些 AWS Lambdas,一些 Lambdas 查询 DynamoDB 实例。

All of the above are created and handled manually via the AWS web console.以上所有内容都是通过 AWS web 控制台手动创建和处理的。 There's no cloudformation template for that.没有 cloudformation 模板。


^ I want to be able to have that locally too using CDK: ^ 我也希望能够使用 CDK 在本地拥有它:

I want to apply some healthy developer procedures and create a CDK file system locally, for testing, manage deployment, manage versioning via GitHub or whatever AWS has to offer in that field (didn't get to that part yet).我想应用一些健康的开发人员程序并在本地创建一个 CDK 文件系统,用于测试、管理部署、通过 GitHub 或 AWS 在该领域提供的任何内容(尚未涉及该部分)管理版本控制。

I noticed that there is 0% information on how to do that.我注意到关于如何做到这一点的信息为 0%。 Most tutorials follow a situation where:大多数教程遵循以下情况:

  1. i am creating a cdk from scratch locally我正在本地从头开始创建一个 cdk
  2. or already have a cloudformation structure.或者已经有一个cloudformation结构。

Please help me figure our the best proper way to do that.请帮我想出最好的方法来做到这一点。 Some things that came up but didn't actually do:一些出现但实际上没有做的事情:

  • Do i just init a cdk and name services the same as my current services to "take over them"?我是否只是初始化一个与我当前服务相同的 cdk 和名称服务来“接管它们”?
  • or will they get re-written ( = total disaster).或者他们会被重写(=彻底的灾难)。
  • Is there a way to export a code sample for each service i currently have and connect them with each other?有没有办法为我目前拥有的每项服务导出代码示例并将它们相互连接?

You must import existing resource's to CDK.您必须将现有资源导入 CDK。

https://link.medium.com/1RbcEdal4wb https://link.medium.com/1RbcEdal4wb

TL;DR Use cdk import for supported resources. TL;DR使用cdk import获取支持的资源。 Re-create the RestApi from an exported OpenApi Definition.从导出的 OpenApi 定义重新创建 RestApi。


cdk import

The CDK has experimental import functionality to bring existing console-created resources under CDK management. CDK 具有实验性的导入功能,可以将现有的控制台创建的资源置于 CDK 管理之下。 The cdk import CLI command piggybacks off the related CloudFormation resource import operation. cdk import CLI 命令搭载了相关的 CloudFormation 资源导入操作。

Not all resources support the import operation .并非所有资源都支持导入操作 The AWS::Lambda::Function and AWS::DynamoDB::Table resources are supported.支持AWS::Lambda::FunctionAWS::DynamoDB::Table资源。 You must also consider secondary resources, such as the Lambda's execution role ( AWS::IAM::Role is supported for importing).您还必须考虑辅助资源,例如 Lambda 的执行角色(支持导入AWS::IAM::Role )。

Resource importing starts with manually configuring a CDK stack that matches the existing cloud-side configuration.资源导入从手动配置与现有云端配置相匹配的 CDK 堆栈开始。 To guide your work, say, on recreating a dynamodb.Table in CDK, consider running the DescribeTable API to get a dump of the current configuration.为了指导您的工作,例如,在 CDK 中重新创建dynamodb.Table ,请考虑运行DescribeTable API 以获取当前配置的转储。 Because of the manual work involved, it's wise to focus your importing energies on stateful resources and consider simply destroying and recreating stateless resources.由于涉及手动工作,将导入的精力集中在有状态资源上并考虑简单地销毁和重建无状态资源是明智的。

Once the app is complete, run the cdk import command.应用程序完成后,运行cdk import命令。 After that, the imported resources can be modified like any other CDK resource.之后,可以像修改任何其他 CDK 资源一样修改导入的资源。

CDK RestApi from an exported OpenApi Definition来自导出的 OpenApi 定义的 CDK RestApi

AWS::ApiGateway::RestApi is not on the list of supported resources for import 1 . AWS::ApiGateway::RestApi不在 import 1支持的资源列表中。 A Plan B for your Api Gateway is to export your API as an OpenAPI defnition 2 . Api 网关的 B 计划是将 API 导出为 OpenAPI 定义2 Then pass the JSON as the API definition to CDK RestApi construct with the ApiDefinition.fromAsset method.然后使用ApiDefinition.fromAsset方法将 JSON 作为 API 定义传递给 CDK RestApi 构造。 This will create a new API, not import it per se.这将创建一个新的 API,而不是导入它本身。


  1. Although the RestApi resource itself is not supported for importing, many related AWS::ApiGateway resources are, such as Resource, Stage, Model and Method.虽然RestApi资源本身不支持导入,但是很多相关的AWS::ApiGateway资源是支持导入的,比如Resource, Stage, Model和Method。
  2. See the AWS How do I migrate API Gateway REST APIs between AWS accounts or Regions?请参阅 AWS 如何在 AWS 账户或区域之间迁移 API 网关 REST API? for a non-CDK use case.对于非 CDK 用例。

暂无
暂无

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

相关问题 AWS CDK:如何通过名称获取已创建角色或策略的“Arn” - AWS CDK : how to get 'Arn' of an already created role or policy by their names 如何通过 CDK 将自定义标头添加到 API 网关 RestAPI 集成响应(它通过 web 控制台工作) - How do I add custom headers to API Gateway RestAPI Integration responses via CDK (it works through the web console) AWS CDK - 如何在本地运行 API 和 Lambdas? - AWS CDK - How to run API and Lambdas locally? 哪个角色需要 getSecrets 权限才能部署此 CDK 堆栈? - Which role needs getSecrets permissions to be able to deploy this CDK stack? 如何将 AWS CDK 堆栈部署到多个账户? - How to deploy AWS CDK stacks to multiple accounts? 在已经创建的资源上部署 lambda function - Deploy lambda function on already created resources 如何在已创建的 ECS 服务上更新选项“propagateTags”? - How can I update the option "propagateTags" on an already created ECS service? 如何在 AWS-CDK 应用程序中形成没有 cdk synt 和 cdk deploy 的云形成模板? - How to form cloud formation template without cdk synt and cdk deploy in AWS-CDK app? CDK 创建的兄弟堆栈中的竞争条件导致“找不到名为 X 的导出” - Race condition in siblings stacks created by CDK leading to "No export named X found" 对于通过 AWS CDK 创建的堡垒主机,用户数据不会自动更新 - userData is not getting updated automatically for Bastion Host created via AWS CDK
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM