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


Current situation: I have an API Gateway referencing some Lambdas, and some Lambdas querying a DynamoDB instance. All created and handled manually via the AWS web console. There's no cloudformation template.

^ 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 1, i am creating a cdk from scratch, 2. or already have a cloudformation structure.

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? I don't know where to start doing that and there is totally no information about that anywhere

Please help me figure our the best proper way to do that.

2 Answers

The best way to go about this would be to use the AWS CDK to create a CloudFormation template that will replicate the existing resources in your AWS environment. This template can then be used to create a local version of the environment using the AWS CDK.

To create the CloudFormation template, you can use the AWS CDK to generate the necessary code from your existing resources. You can do this by running the following commands in the AWS CLI:

To generate the CloudFormation template for an API Gateway:

aws cloudformation export-stack-template --stack-name <stack_name> --output text > <output_file_name>

To generate the CloudFormation template for a Lambda Function:

aws cloudformation export-stack-template --stack-name <lambda_function_name> --output text > <output_file_name>

To generate the CloudFormation template for a DynamoDB instance:

aws cloudformation export-stack-template --stack-name <dynamodb_table_name> --output text > <output_file_name>

Once you have the CloudFormation templates for each of your resources, you can then use

profile picture
answered a year ago

thanks for you fast reply !

sudo aws cloudformation export-stack-template --stack-name 9nc4das --output-text apigw.txt 
usage: aws [options] <command> <subcommand> [<subcommand> ...] [parameters]
To see help text, you can run:
  aws help
  aws <command> help
  aws <command> <subcommand> help

aws: error: argument operation: Invalid choice, valid choices are:

activate-type                            | batch-describe-type-configurations      
cancel-update-stack                      | continue-update-rollback                
create-change-set                        | create-stack                            
create-stack-instances                   | create-stack-set                        
deactivate-type                          | delete-change-set                       
delete-stack                             | delete-stack-instances                  
delete-stack-set                         | deregister-type                         
describe-account-limits                  | describe-change-set                     
describe-change-set-hooks                | describe-publisher                      
describe-stack-drift-detection-status    | describe-stack-events                   
describe-stack-instance                  | describe-stack-resource                 
describe-stack-resource-drifts           | describe-stack-resources                
describe-stack-set                       | describe-stack-set-operation            
describe-stacks                          | describe-type                           
describe-type-registration               | detect-stack-drift                      
detect-stack-resource-drift              | detect-stack-set-drift                  
estimate-template-cost                   | execute-change-set                      
get-stack-policy                         | get-template                            
get-template-summary                     | import-stacks-to-stack-set              
list-change-sets                         | list-exports                            
list-imports                             | list-stack-instances                    
list-stack-resources                     | list-stack-set-operation-results        
list-stack-set-operations                | list-stack-sets                         
list-stacks                              | list-type-registrations                 
list-type-versions                       | list-types                              
publish-type                             | record-handler-progress                 
register-publisher                       | register-type                           
rollback-stack                           | set-stack-policy                        
set-type-configuration                   | set-type-default-version                
signal-resource                          | stop-stack-set-operation                
test-type                                | update-stack                            
update-stack-instances                   | update-stack-set                        
update-termination-protection            | validate-template                       
package                                  | deploy                                  
wait                                     | help                                    

I made sure im using the last version following aws documentation:

usre@a-len:~/myapi$ sudo aws --version
aws-cli/2.9.20 Python/3.9.11 Linux/5.15.0-58-generic exe/x86_64.ubuntu.22 prompt/off
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions