aws-cdk-python-setup
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAWS CDK Python Setup Instructions
AWS CDK Python 设置说明
This skill provides setup guidance for working with AWS CDK (Cloud Development Kit) projects using Python.
本指南提供了使用Python进行**AWS CDK (Cloud Development Kit)**项目开发的设置指导。
Prerequisites
先决条件
Before starting, ensure the following tools are installed:
- Node.js ≥ 14.15.0 — Required for the AWS CDK CLI
- Python ≥ 3.7 — Used for writing CDK code
- AWS CLI — Manages credentials and resources
- Git — Version control and project management
开始之前,请确保已安装以下工具:
- Node.js ≥ 14.15.0 — AWS CDK CLI 的必备工具
- Python ≥ 3.7 — 用于编写CDK代码
- AWS CLI — 管理凭证和资源
- Git — 版本控制和项目管理
Installation Steps
安装步骤
1. Install AWS CDK CLI
1. 安装 AWS CDK CLI
bash
npm install -g aws-cdk
cdk --versionbash
npm install -g aws-cdk
cdk --version2. Configure AWS Credentials
2. 配置 AWS 凭证
bash
undefinedbash
undefinedInstall AWS CLI (if not installed)
安装 AWS CLI(如果尚未安装)
brew install awscli
brew install awscli
Configure credentials
配置凭证
aws configure
Enter your AWS Access Key, Secret Access Key, default region, and output format when prompted.aws configure
当系统提示时,输入您的AWS访问密钥、秘密访问密钥、默认区域和输出格式。3. Create a New CDK Project
3. 创建新的 CDK 项目
bash
mkdir my-cdk-project
cd my-cdk-project
cdk init app --language pythonYour project will include:
- — Main application entry point
app.py - — CDK stack definitions
my_cdk_project/ - — Python dependencies
requirements.txt - — Configuration file
cdk.json
bash
mkdir my-cdk-project
cd my-cdk-project
cdk init app --language python您的项目将包含:
- — 主应用入口文件
app.py - — CDK堆栈定义
my_cdk_project/ - — Python依赖项
requirements.txt - — 配置文件
cdk.json
4. Set Up Python Virtual Environment
4. 设置 Python 虚拟环境
bash
undefinedbash
undefinedmacOS/Linux
macOS/Linux
source .venv/bin/activate
source .venv/bin/activate
Windows
Windows
.venv\Scripts\activate
undefined.venv\Scripts\activate
undefined5. Install Python Dependencies
5. 安装 Python 依赖项
bash
pip install -r requirements.txtPrimary dependencies:
- — Core CDK constructs
aws-cdk-lib - — Base construct library
constructs
bash
pip install -r requirements.txt主要依赖项:
- — 核心CDK构造库
aws-cdk-lib - — 基础构造库
constructs
Development Workflow
开发工作流
Synthesize CloudFormation Templates
生成 CloudFormation 模板
bash
cdk synthGenerates containing CloudFormation templates.
cdk.out/bash
cdk synth生成包含CloudFormation模板的 目录。
cdk.out/Deploy Stacks to AWS
将堆栈部署到 AWS
bash
cdk deployReviews and confirms deployment to the configured AWS account.
bash
cdk deploy查看并确认部署到已配置的AWS账户。
Bootstrap (First Deployment Only)
引导操作(仅首次部署需要)
bash
cdk bootstrapPrepares environment resources like S3 buckets for asset storage.
bash
cdk bootstrap准备环境资源,如用于存储资产的S3存储桶。
Best Practices
最佳实践
- Always activate the virtual environment before working.
- Run before deployment to preview changes.
cdk diff - Use development accounts for testing.
- Follow Pythonic naming and directory conventions.
- Keep pinned for consistent builds.
requirements.txt
- 工作前始终激活虚拟环境。
- 部署前运行 预览变更。
cdk diff - 使用开发账户进行测试。
- 遵循Python风格的命名和目录约定。
- 固定 中的依赖版本以确保构建一致性。
requirements.txt
Troubleshooting Tips
故障排除提示
If issues occur, check:
- AWS credentials are correctly configured.
- Default region is set properly.
- Node.js and Python versions meet minimum requirements.
- Run to diagnose environment issues.
cdk doctor
如果遇到问题,请检查:
- AWS凭证是否配置正确。
- 默认区域是否设置妥当。
- Node.js和Python版本是否满足最低要求。
- 运行 诊断环境问题。
cdk doctor