This guide outlines the steps to migrate the KMS (Keyword Management System) from Serverless Framework to AWS CDK (Cloud Development Kit).
- AWS CLI configured with appropriate permissions
- Node.js and npm installed
- AWS CDK CLI installed (
npm install -g aws-cdk) - Serverless Framework CLI installed (for removal of old stack)
Before removing the old Serverless stack, we need to ensure that the API Gateway is retained. This step involves deploying a minimal CDK stack that imports the existing API Gateway.
./bin/deploy_bamoo.shThe serverless.yml file has been updated to include a retention policy for the API Gateway:
resources:
- ${file(./serverless-configs/${self:provider.name}-resources.yml)}
- extensions:
ApiGatewayRestApi:
DeletionPolicy: Retain
UpdateReplacePolicy: RetainUse the Serverless Framework CLI to remove the existing stack. Replace sit with your appropriate stage name if different.
npx sls remove --stage sitThis step removes all resources managed by the Serverless Framework except for the API Gateway, which we retained in step 1.
We need to set environment variables for the existing API Gateway ID and its root resource ID. These will be used in our CDK stack.
First, set the EXISTING_API_ID:
export EXISTING_API_ID=<your-api-id>Then, retrieve and set the ROOT_RESOURCE_ID:
export ROOT_RESOURCE_ID=$(aws apigateway get-rest-api --rest-api-id $EXISTING_API_ID --query 'rootResourceId' --output text)Verify that both environment variables are set correctly:
echo $EXISTING_API_ID
echo $ROOT_RESOURCE_IDWith the environment variables set, we can now deploy our full CDK stack:
cdk deploy KmsStackThis command deploys the new KMS infrastructure using CDK, integrating with the existing API Gateway.
- Verify that all resources are correctly deployed and functioning as expected.
- Update any CI/CD pipelines to use CDK commands instead of Serverless Framework commands.
- Remove any Serverless Framework specific files and configurations that are no longer needed.
- If you encounter permission issues, ensure that your AWS CLI is configured with the correct credentials and has the necessary permissions.
- If the API Gateway integration fails, double-check that the
EXISTING_API_IDandROOT_RESOURCE_IDenvironment variables are set correctly.
In case of any issues during migration:
- You can redeploy the Serverless Framework stack.
- Remove the CDK stack using:
cdk destroy KmsStack
- Ensure that the API Gateway and any critical resources are not accidentally deleted.
- This migration process is designed to minimize downtime by retaining the existing API Gateway.
- Always perform this migration in a non-production environment first to identify and resolve any potential issues.
- Keep backups of your Serverless Framework configurations and any custom resources for reference.