Current situation:
I have an AWS API Gateway referencing some AWS Lambdas, and some Lambdas querying a DynamoDB instance.
All of the above are created and handled manually via the AWS web console. There's no cloudformation template for that.
^ I want to be able to have that locally too using 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).
I noticed that there is 0% information on how to do that. Most tutorials follow a situation where:
- i am creating a cdk from scratch locally
- or already have a cloudformation structure.
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"?
- 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?
CodePudding user response:
You must import existing resource's to CDK.
https://link.medium.com/1RbcEdal4wb
CodePudding user response:
TL;DR Use cdk import
for supported resources. Re-create the RestApi from an exported OpenApi Definition.
cdk import
The CDK has experimental import functionality to bring existing console-created resources under CDK management. The cdk import CLI command piggybacks off the related CloudFormation resource import operation.
Not all resources support the import operation. The AWS::Lambda::Function
and AWS::DynamoDB::Table
resources are supported. You must also consider secondary resources, such as the Lambda's execution role (AWS::IAM::Role
is supported for importing).
Resource importing starts with manually configuring a CDK stack that matches the existing cloud-side configuration. 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. 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. After that, the imported resources can be modified like any other CDK resource.
CDK RestApi from an exported OpenApi Definition
AWS::ApiGateway::RestApi
is not on the list of supported resources for import1. A Plan B for your Api Gateway is to export your API as an OpenAPI defnition2. Then pass the JSON as the API definition to CDK RestApi construct with the ApiDefinition.fromAsset method. This will create a new API, not import it per se.
- Although the RestApi resource itself is not supported for importing, many related
AWS::ApiGateway
resources are, such as Resource, Stage, Model and Method. - See the AWS How do I migrate API Gateway REST APIs between AWS accounts or Regions? for a non-CDK use case.