// MetaData :repo-name: CONFIGURABLE-DYNAMIC-CDK-RESOURCE-PROVISIONING-PYTHON :application-name: CONFIGURABLE-DYNAMIC-CDK-RESOURCE-PROVISIONING-PYTHON = How to configure a CDK App and dynamically provision resource quantities in Python == Overview This repository contains source code to configure a CDK app via environment variables. The repo will show how you can, through environment variables, configure the CDK app to provision resources dynamically while keeping the state for the app without having to add extra code when increasing a resource count. == Considerations . How can you configure the CDK app to use environment variables to set the CDK app name? . How can you configure the CDK app to use environment variables to set the region and account? . How can you configure the CDK app to use environment variables to set the resource names and quantity using a list? . How can you configure the CDK app to use environment variables to set the resource name & quantity with env? . How can you configure the CDK app to use environment variables to keep state and/while allowing for dynamic configuration? == Solution The solution uses environment variables, `export =`, to configure the app during a CDK `deploy`, or `destroy`. If the environment variables are not set, defaults are used. The code in `{repo-name}` directory and `app.py` in the root directory use the environment variables to configure the CDK app. The sample code deploys the following: . S3 bucket(s) . SQS queue(s) . SNS topic(s) Depending on the values set in the environment variables, the CDK app will deploy the resources dynamically. [IMPORTANT] ==== The QUEUE_COUNT determines the SNS TOPIC as well. ==== [CAUTION] ==== Charges do apply. Do not set the Counts to a high number. The CDK app will deploy the resources dynamically. ==== --- . How can I configure the CDK app to use environment variables to set the CDK app name? .. Make sure you set the default in the `.py` files .. Get the environment variable value, see source code for how to get the environment variable value .. Create an environment variable, source code uses `applicationName` [source, bash] export applicationName= . How can I configure the CDK app to use environment variables to set the region and account? .. Make sure you set the defaults in the `.py` files .. Get the environment variable value, see source code for how to get the environment variable value .. If you don't set the region and account, the CDK defaults are used .. Create the environment variables, source code uses `DEPLOY_REGION` and `DEPLOY_ACCOUNT` [source, bash] export DEPLOY_REGION export DEPLOY_ACCOUNT= . How can I configure the CDK app to use environment variables to set the resource names based on a list? .. For this code, we use the following list: ... S3 bucket name(s), a string of bucket names separated by a comma [NOTE] * ***There is an intentional error introduced on `Line 39` of the `{application-name}.py` file in the `{application-name}` directory for the default bucket names . You must set this value to for example ',' to deploy the app. .. Make sure you set the defaults in the `{repo-name}.py` file in the {repo-name} directory .. `BUCKET_NAMES` is a string of bucket names separated by a comma. The code will split the string and create a list of bucket names. .. If you set the string to `bucket1,bucket2,bucket3`, the code will create 3 buckets with the names `bucket1`, `bucket2`, and `bucket3` .. If you remove a bucket name from the string and deploy, the bucket name that was removed from the string will be deleted .. If you add a bucket name to the string and deploy, the bucket name that was added to the string will be created .. Get the environment variable values for each resource name, see source code for how to get the environment variable value .. Create the environment variables, source code uses `BUCKET_NAMES` [source, bash] export BUCKET_NAMES= . How can I configure the CDK app to use environment variables to set the base name and resource quantity? .. For this code, we use the following base name and resource quantity: ... SQS queue name, a string of queue names separated by a comma .. Create an environment variable count for resources you want to deploy based on a base name + some resource countID .. In this solution if you set the QUEUE_NAME to `queue` and the QUEUE_COUNT to `3`, the code will create 3 queues with the names `queue1`, `queue2`, and `queue3` [source, bash] export NUMBER_OF_QUEUES= export QUEUE_NAME= . How can I configure the CDK app to use environment variables to keep state allowing for dynamic configuration? You need to make sure your resources are unique every time a new stack is deployed. In the code, a combination of the app name, region, and hashed account number are used to create a unique name for each resource. This allows you to deploy the same stack multiple times and not have resource name conflicts and keeping the state. [WARNING] ==== . NEVER USE THE REAL ACCOUNT CODE WHEN CREATING RESOURCES. ALWAYS USE A HASHED ACCOUNT NUMBER. USE A PROPER HASHING ALGORITHM TO CREATE A UNIQUE HASHED ACCOUNT NUMBER. . REPLACE THE `hash` FUNCTION WITH A PROPER HASHING ALGORITHM. ==== == Clone the repo [source,bash] git clone https://github.com/aws-samples/configurable-dynamic-cdk-resource-provisioning-python.git == Prerequisites . Install nodejs . Install python3 . Install TypeScript . Install CDK Toolkit globally pass:[(If you don't have it installed already)] [source,bash] npm install -g aws-cdk . Bootstrap account/region pass:[(If you have not bootstrapped it already)] [source,bash] cdk bootstrap aws:/// OR --profile == Useful Commands 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` 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 once the init process completes. To manually create a virtualenv on MacOS and Linux: [source,bash] $ 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,bash] $ source .venv/bin/activate If you are a Windows platform, you would activate the virtualenv like this: [source,bash] % .venv\Scripts\activate.bat Once the virtualenv is activated, you can install the required dependencies. [source,bash] $ pip install -r requirements.txt At this point you can now synthesize the CloudFormation template for this code. [source,bash] $ cdk synth You can now begin exploring the source code, contained in the hello directory. There is also a very trivial test included that can be run like this: [source,bash] pytest To add additional dependencies, for example other CDK libraries, just add to your requirements.txt file and rerun the `pip install -r requirements.txt` command. === Other useful commands * `cdk ls` list all stacks in the app * `cdk synth` emits the synthesized CloudFormation template * `cdk deploy` deploy this stack to your default AWS account/region * `cdk diff` compare deployed stack with current state * `cdk docs` open CDK documentation == Security See link:./CONTRIBUTING.md#security-issue-notifications[Security Issue Notifications] for more information. == Contributing Please refer to our link:./CONTRIBUTING.md[Contributing] Guideline before reporting bugs or feature requests. == License See the link:./LICENSE[LICENSE] file for our project's licensing.