This is a CDK Python project for Amazon MemoryDB Multi-Region cluster.
The cdk.json
file tells the CDK Toolkit how to execute your app.
This project is set up like a standard Python project. The initialization
process also creates a virtualenv within this project, stored under the .venv
directory. To create the virtualenv it assumes that there is a python3
(or python
for Windows) executable in your path with access to the venv
package. If for any reason the automatic creation of the virtualenv fails,
you can create the virtualenv manually.
To manually create a virtualenv on MacOS and Linux:
$ python3 -m venv .venv
After the init process completes and the virtualenv is created, you can use the following step to activate your virtualenv.
$ source .venv/bin/activate
If you are a Windows platform, you would activate the virtualenv like this:
% .venv\Scripts\activate.bat
Once the virtualenv is activated, you can install the required dependencies.
(.venv) $ pip install -r requirements.txt
To add additional dependencies, for example other CDK libraries, just add
them to your setup.py
file and rerun the pip install -r requirements.txt
command.
Before synthesizing the CloudFormation, you should set approperly the cdk context configuration file, cdk.context.json
.
For example:
{ "memorydb_mlti_region_cluster": { "node_type": "db.r7g.xlarge", "engine": "Valkey", "engine_version": "7.3", "multi_region_cluster_name_suffix": "mrc-demo", "num_shards": 3 } }
At this point you can now synthesize the CloudFormation template for this code.
(.venv) $ export CDK_DEFAULT_ACCOUNT=$(aws sts get-caller-identity --query Account --output text)
(.venv) $ export CDK_DEFAULT_REGION=$(aws configure get region)
(.venv) $ cdk synth --all
Now we will be able to deploy all the CDK stacks at once like this:
(.venv) $ cdk deploy --require-approval never --all
Create the Regional cluster within your Multi-Region cluster with the appropriate cluster settings.
(.venv) $ MULTI_REGION_CLUSTER_NAME=$(aws cloudformation describe-stacks --stack-name MemoryDBMultiRegionClusterStack \ | jq -r '.Stacks[0].Outputs | map(select(.OutputKey == "MemDBMultiRegionClusterName")) | .[0].OutputValue') (.venv) cd ../valkey-cluster (.venv) cdk deploy \ -c memorydb_multi_region_cluster_name=${MULTI_REGION_CLUSTER_NAME} \ --require-approval never --all
You can add a second Regional cluster to your Multi-Region cluster after the Multi-Region cluster and the first Regional cluster are set up.
First, change the default region of your aws account to a second region (e.g., us-west-2
) where you deploy a second memorydb cluster like this:
(.venv) aws configure set region us-west-2
Then deploy the cdk stacks for the second memorydb cluster to the second region.
(.venv) cdk deploy \ -c memorydb_multi_region_cluster_name=${MULTI_REGION_CLUSTER_NAME} \ --require-approval never --all
The process of deleting a memorydb multi-region cluster involves reversing the steps taken during its creation. This cleanup procedure consists of three main steps:
To delete the cluster in the second region:
- Navigate to the
valkey-cluster
directory. - Verify that the AWS CLI is configured for the
us-west-2
region. - Execute the CDK destroy command:
(.venv) basename $(pwd) valkey-cluster (.venv) aws configure get region us-west-2 (.venv) cdk destroy --force --all
For deleting the cluster in the first region:
- Ensure you're still in the
valkey-cluster
directory. - Set the AWS CLI region to your default CDK region.
- Run the CDK destroy command:
(.venv) basename $(pwd) valkey-cluster (.venv) aws configure set region ${CDK_DEFAULT_REGION} (.venv) cdk destroy --force --all
After removing both regional clusters, proceed to delete the multi-region cluster setup.
- Navigate to the
multi-region-cluster
directory. - Execute the CDK destroy command:
(.venv) cd ../multi-region-cluster (.venv) basename $(pwd) multi-region-cluster (.venv) cdk destroy --force --all
cdk ls
list all stacks in the appcdk synth
emits the synthesized CloudFormation templatecdk deploy
deploy this stack to your default AWS account/regioncdk diff
compare deployed stack with current statecdk docs
open CDK documentation
Enjoy!