aws-cdk-python-setup

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

AWS 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 --version
bash
npm install -g aws-cdk
cdk --version

2. Configure AWS Credentials

2. 配置 AWS 凭证

bash
undefined
bash
undefined

Install 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 python
Your project will include:
  • app.py
    — Main application entry point
  • my_cdk_project/
    — CDK stack definitions
  • requirements.txt
    — Python dependencies
  • cdk.json
    — Configuration file
bash
mkdir my-cdk-project
cd my-cdk-project
cdk init app --language python
您的项目将包含:
  • app.py
    — 主应用入口文件
  • my_cdk_project/
    — CDK堆栈定义
  • requirements.txt
    — Python依赖项
  • cdk.json
    — 配置文件

4. Set Up Python Virtual Environment

4. 设置 Python 虚拟环境

bash
undefined
bash
undefined

macOS/Linux

macOS/Linux

source .venv/bin/activate
source .venv/bin/activate

Windows

Windows

.venv\Scripts\activate
undefined
.venv\Scripts\activate
undefined

5. Install Python Dependencies

5. 安装 Python 依赖项

bash
pip install -r requirements.txt
Primary dependencies:
  • aws-cdk-lib
    — Core CDK constructs
  • constructs
    — Base construct library

bash
pip install -r requirements.txt
主要依赖项:
  • aws-cdk-lib
    — 核心CDK构造库
  • constructs
    — 基础构造库

Development Workflow

开发工作流

Synthesize CloudFormation Templates

生成 CloudFormation 模板

bash
cdk synth
Generates
cdk.out/
containing CloudFormation templates.
bash
cdk synth
生成包含CloudFormation模板的
cdk.out/
目录。

Deploy Stacks to AWS

将堆栈部署到 AWS

bash
cdk deploy
Reviews and confirms deployment to the configured AWS account.
bash
cdk deploy
查看并确认部署到已配置的AWS账户。

Bootstrap (First Deployment Only)

引导操作(仅首次部署需要)

bash
cdk bootstrap
Prepares environment resources like S3 buckets for asset storage.

bash
cdk bootstrap
准备环境资源,如用于存储资产的S3存储桶。

Best Practices

最佳实践

  • Always activate the virtual environment before working.
  • Run
    cdk diff
    before deployment to preview changes.
  • Use development accounts for testing.
  • Follow Pythonic naming and directory conventions.
  • Keep
    requirements.txt
    pinned for consistent builds.

  • 工作前始终激活虚拟环境。
  • 部署前运行
    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
    cdk doctor
    to diagnose environment issues.
如果遇到问题,请检查:
  • AWS凭证是否配置正确。
  • 默认区域是否设置妥当。
  • Node.js和Python版本是否满足最低要求。
  • 运行
    cdk doctor
    诊断环境问题。