|
| 1 | +--- |
| 2 | +description: 'Setup instructions for AWS CDK Python projects' |
| 3 | +applyTo: '**/*.py' |
| 4 | +--- |
| 5 | + |
| 6 | +# AWS CDK Python Setup Instructions |
| 7 | + |
| 8 | +This instructions file provides guidance for setting up and working with AWS CDK (Cloud Development Kit) projects using Python. These instructions apply to Python files in CDK projects. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +Before starting with AWS CDK Python development, ensure you have the following installed: |
| 13 | + |
| 14 | +- **Node.js** (version 14.15.0 or later) - Required for the AWS CDK CLI |
| 15 | +- **Python** (version 3.7 or later) |
| 16 | +- **AWS CLI** - For configuring AWS credentials and managing AWS resources |
| 17 | +- **Git** - For version control |
| 18 | + |
| 19 | +## Installation Steps |
| 20 | + |
| 21 | +### 1. Install AWS CDK CLI |
| 22 | + |
| 23 | +The AWS CDK CLI is a Node.js package that provides the `cdk` command. |
| 24 | + |
| 25 | +```bash |
| 26 | +npm install -g aws-cdk |
| 27 | +``` |
| 28 | + |
| 29 | +Verify the installation: |
| 30 | + |
| 31 | +```bash |
| 32 | +cdk --version |
| 33 | +``` |
| 34 | + |
| 35 | +### 2. Configure AWS Credentials |
| 36 | + |
| 37 | +Install and configure the AWS CLI: |
| 38 | + |
| 39 | +```bash |
| 40 | +# Install AWS CLI (if not already installed) |
| 41 | +# On macOS, you can use Homebrew: |
| 42 | +brew install awscli |
| 43 | + |
| 44 | +# Configure AWS credentials |
| 45 | +aws configure |
| 46 | +``` |
| 47 | + |
| 48 | +Enter your AWS Access Key ID, Secret Access Key, default region, and output format when prompted. |
| 49 | + |
| 50 | +### 3. Create a New CDK Project |
| 51 | + |
| 52 | +Create a new directory for your CDK project and initialize it: |
| 53 | + |
| 54 | +```bash |
| 55 | +mkdir my-cdk-project |
| 56 | +cd my-cdk-project |
| 57 | +cdk init app --language python |
| 58 | +``` |
| 59 | + |
| 60 | +This creates a basic CDK app structure with: |
| 61 | +- `app.py` - Main application entry point |
| 62 | +- `my_cdk_project/` - Python package containing your CDK stacks |
| 63 | +- `requirements.txt` - Python dependencies |
| 64 | +- `cdk.json` - CDK configuration |
| 65 | + |
| 66 | +### 4. Set Up Python Virtual Environment |
| 67 | + |
| 68 | +CDK Python projects include a virtual environment. Activate it: |
| 69 | + |
| 70 | +```bash |
| 71 | +# On macOS/Linux |
| 72 | +source .venv/bin/activate |
| 73 | + |
| 74 | +# On Windows |
| 75 | +.venv\Scripts\activate |
| 76 | +``` |
| 77 | + |
| 78 | +### 5. Install Python Dependencies |
| 79 | + |
| 80 | +Install the required Python packages: |
| 81 | + |
| 82 | +```bash |
| 83 | +pip install -r requirements.txt |
| 84 | +``` |
| 85 | + |
| 86 | +The main dependencies include: |
| 87 | +- `aws-cdk-lib` - Core CDK constructs |
| 88 | +- `constructs` - Base construct library |
| 89 | + |
| 90 | +## Development Workflow |
| 91 | + |
| 92 | +### Synthesize CloudFormation Templates |
| 93 | + |
| 94 | +Generate CloudFormation templates from your CDK code: |
| 95 | + |
| 96 | +```bash |
| 97 | +cdk synth |
| 98 | +``` |
| 99 | + |
| 100 | +This creates `cdk.out/` directory with synthesized templates. |
| 101 | + |
| 102 | +### Deploy to AWS |
| 103 | + |
| 104 | +Deploy your CDK stacks to AWS: |
| 105 | + |
| 106 | +```bash |
| 107 | +cdk deploy |
| 108 | +``` |
| 109 | + |
| 110 | +Review the changes and confirm deployment when prompted. |
| 111 | + |
| 112 | +### Bootstrap (First Time Only) |
| 113 | + |
| 114 | +If deploying to a new AWS account/region, bootstrap the environment: |
| 115 | + |
| 116 | +```bash |
| 117 | +cdk bootstrap |
| 118 | +``` |
| 119 | + |
| 120 | +This sets up necessary resources like S3 buckets for storing assets. |
| 121 | + |
| 122 | +## Best Practices |
| 123 | + |
| 124 | +- Always activate the virtual environment before working |
| 125 | +- Use `cdk diff` to preview changes before deployment |
| 126 | +- Test deployments in development accounts first |
| 127 | +- Use AWS CDK constructs for infrastructure as code |
| 128 | +- Follow Python naming conventions and structure |
| 129 | + |
| 130 | +## Troubleshooting |
| 131 | + |
| 132 | +- Ensure AWS credentials are properly configured |
| 133 | +- Check that the correct AWS region is set |
| 134 | +- Verify Node.js and Python versions meet requirements |
| 135 | +- Use `cdk doctor` to diagnose common issues |
| 136 | + |
| 137 | +For detailed documentation, refer to the official AWS CDK documentation and the attached `awscdk.pdf` file. |
0 commit comments