awslabs.cdk-mcp-server 0.0.10417__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- awslabs/__init__.py +2 -0
- awslabs/cdk_mcp_server/__init__.py +8 -0
- awslabs/cdk_mcp_server/core/__init__.py +1 -0
- awslabs/cdk_mcp_server/core/resources.py +271 -0
- awslabs/cdk_mcp_server/core/search_utils.py +182 -0
- awslabs/cdk_mcp_server/core/server.py +74 -0
- awslabs/cdk_mcp_server/core/tools.py +324 -0
- awslabs/cdk_mcp_server/data/__init__.py +1 -0
- awslabs/cdk_mcp_server/data/cdk_nag_parser.py +331 -0
- awslabs/cdk_mcp_server/data/construct_descriptions.py +32 -0
- awslabs/cdk_mcp_server/data/genai_cdk_loader.py +423 -0
- awslabs/cdk_mcp_server/data/lambda_powertools_loader.py +48 -0
- awslabs/cdk_mcp_server/data/schema_generator.py +666 -0
- awslabs/cdk_mcp_server/data/solutions_constructs_parser.py +782 -0
- awslabs/cdk_mcp_server/server.py +7 -0
- awslabs/cdk_mcp_server/static/CDK_GENERAL_GUIDANCE.md +232 -0
- awslabs/cdk_mcp_server/static/CDK_NAG_GUIDANCE.md +192 -0
- awslabs/cdk_mcp_server/static/__init__.py +5 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/actiongroups.md +137 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/alias.md +39 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/collaboration.md +91 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/creation.md +149 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/custom_orchestration.md +74 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/overview.md +78 -0
- awslabs/cdk_mcp_server/static/bedrock/agent/prompt_override.md +70 -0
- awslabs/cdk_mcp_server/static/bedrock/bedrockguardrails.md +188 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/chunking.md +137 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/datasources.md +225 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/kendra.md +81 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/overview.md +116 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/parsing.md +36 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/transformation.md +30 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/aurora.md +185 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/creation.md +80 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/opensearch.md +56 -0
- awslabs/cdk_mcp_server/static/bedrock/knowledgebases/vector/pinecone.md +66 -0
- awslabs/cdk_mcp_server/static/bedrock/profiles.md +153 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/actiongroups.md +137 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/alias.md +39 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/collaboration.md +91 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/creation.md +149 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/custom_orchestration.md +74 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/overview.md +78 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/agent/prompt_override.md +70 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/bedrockguardrails.md +188 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/chunking.md +137 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/datasources.md +225 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/kendra.md +81 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/overview.md +116 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/parsing.md +36 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/transformation.md +30 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/aurora.md +185 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/creation.md +80 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/opensearch.md +56 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/knowledgebases/vector/pinecone.md +66 -0
- awslabs/cdk_mcp_server/static/genai_cdk/bedrock/profiles.md +153 -0
- awslabs/cdk_mcp_server/static/genai_cdk/opensearch-vectorindex/overview.md +135 -0
- awslabs/cdk_mcp_server/static/genai_cdk/opensearchserverless/overview.md +17 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/bedrock.md +127 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/cdk.md +99 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/dependencies.md +45 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/index.md +36 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/insights.md +95 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/logging.md +43 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/metrics.md +93 -0
- awslabs/cdk_mcp_server/static/lambda_powertools/tracing.md +63 -0
- awslabs/cdk_mcp_server/static/opensearch-vectorindex/overview.md +135 -0
- awslabs/cdk_mcp_server/static/opensearchserverless/overview.md +17 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/METADATA +14 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/RECORD +72 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/WHEEL +4 -0
- awslabs_cdk_mcp_server-0.0.10417.dist-info/entry_points.txt +2 -0
|
@@ -0,0 +1,232 @@
|
|
|
1
|
+
# AWS CDK Best Practices Guide
|
|
2
|
+
|
|
3
|
+
This guide provides essential best practices for AWS CDK development, focusing on when to use specific constructs and tools.
|
|
4
|
+
|
|
5
|
+
## Getting Started with CDK
|
|
6
|
+
|
|
7
|
+
Always initialize CDK projects properly using the CDK CLI:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
# For TypeScript projects
|
|
11
|
+
cdk init app --language typescript
|
|
12
|
+
|
|
13
|
+
# For Python projects
|
|
14
|
+
cdk init app --language python
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
Proper initialization ensures:
|
|
18
|
+
- Consistent project structure
|
|
19
|
+
- Correct dependency setup
|
|
20
|
+
- Appropriate tsconfig/package.json configuration
|
|
21
|
+
- Necessary boilerplate files
|
|
22
|
+
|
|
23
|
+
This foundation helps avoid common issues and ensures compatibility with AWS CDK best practices.
|
|
24
|
+
|
|
25
|
+
## Development Workflow
|
|
26
|
+
|
|
27
|
+
When developing CDK applications, use these commands for an efficient workflow:
|
|
28
|
+
|
|
29
|
+
```bash
|
|
30
|
+
# Synthesize CloudFormation templates (recommended for validation)
|
|
31
|
+
cdk synth
|
|
32
|
+
|
|
33
|
+
# Deploy your CDK application
|
|
34
|
+
cdk deploy
|
|
35
|
+
|
|
36
|
+
# Compare deployed stack with current state
|
|
37
|
+
cdk diff
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
**Important**: Prefer `cdk synth` over `npm run build` or `tsc` for TypeScript projects. The `cdk synth` command:
|
|
41
|
+
|
|
42
|
+
- Automatically compiles TypeScript code when needed
|
|
43
|
+
- Validates CDK constructs and catches potential deployment issues
|
|
44
|
+
- Generates CloudFormation templates for inspection
|
|
45
|
+
- Provides more informative error messages for debugging
|
|
46
|
+
|
|
47
|
+
## Decision Flow for CDK Implementation
|
|
48
|
+
|
|
49
|
+
When implementing AWS infrastructure with CDK, consider these complementary approaches:
|
|
50
|
+
|
|
51
|
+
1. **For Common Architecture Patterns: AWS Solutions Constructs**
|
|
52
|
+
- Use the `GetAwsSolutionsConstructPattern` tool to search for patterns that match your use case
|
|
53
|
+
- Example: `GetAwsSolutionsConstructPattern(services=["lambda", "dynamodb"])`
|
|
54
|
+
- AWS Solutions Constructs implement AWS best practices by default
|
|
55
|
+
- For complete documentation: `aws-solutions-constructs://{pattern_name}`
|
|
56
|
+
- Ideal for REST APIs, serverless backends, data processing pipelines, etc.
|
|
57
|
+
|
|
58
|
+
2. **For GenAI/AI/ML Use Cases: GenAI CDK Constructs**
|
|
59
|
+
- Use the `SearchGenAICDKConstructs` tool for specialized AI/ML constructs
|
|
60
|
+
- These simplify implementation of Bedrock, SageMaker, and other AI services
|
|
61
|
+
- Perfect for agents, knowledge bases, vector stores, and other GenAI components
|
|
62
|
+
|
|
63
|
+
**Installation:**
|
|
64
|
+
```typescript
|
|
65
|
+
// TypeScript
|
|
66
|
+
// Create or use an existing CDK application
|
|
67
|
+
cdk init app --language typescript
|
|
68
|
+
// Install the package
|
|
69
|
+
npm install @cdklabs/generative-ai-cdk-constructs
|
|
70
|
+
// Import the library
|
|
71
|
+
import * as genai from '@cdklabs/generative-ai-cdk-constructs';
|
|
72
|
+
```
|
|
73
|
+
|
|
74
|
+
```python
|
|
75
|
+
# Python
|
|
76
|
+
# Create or use an existing CDK application
|
|
77
|
+
cdk init app --language python
|
|
78
|
+
# Install the package
|
|
79
|
+
pip install cdklabs.generative-ai-cdk-constructs
|
|
80
|
+
# Import the library
|
|
81
|
+
import cdklabs.generative_ai_cdk_constructs
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
3. **For All Projects: Apply CDK Nag**
|
|
85
|
+
- Always apply CDK Nag to ensure security best practices
|
|
86
|
+
- Use the `ExplainCDKNagRule` tool to understand specific rules
|
|
87
|
+
|
|
88
|
+
4. **For Custom Requirements: Custom Implementation**
|
|
89
|
+
- Create custom CDK code when no suitable constructs exist
|
|
90
|
+
- Follow AWS Well-Architected best practices
|
|
91
|
+
|
|
92
|
+
> **IMPORTANT**: AWS Solutions Constructs and GenAI CDK Constructs are complementary and can be used together in the same project. For example, you might use GenAI CDK Constructs for Bedrock components and AWS Solutions Constructs for the REST API and database layers of your application.
|
|
93
|
+
|
|
94
|
+
## Key Principles
|
|
95
|
+
|
|
96
|
+
- **Security First**: Always implement security best practices by default
|
|
97
|
+
- **Cost Optimization**: Design resources to minimize costs while meeting requirements
|
|
98
|
+
- **Operational Excellence**: Implement proper monitoring, logging, and observability
|
|
99
|
+
- **Serverless-First**: Prefer serverless services when possible
|
|
100
|
+
- **Infrastructure as Code**: Use CDK to define all infrastructure
|
|
101
|
+
- **Use Vetted Patterns**: Prefer AWS Solutions Constructs over custom implementations
|
|
102
|
+
- **Regional Awareness**: Consider regional availability and constraints for services
|
|
103
|
+
|
|
104
|
+
## Amazon Bedrock Cross-Region Inference Profiles
|
|
105
|
+
|
|
106
|
+
When working with Amazon Bedrock foundation models, many models (including Claude models, Meta Llama models, and Amazon's own Nova models) require the use of inference profiles rather than direct on-demand usage in specific regions. Failing to use inference profiles can result in errors like:
|
|
107
|
+
|
|
108
|
+
```
|
|
109
|
+
Invocation of model ID anthropic.claude-3-7-sonnet-20250219-v1:0 with on-demand throughput isn't supported.
|
|
110
|
+
Retry your request with the ID or ARN of an inference profile that contains this model.
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
### Using Cross-Region Inference Profiles
|
|
114
|
+
|
|
115
|
+
To properly configure Bedrock models with cross-region inference profiles:
|
|
116
|
+
|
|
117
|
+
#### TypeScript
|
|
118
|
+
|
|
119
|
+
```typescript
|
|
120
|
+
import { bedrock } from '@cdklabs/generative-ai-cdk-constructs';
|
|
121
|
+
|
|
122
|
+
// Create a cross-region inference profile for Claude
|
|
123
|
+
const claudeInferenceProfile = bedrock.CrossRegionInferenceProfile.fromConfig({
|
|
124
|
+
// Choose the appropriate region:
|
|
125
|
+
// US (default) - bedrock.CrossRegionInferenceProfileRegion.US
|
|
126
|
+
// EU - bedrock.CrossRegionInferenceProfileRegion.EU
|
|
127
|
+
// APAC - bedrock.CrossRegionInferenceProfileRegion.APAC
|
|
128
|
+
geoRegion: bedrock.CrossRegionInferenceProfileRegion.US,
|
|
129
|
+
model: bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_7_SONNET_V1_0
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
// Use the inference profile with your agent or other Bedrock resources
|
|
133
|
+
const agent = new bedrock.Agent(this, 'MyAgent', {
|
|
134
|
+
// Use the inference profile instead of directly using the foundation model
|
|
135
|
+
foundationModel: claudeInferenceProfile,
|
|
136
|
+
// Other agent configuration...
|
|
137
|
+
});
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
#### Python
|
|
141
|
+
|
|
142
|
+
```python
|
|
143
|
+
from cdklabs.generative_ai_cdk_constructs import bedrock
|
|
144
|
+
|
|
145
|
+
# Create a cross-region inference profile for Claude
|
|
146
|
+
claude_inference_profile = bedrock.CrossRegionInferenceProfile.from_config(
|
|
147
|
+
# Choose the appropriate region:
|
|
148
|
+
# US (default) - bedrock.CrossRegionInferenceProfileRegion.US
|
|
149
|
+
# EU - bedrock.CrossRegionInferenceProfileRegion.EU
|
|
150
|
+
# APAC - bedrock.CrossRegionInferenceProfileRegion.APAC
|
|
151
|
+
geo_region=bedrock.CrossRegionInferenceProfileRegion.US,
|
|
152
|
+
model=bedrock.BedrockFoundationModel.ANTHROPIC_CLAUDE_3_7_SONNET_V1_0
|
|
153
|
+
)
|
|
154
|
+
|
|
155
|
+
# Use the inference profile with your agent or other Bedrock resources
|
|
156
|
+
agent = bedrock.Agent(self, "MyAgent",
|
|
157
|
+
# Use the inference profile instead of directly using the foundation model
|
|
158
|
+
foundation_model=claude_inference_profile,
|
|
159
|
+
# Other agent configuration...
|
|
160
|
+
)
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
### Regional Considerations
|
|
164
|
+
|
|
165
|
+
- **Model Availability**: Not all foundation models are available in all regions
|
|
166
|
+
- **Inference Profile Requirements**: Some models require inference profiles in specific regions
|
|
167
|
+
- **Performance**: Choose the region closest to your users for optimal latency
|
|
168
|
+
- **Compliance**: Consider data residency requirements when selecting regions
|
|
169
|
+
|
|
170
|
+
Always check the [Amazon Bedrock documentation](https://docs.aws.amazon.com/bedrock/latest/userguide/what-is-bedrock.html) for the latest information on model availability and regional constraints.
|
|
171
|
+
|
|
172
|
+
## AWS Solutions Constructs
|
|
173
|
+
|
|
174
|
+
AWS Solutions Constructs are vetted architecture patterns that combine multiple AWS services to solve common use cases following AWS Well-Architected best practices.
|
|
175
|
+
|
|
176
|
+
**Key benefits:**
|
|
177
|
+
- Accelerated Development: Implement common patterns without boilerplate code
|
|
178
|
+
- Best Practices Built-in: Security, reliability, and performance best practices
|
|
179
|
+
- Reduced Complexity: Simplified interfaces for multi-service architectures
|
|
180
|
+
- Well-Architected: Patterns follow AWS Well-Architected Framework principles
|
|
181
|
+
|
|
182
|
+
**When to use Solutions Constructs:**
|
|
183
|
+
- Implementing common architecture patterns (e.g., API + Lambda + DynamoDB)
|
|
184
|
+
- You want secure defaults and best practices applied automatically
|
|
185
|
+
- You need to quickly prototype or build production-ready infrastructure
|
|
186
|
+
|
|
187
|
+
To discover available patterns, use the `GetAwsSolutionsConstructPattern` tool.
|
|
188
|
+
|
|
189
|
+
## Security with CDK Nag
|
|
190
|
+
|
|
191
|
+
CDK Nag is a crucial tool for ensuring your CDK applications follow AWS security best practices. **Always apply CDK Nag to all your stacks by default.**
|
|
192
|
+
|
|
193
|
+
Key security practices to remember:
|
|
194
|
+
- Follow the principle of least privilege for IAM
|
|
195
|
+
- Secure S3 buckets with encryption, access controls, and policies
|
|
196
|
+
- Implement secure authentication with Cognito
|
|
197
|
+
- Secure API Gateway endpoints with proper authorization
|
|
198
|
+
|
|
199
|
+
For detailed guidance, use the `CDKNagGuidance` tool.
|
|
200
|
+
|
|
201
|
+
## Operational Excellence with Lambda Powertools
|
|
202
|
+
|
|
203
|
+
Always implement Lambda Powertools for:
|
|
204
|
+
- Structured Logging
|
|
205
|
+
- Tracing
|
|
206
|
+
- Metrics
|
|
207
|
+
|
|
208
|
+
For detailed guidance, use the `LambdaPowertoolsGuidance` tool.
|
|
209
|
+
|
|
210
|
+
## Available MCP Tools
|
|
211
|
+
|
|
212
|
+
This MCP server provides several tools to help you implement AWS CDK best practices:
|
|
213
|
+
|
|
214
|
+
1. **CDKGeneralGuidance**: This document - general CDK best practices
|
|
215
|
+
2. **ExplainCDKNagRule**: Explain a specific CDK Nag rule with AWS Well-Architected guidance
|
|
216
|
+
3. **CheckCDKNagSuppressions**: Check if CDK code contains Nag suppressions that require human review
|
|
217
|
+
4. **GenerateBedrockAgentSchemaFromFile**: Generate OpenAPI schema for Bedrock Agent Action Groups from Lambda functions
|
|
218
|
+
5. **GetAwsSolutionsConstructPattern**: Search and discover AWS Solutions Constructs patterns
|
|
219
|
+
6. **SearchGenAICDKConstructs**: Search for GenAI CDK constructs by name or type
|
|
220
|
+
|
|
221
|
+
## Available MCP Resources
|
|
222
|
+
|
|
223
|
+
This MCP server also provides several resources for accessing documentation:
|
|
224
|
+
|
|
225
|
+
1. **cdk-nag://rules/{rule_pack}**: Get all rules for a specific CDK Nag rule pack
|
|
226
|
+
2. **cdk-nag://warnings/{rule_pack}**: Get warnings for a specific CDK Nag rule pack
|
|
227
|
+
3. **cdk-nag://errors/{rule_pack}**: Get errors for a specific CDK Nag rule pack
|
|
228
|
+
4. **lambda-powertools://{topic}**: Get Lambda Powertools guidance on a specific topic
|
|
229
|
+
5. **aws-solutions-constructs://{pattern_name}**: Get complete documentation for an AWS Solutions Constructs pattern
|
|
230
|
+
6. **genai-cdk-constructs://{construct_type}/{construct_name}**: Get documentation for a GenAI CDK construct
|
|
231
|
+
|
|
232
|
+
Always check for these tools and resources when implementing CDK infrastructure to ensure you're following AWS best practices.
|
|
@@ -0,0 +1,192 @@
|
|
|
1
|
+
# CDK Nag Guidance
|
|
2
|
+
|
|
3
|
+
This guide provides implementation details for CDK Nag in AWS CDK projects. CDK Nag analyzes CDK constructs to identify security issues based on AWS Well-Architected Framework best practices.
|
|
4
|
+
|
|
5
|
+
## Table of Contents
|
|
6
|
+
|
|
7
|
+
1. [Optional Integration](#optional-integration)
|
|
8
|
+
2. [AwsSolutions Rule Pack](#awssolutions-rule-pack)
|
|
9
|
+
3. [Suppressing Violations](#suppressing-violations)
|
|
10
|
+
|
|
11
|
+
## Optional Integration
|
|
12
|
+
|
|
13
|
+
CDK Nag can be made optional in your CDK projects, which is particularly useful during development or prototyping phases. Here are several approaches to make CDK Nag optional:
|
|
14
|
+
|
|
15
|
+
### Using Environment Variables
|
|
16
|
+
|
|
17
|
+
```typescript
|
|
18
|
+
import { AwsSolutionsChecks } from 'cdk-nag';
|
|
19
|
+
import { App } from 'aws-cdk-lib';
|
|
20
|
+
|
|
21
|
+
// Create your CDK app
|
|
22
|
+
const app = new App();
|
|
23
|
+
|
|
24
|
+
// Add your stacks
|
|
25
|
+
new MyStack(app, 'MyStack');
|
|
26
|
+
|
|
27
|
+
// Apply CDK Nag conditionally based on environment variable
|
|
28
|
+
if (process.env.ENABLE_CDK_NAG === 'true') {
|
|
29
|
+
console.log('CDK Nag enabled - checking for security issues');
|
|
30
|
+
AwsSolutionsChecks.check(app);
|
|
31
|
+
} else {
|
|
32
|
+
console.log('CDK Nag disabled - skipping security checks');
|
|
33
|
+
}
|
|
34
|
+
```
|
|
35
|
+
|
|
36
|
+
### Using CDK Context Parameters
|
|
37
|
+
|
|
38
|
+
```typescript
|
|
39
|
+
import { AwsSolutionsChecks } from 'cdk-nag';
|
|
40
|
+
import { App } from 'aws-cdk-lib';
|
|
41
|
+
|
|
42
|
+
// Create your CDK app
|
|
43
|
+
const app = new App();
|
|
44
|
+
|
|
45
|
+
// Add your stacks
|
|
46
|
+
new MyStack(app, 'MyStack');
|
|
47
|
+
|
|
48
|
+
// Apply CDK Nag conditionally based on context parameter
|
|
49
|
+
if (app.node.tryGetContext('enableCdkNag') === 'true') {
|
|
50
|
+
console.log('CDK Nag enabled - checking for security issues');
|
|
51
|
+
AwsSolutionsChecks.check(app);
|
|
52
|
+
} else {
|
|
53
|
+
console.log('CDK Nag disabled - skipping security checks');
|
|
54
|
+
}
|
|
55
|
+
```
|
|
56
|
+
|
|
57
|
+
To enable CDK Nag with this approach, use:
|
|
58
|
+
|
|
59
|
+
```bash
|
|
60
|
+
cdk deploy --context enableCdkNag=true
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
### Environment-Specific Application
|
|
64
|
+
|
|
65
|
+
You can also apply CDK Nag only to specific environments:
|
|
66
|
+
|
|
67
|
+
```typescript
|
|
68
|
+
import { AwsSolutionsChecks } from 'cdk-nag';
|
|
69
|
+
import { App, Stack } from 'aws-cdk-lib';
|
|
70
|
+
|
|
71
|
+
// Create your CDK app
|
|
72
|
+
const app = new App();
|
|
73
|
+
|
|
74
|
+
// Get environment from context
|
|
75
|
+
const environment = app.node.tryGetContext('environment') || 'development';
|
|
76
|
+
|
|
77
|
+
// Add your stacks
|
|
78
|
+
const stack = new MyStack(app, 'MyStack');
|
|
79
|
+
|
|
80
|
+
// Apply CDK Nag only to production and staging environments
|
|
81
|
+
if (['production', 'staging'].includes(environment)) {
|
|
82
|
+
console.log(`Applying CDK Nag checks for ${environment} environment`);
|
|
83
|
+
AwsSolutionsChecks.check(stack);
|
|
84
|
+
}
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
## AwsSolutions Rule Pack
|
|
88
|
+
|
|
89
|
+
The AwsSolutions rule pack is the primary rule pack provided by CDK Nag. It contains rules based on AWS Solutions best practices and the AWS Well-Architected Framework.
|
|
90
|
+
|
|
91
|
+
To apply the AwsSolutions rule pack:
|
|
92
|
+
|
|
93
|
+
```typescript
|
|
94
|
+
import { AwsSolutionsChecks } from 'cdk-nag';
|
|
95
|
+
import { App } from 'aws-cdk-lib';
|
|
96
|
+
|
|
97
|
+
// Create your CDK app
|
|
98
|
+
const app = new App();
|
|
99
|
+
|
|
100
|
+
// Add your stacks
|
|
101
|
+
new MyStack(app, 'MyStack');
|
|
102
|
+
|
|
103
|
+
// Apply AwsSolutions checks
|
|
104
|
+
AwsSolutionsChecks.check(app);
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
## Suppressing Violations
|
|
108
|
+
|
|
109
|
+
⚠️ **CRITICAL: HUMAN DEVELOPER RESPONSIBILITY ONLY** ⚠️
|
|
110
|
+
|
|
111
|
+
CDK Nag suppressions should NEVER be applied by AI assistants or MCP clients. This is exclusively a human developer responsibility that requires careful consideration and security expertise.
|
|
112
|
+
|
|
113
|
+
### Process for Handling Violations
|
|
114
|
+
|
|
115
|
+
1. **Review**: Human developers should review all CDK Nag warnings and errors
|
|
116
|
+
2. **Analyze**: Determine if each violation represents a genuine security concern
|
|
117
|
+
3. **Remediate**: Whenever possible, fix the underlying issue rather than suppressing
|
|
118
|
+
4. **Document**: If suppression is necessary, document the specific reason with detailed justification
|
|
119
|
+
5. **Approve**: Require explicit human approval (ideally through code review)
|
|
120
|
+
|
|
121
|
+
### For Human Developers Only
|
|
122
|
+
|
|
123
|
+
The following code examples are provided ONLY for human developers to understand the syntax. AI assistants should NOT implement these patterns without explicit human review and approval.
|
|
124
|
+
|
|
125
|
+
#### Stack-Level Suppressions
|
|
126
|
+
|
|
127
|
+
```typescript
|
|
128
|
+
// HUMAN DEVELOPER RESPONSIBILITY - DO NOT IMPLEMENT AUTOMATICALLY
|
|
129
|
+
// This code must only be added after careful security review
|
|
130
|
+
import { NagSuppressions } from 'cdk-nag';
|
|
131
|
+
import { Stack } from 'aws-cdk-lib';
|
|
132
|
+
|
|
133
|
+
// Create your stack
|
|
134
|
+
const stack = new MyStack(app, 'MyStack');
|
|
135
|
+
|
|
136
|
+
// Suppress a rule for the entire stack
|
|
137
|
+
NagSuppressions.addStackSuppressions(stack, [
|
|
138
|
+
{
|
|
139
|
+
id: 'AwsSolutions-IAM4',
|
|
140
|
+
reason: 'REQUIRES SPECIFIC HUMAN JUSTIFICATION',
|
|
141
|
+
},
|
|
142
|
+
]);
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
#### Resource-Level Suppressions
|
|
146
|
+
|
|
147
|
+
```typescript
|
|
148
|
+
// HUMAN DEVELOPER RESPONSIBILITY - DO NOT IMPLEMENT AUTOMATICALLY
|
|
149
|
+
// This code must only be added after careful security review
|
|
150
|
+
import { NagSuppressions } from 'cdk-nag';
|
|
151
|
+
import { Role, ServicePrincipal, ManagedPolicy } from 'aws-cdk-lib/aws-iam';
|
|
152
|
+
|
|
153
|
+
// Create a role with a managed policy
|
|
154
|
+
const role = new Role(this, 'MyRole', {
|
|
155
|
+
assumedBy: new ServicePrincipal('lambda.amazonaws.com'),
|
|
156
|
+
});
|
|
157
|
+
role.addManagedPolicy(ManagedPolicy.fromAwsManagedPolicyName('AmazonS3ReadOnlyAccess'));
|
|
158
|
+
|
|
159
|
+
// Suppress the warning for this specific role
|
|
160
|
+
NagSuppressions.addResourceSuppressions(role, [
|
|
161
|
+
{
|
|
162
|
+
id: 'AwsSolutions-IAM4',
|
|
163
|
+
reason: 'REQUIRES SPECIFIC HUMAN JUSTIFICATION',
|
|
164
|
+
},
|
|
165
|
+
]);
|
|
166
|
+
```
|
|
167
|
+
|
|
168
|
+
#### Path-Based Suppressions
|
|
169
|
+
|
|
170
|
+
```typescript
|
|
171
|
+
// HUMAN DEVELOPER RESPONSIBILITY - DO NOT IMPLEMENT AUTOMATICALLY
|
|
172
|
+
// This code must only be added after careful security review
|
|
173
|
+
import { NagSuppressions } from 'cdk-nag';
|
|
174
|
+
import { Construct } from 'constructs';
|
|
175
|
+
|
|
176
|
+
// Create a construct
|
|
177
|
+
const myConstruct = new MyConstruct(this, 'MyConstruct');
|
|
178
|
+
|
|
179
|
+
// Suppress a rule for the construct and all its children
|
|
180
|
+
NagSuppressions.addResourceSuppressionsByPath(
|
|
181
|
+
stack,
|
|
182
|
+
'/MyStack/MyConstruct',
|
|
183
|
+
[
|
|
184
|
+
{
|
|
185
|
+
id: 'AwsSolutions-IAM5',
|
|
186
|
+
reason: 'REQUIRES SPECIFIC HUMAN JUSTIFICATION',
|
|
187
|
+
},
|
|
188
|
+
]
|
|
189
|
+
);
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
For more detailed security best practices by service (IAM, S3, Cognito, API Gateway), please use the **CDK Guidance** tool in this MCP server and refer to the Security Best Practices section.
|
|
@@ -0,0 +1,137 @@
|
|
|
1
|
+
# Action Groups
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
Action groups define functions your agent can call, connecting Bedrock Agents to your business logic via Lambda functions. The agent uses an OpenAPI schema to understand what your functions do and how to call them.
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph LR
|
|
9
|
+
A[Agent] --> B[Action Group]
|
|
10
|
+
B --> C[Lambda Function]
|
|
11
|
+
B --> D[OpenAPI Schema]
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Action Group Properties
|
|
15
|
+
|
|
16
|
+
| Name | Type | Required | Description |
|
|
17
|
+
|---|---|---|---|
|
|
18
|
+
| name | string | Yes | The name of the action group |
|
|
19
|
+
| description | string | No | A description of the action group |
|
|
20
|
+
| apiSchema | ApiSchema | No | The API Schema |
|
|
21
|
+
| executor | ActionGroupExecutor | No | The action group executor |
|
|
22
|
+
| enabled | boolean | No | Specifies whether the action group is available for the agent to invoke or not when sending an InvokeAgent request. Defaults to true |
|
|
23
|
+
| forceDelete | boolean | No | Specifies whether to delete the resource even if it's in use. Defaults to false |
|
|
24
|
+
| functionSchema | CfnAgent.FunctionSchemaProperty | No | Defines functions that each define parameters that the agent needs to invoke from the user |
|
|
25
|
+
| parentActionGroupSignature | ParentActionGroupSignature | No | The AWS Defined signature for enabling certain capabilities in your agent. When specified, description, apiSchema, and actionGroupExecutor must be blank |
|
|
26
|
+
|
|
27
|
+
## OpenAPI Schema Generation & Best Practices
|
|
28
|
+
|
|
29
|
+
For Action Groups, use the built-in OpenAPI schema generation tool provided by the AWS CDK MCP server:
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
// Using MCP client
|
|
33
|
+
const result = await use_mcp_tool({
|
|
34
|
+
server_name: "awslabs.cdk-mcp-server",
|
|
35
|
+
tool_name: "GenerateBedrockAgentSchemaFromFile",
|
|
36
|
+
arguments: {
|
|
37
|
+
lambda_code_path: "path/to/your/lambda.py",
|
|
38
|
+
output_path: "path/to/output/openapi.json"
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
The tool will:
|
|
44
|
+
|
|
45
|
+
1. Parse your Lambda function code
|
|
46
|
+
2. Extract function signatures and docstrings
|
|
47
|
+
3. Generate a Bedrock-compatible OpenAPI schema
|
|
48
|
+
4. Save it to the specified output path
|
|
49
|
+
|
|
50
|
+
### Key Requirements
|
|
51
|
+
- Ensure each operation has a unique operationId
|
|
52
|
+
- Define complete response schemas
|
|
53
|
+
- Use `fromLocalAsset` (not `fromAsset`) for API schemas
|
|
54
|
+
- Include detailed descriptions for all endpoints, parameters, and return values
|
|
55
|
+
- Use proper type annotations with Python type hints
|
|
56
|
+
|
|
57
|
+
### Lambda Function Example
|
|
58
|
+
|
|
59
|
+
```python
|
|
60
|
+
from aws_lambda_powertools.event_handler import BedrockAgentResolver
|
|
61
|
+
from typing import Annotated, List
|
|
62
|
+
from aws_lambda_powertools.event_handler.openapi.params import Query, Path
|
|
63
|
+
from pydantic import BaseModel, Field
|
|
64
|
+
|
|
65
|
+
app = BedrockAgentResolver()
|
|
66
|
+
|
|
67
|
+
class Product(BaseModel):
|
|
68
|
+
product_id: str = Field(description="Unique product identifier")
|
|
69
|
+
name: str = Field(description="Product name")
|
|
70
|
+
price: float = Field(description="Product price in USD")
|
|
71
|
+
|
|
72
|
+
@app.get("/products", description="List available products")
|
|
73
|
+
def list_products(
|
|
74
|
+
category: Annotated[str, Query(description="Product category")] = None
|
|
75
|
+
) -> List[Product]:
|
|
76
|
+
# Your business logic here
|
|
77
|
+
return [Product(product_id="1", name="Product A", price=19.99)]
|
|
78
|
+
|
|
79
|
+
def lambda_handler(event, context):
|
|
80
|
+
return app.resolve(event, context)
|
|
81
|
+
```
|
|
82
|
+
|
|
83
|
+
## Examples
|
|
84
|
+
|
|
85
|
+
### TypeScript
|
|
86
|
+
|
|
87
|
+
```ts
|
|
88
|
+
const actionGroupFunction = new lambda_python.PythonFunction(this, 'ActionGroupFunction', {
|
|
89
|
+
runtime: lambda.Runtime.PYTHON_3_12,
|
|
90
|
+
entry: path.join(__dirname, '../lambda/action-group'),
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
// Example of proper Action Group setup with fromLocalAsset
|
|
94
|
+
const actionGroup = new AgentActionGroup({
|
|
95
|
+
name: 'query-library',
|
|
96
|
+
description: 'Use these functions to get information about the books in the library.',
|
|
97
|
+
executor: bedrock.ActionGroupExecutor.fromlambdaFunction(actionGroupFunction),
|
|
98
|
+
enabled: true,
|
|
99
|
+
apiSchema: bedrock.ApiSchema.fromLocalAsset(path.join(__dirname, 'action-group.yaml')),
|
|
100
|
+
});
|
|
101
|
+
|
|
102
|
+
agent.addActionGroup(actionGroup);
|
|
103
|
+
|
|
104
|
+
// Real-world example with account actions
|
|
105
|
+
const agentAccountActions = new AgentActionGroup({
|
|
106
|
+
name: "agent-account-actions",
|
|
107
|
+
description: "Use these functions to take actions on authenticated user's accounts",
|
|
108
|
+
executor: bedrock.ActionGroupExecutor.fromlambdaFunction(accountActionsLambda),
|
|
109
|
+
apiSchema: bedrock.ApiSchema.fromLocalAsset(
|
|
110
|
+
path.join(agentsLambdaDir, "account_actions", "openapi.json"),
|
|
111
|
+
),
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
agent.addActionGroup(agentAccountActions);
|
|
115
|
+
```
|
|
116
|
+
|
|
117
|
+
### Python
|
|
118
|
+
|
|
119
|
+
```python
|
|
120
|
+
action_group_function = PythonFunction(
|
|
121
|
+
self,
|
|
122
|
+
"LambdaFunction",
|
|
123
|
+
runtime=Runtime.PYTHON_3_12,
|
|
124
|
+
entry="./lambda",
|
|
125
|
+
index="app.py",
|
|
126
|
+
handler="lambda_handler",
|
|
127
|
+
)
|
|
128
|
+
|
|
129
|
+
actionGroup = bedrock.AgentActionGroup(
|
|
130
|
+
name="query-library",
|
|
131
|
+
description="Use these functions to get information about the books in the library.",
|
|
132
|
+
executor= bedrock.ActionGroupExecutor.fromlambda_function(action_group_function),
|
|
133
|
+
enabled=True,
|
|
134
|
+
api_schema=bedrock.ApiSchema.from_local_asset("action-group.yaml"))
|
|
135
|
+
|
|
136
|
+
agent.add_action_group(actionGroup)
|
|
137
|
+
```
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
# Agent Alias
|
|
2
|
+
|
|
3
|
+
## Overview
|
|
4
|
+
|
|
5
|
+
After you have sufficiently iterated on your working draft and are satisfied with the behavior of your agent, you can set it up for deployment and integration into your application by creating aliases of your agent.
|
|
6
|
+
|
|
7
|
+
To deploy your agent, you need to create an alias. During alias creation, Amazon Bedrock automatically creates a version of your agent. The alias points to this newly created version. You can point the alias to a previously created version if necessary. You then configure your application to make API calls to that alias.
|
|
8
|
+
|
|
9
|
+
By default, the `Agent` resource does not create any aliases, and you can use the 'DRAFT' version.
|
|
10
|
+
|
|
11
|
+
## Specific Version
|
|
12
|
+
|
|
13
|
+
You can use the `AgentAlias` resource if you want to create an Alias for an existing Agent.
|
|
14
|
+
|
|
15
|
+
## Example
|
|
16
|
+
|
|
17
|
+
### TypeScript
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
const agentAlias2 = new bedrock.AgentAlias(this, 'myalias2', {
|
|
21
|
+
aliasName: 'myalias',
|
|
22
|
+
agent: agent,
|
|
23
|
+
agentVersion: '1', // optional
|
|
24
|
+
description: 'mytest'
|
|
25
|
+
});
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
### Python
|
|
29
|
+
|
|
30
|
+
```python
|
|
31
|
+
agent_alias_2 = bedrock.AgentAlias(self, 'myalias2',
|
|
32
|
+
alias_name='myalias',
|
|
33
|
+
agent=agent,
|
|
34
|
+
agent_version='1', # optional
|
|
35
|
+
description='mytest'
|
|
36
|
+
)
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
[View full documentation](https://github.com/awslabs/generative-ai-cdk-constructs/blob/main/src/cdk-lib/bedrock/README.md)
|