{"id":49411073,"url":"https://github.com/jdmacleod/aws-cdk-multiple-stacks-python","last_synced_at":"2026-04-29T00:32:19.631Z","repository":{"id":290118470,"uuid":"973424471","full_name":"jdmacleod/aws-cdk-multiple-stacks-python","owner":"jdmacleod","description":"AWS CDK Example - App with multiple stacks in Python","archived":false,"fork":false,"pushed_at":"2025-04-27T00:48:02.000Z","size":10,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-04-30T17:52:27.965Z","etag":null,"topics":["aws","cdk","example"],"latest_commit_sha":null,"homepage":"https://docs.aws.amazon.com/cdk/v2/guide/stack-how-to-create-multiple-stacks.html","language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/jdmacleod.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2025-04-27T00:44:46.000Z","updated_at":"2025-04-27T00:48:05.000Z","dependencies_parsed_at":"2025-04-27T01:39:19.009Z","dependency_job_id":null,"html_url":"https://github.com/jdmacleod/aws-cdk-multiple-stacks-python","commit_stats":null,"previous_names":["jdmacleod/aws-cdk-multiple-stacks-python"],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/jdmacleod/aws-cdk-multiple-stacks-python","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdmacleod%2Faws-cdk-multiple-stacks-python","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdmacleod%2Faws-cdk-multiple-stacks-python/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdmacleod%2Faws-cdk-multiple-stacks-python/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdmacleod%2Faws-cdk-multiple-stacks-python/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/jdmacleod","download_url":"https://codeload.github.com/jdmacleod/aws-cdk-multiple-stacks-python/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/jdmacleod%2Faws-cdk-multiple-stacks-python/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":32405901,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-04-28T19:38:08.556Z","status":"ssl_error","status_checked_at":"2026-04-28T19:37:55.688Z","response_time":56,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","cdk","example"],"created_at":"2026-04-29T00:32:19.010Z","updated_at":"2026-04-29T00:32:19.619Z","avatar_url":"https://github.com/jdmacleod.png","language":"Python","readme":"# Multiple Stacks AWS CDK Tutorial - Python\n\n\u003chttps://docs.aws.amazon.com/cdk/v2/guide/stack_how_to_create_multiple_stacks.html\u003e\n\nYou can create an AWS Cloud Development Kit (AWS CDK) application containing multiple stacks. When you deploy the AWS CDK app, each stack becomes its own AWS CloudFormation template. You can also synthesize and deploy each stack individually using the AWS CDK CLI cdk deploy command.\n\n1. Create the CDK project\n\n   - Create the project directory\n\n    ```bash\n    mkdir aws-cdk-multiple-stacks-python\n    cd aws-cdk-multiple-stacks-python\n    ```\n\n   - Initialize a new CDK project (Python)\n\n    ```bash\n    cdk init app --language python\n    ```\n\n    This generates the skeleton files for the project. If Git is detected, then the project directory is also initialized as a Git repository.\n\n    See [REFERENCE.md](./REFERENCE.md) for the skeleton README file.\n\n    Update [requirements-dev.txt](./requirements-dev.txt) to add [Astral ruff](https://docs.astral.sh/ruff/), [pytest-cov](https://pypi.org/project/pytest-cov/), and [Git pre-commit](https://pre-commit.com/)\n\n    Activate the Python virtual environment and install the requirements.\n\n    ```bash\n    source .venv/bin/activate\n    which python   # check python location\n    python -V   # check python version\n    python -m pip install -r requirements.txt\n    python -m pip install -r requirements-dev.txt\n    ```\n\n    Install the Git pre-commit hooks - see [.pre-commit-config.yaml](./.pre-commit-config.yaml)\n\n    ```bash\n    pre-commit install\n    ```\n\n    Add [pyproject.toml](./pyproject.toml) to configure `ruff`.\n\n    The project is now set up for development and testing.\n\n2. Configure the AWS environment\n\n    We will use `CDK_DEFAULT_ACCOUNT` that is provided by the `cdk` command. The region will be specified directly in the stack declarations.\n\n3. Bootstrap the AWS environment (as Admin role) (needed once for each region)\n\n    This example uses regions `us-east-1` and `us-west-1`, so we have to run the bootstrap command in each region as Administrator (if it has never been run in a given region).\n\n    The AWS `config` and `credentials` files were edited to add `admin-us-east-1` and `admin-us-west-1` entries.\n\n    ```bash\n    cdk --profile admin-us-east-1 bootstrap\n    ...\n    [15:04:22] Stack CDKToolkit has completed updating\n    ✅  Environment aws://XXXX/us-east-1 bootstrapped.\n\n    cdk --profile admin-us-west-1 bootstrap\n    ...\n    [15:06:10] CDKToolkit: skipping deployment (use --force to override)\n    ✅  Environment aws://XXXX/us-west-1 bootstrapped (no changes).\n    ```\n\n    The privileges needed for bootstrap exceed 'PowerUser'. To get this step to work, I used the Administrator role with AdministratorAccess Policy.\n\n    See\n    \u003chttps://stackoverflow.com/questions/57118082/what-iam-permissions-are-needed-to-use-cdk-deploy\u003e\n    and\n    \u003chttps://stackoverflow.com/questions/71848231/must-cdk-bootstrapping-be-done-by-a-user-with-admin-access?noredirect=1\u0026lq=1\u003e\n\n4. Build the CDK app (not needed for Python)\n5. List the CDK stacks in the app\n\n    ```bash\n    cdk list\n    MyWestCdkStack\n    MyEastCdkStack\n    ```\n\n6. Define two stacks in the app\n\n    In this example, two different stacks are defined, making use of the extended MultistackStack() class.\n\n    In [app.py](./app.py):\n\n    ```python\n        # define the App, and declare two different Stacks.\n        from aws_cdk_multiple_stacks_python.multistack_stack import MultistackStack\n\n        app = cdk.App()\n\n        MultistackStack(\n            app,\n            \"MyWestCdkStack\",\n            env=cdk.Environment(account=os.getenv(\"CDK_DEFAULT_ACCOUNT\"), region=\"us-west-1\"),\n            encrypt_bucket=False,\n        )\n\n        MultistackStack(\n            app,\n            \"MyEastCdkStack\",\n            env=cdk.Environment(account=os.getenv(\"CDK_DEFAULT_ACCOUNT\"), region=\"us-east-1\"),\n            encrypt_bucket=True,\n        )\n\n        app.synth()\n    ```\n\n7. Synthesize a CloudFormation template for each Stack (East, West)\n\n    ```bash\n    cdk synth MyEastCdkStack\n    cdk synth MyWestCdkStack\n    ```\n\n    Outputs a YAML template with the parameters for each Stack in the app.\n\n8. Deploy the East CDK stack\n\n    Deploy the app, accepting/confirming any changes if prompted.\n\n    ```bash\n    cdk deploy MyEastCdkStack\n    ...\n    MyEastCdkStack: deploying... [1/1]\n    MyEastCdkStack: creating CloudFormation changeset...\n\n    ✅  MyEastCdkStack\n\n    ✨  Deployment time: 30.88s\n\n    Stack ARN:\n    arn:aws:cloudformation:us-east-1:XXXX:stack/MyEastCdkStack/f96d3730-0b57-11f0-aa31-0e0abc6004d9\n\n    ✨  Total time: 37.02s\n    ```\n\n9. Check that the AWS S3 Bucket was created for the East Stack.\n\n    List S3 buckets.\n\n    ```bash\n    aws s3 ls\n    2025-03-27 15:03:49 cdk-hnb659fds-assets-XXXX-us-east-1\n    2025-03-27 15:03:46 cdk-hnb659fds-assets-XXXX-us-west-1\n    2025-03-26 16:38:15 cdk-hnb659fds-assets-XXXX-us-west-2\n    2025-03-27 15:08:25 myeastcdkstack-mynewbucket15ca675a-eyqstmi0zjmo\n    ```\n\n    Check the encryption for the 'East' bucket.\n\n    ```bash\n    % aws s3api get-bucket-encryption --bucket myeastcdkstack-mynewbucket15ca675a-eyqstmi0zjmo --output text\n    RULES   False\n    APPLYSERVERSIDEENCRYPTIONBYDEFAULT      aws:kms\n    ```\n\n10. Deploy the West CDK stack\n\n    Deploy the app, accepting/confirming any changes if prompted.\n\n    ```bash\n    cdk deploy MyWestCdkStack\n    ...\n    MyWestCdkStack: deploying... [1/1]\n    MyWestCdkStack: creating CloudFormation changeset...\n\n    ✅  MyWestCdkStack\n\n    ✨  Deployment time: 27.08s\n\n    Stack ARN:\n    arn:aws:cloudformation:us-west-1:XXXX:stack/MyWestCdkStack/fbfcf7e0-0b59-11f0-8712-067194afe31d\n\n    ✨  Total time: 33.43s\n    ```\n\n11. Check that the AWS S3 Bucket was created for the West Stack.\n\n    List S3 buckets.\n\n    ```bash\n    aws s3 ls\n    2025-03-27 15:03:49 cdk-hnb659fds-assets-XXXX-us-east-1\n    2025-03-27 15:03:46 cdk-hnb659fds-assets-XXXX-us-west-1\n    2025-03-26 16:38:15 cdk-hnb659fds-assets-XXXX-us-west-2\n    2025-03-27 15:08:25 myeastcdkstack-mynewbucket15ca675a-eyqstmi0zjmo\n    2025-03-27 15:22:47 mywestcdkstack-mynewbucket15ca675a-ti2uqdiqgykw\n    ```\n\n    Check the encryption for the 'West' bucket.\n\n    ```bash\n    % aws s3api get-bucket-encryption --bucket mywestcdkstack-mynewbucket15ca675a-ti2uqdiqgykw --output text\n    RULES   False\n    APPLYSERVERSIDEENCRYPTIONBYDEFAULT      AES256\n    ```\n\n12. Clean up the Stacks from East and West regions.\n\n    Destroy the East Stack.\n\n    ```bash\n        cdk destroy MyEastCdkStack\n        Are you sure you want to delete: MyEastCdkStack (y/n)? y\n        MyEastCdkStack: destroying... [1/1]\n\n        ✅  MyEastCdkStack: destroyed\n    ```\n\n    Destroy the West Stack.\n\n    ```bash\n        cdk destroy MyWestCdkStack\n        Are you sure you want to delete: MyWestCdkStack (y/n)? y\n        MyWestCdkStack: destroying... [1/1]\n\n        ✅  MyWestCdkStack: destroyed\n    ```\n\n    Confirm the S3 buckets for East and West Stacks have been removed. The CDK bootstrap bucket for each region will remain, unless explicitly removed, possibly using the AWS Console.\n\n    ```bash\n        aws s3 ls\n        2025-03-27 15:03:49 cdk-hnb659fds-assets-XXXX-us-east-1\n        2025-03-27 15:03:46 cdk-hnb659fds-assets-XXXX-us-west-1\n        2025-03-26 16:38:15 cdk-hnb659fds-assets-XXXX-us-west-2\n    ```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdmacleod%2Faws-cdk-multiple-stacks-python","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fjdmacleod%2Faws-cdk-multiple-stacks-python","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fjdmacleod%2Faws-cdk-multiple-stacks-python/lists"}