Skip to content

Commit 8242240

Browse files
author
Yuriy Bezsonov
committed
WIP with initial cdk
1 parent 52630e0 commit 8242240

30 files changed

Lines changed: 18721 additions & 45 deletions

.kiro/debug/chats/1.chat

Lines changed: 3402 additions & 0 deletions
Large diffs are not rendered by default.

.kiro/debug/chats/2.chat

Lines changed: 3403 additions & 0 deletions
Large diffs are not rendered by default.

.kiro/debug/debug.log

Lines changed: 3494 additions & 0 deletions
Large diffs are not rendered by default.

.kiro/debug/execution-log.json

Lines changed: 5059 additions & 0 deletions
Large diffs are not rendered by default.

.kiro/debug/kiroDebugLogs.zip

6.77 MB
Binary file not shown.

.kiro/specs/infra/design.md

Lines changed: 80 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,7 @@ infra/
2828
│ │ └── WorkshopApp.java # Main CDK application
2929
│ ├── pom.xml
3030
│ └── cdk.json
31-
├── cfn/ # Generated CloudFormation templates
32-
│ ├── ide.yaml
33-
│ ├── java-on-aws.yaml
34-
│ ├── java-on-eks.yaml
35-
│ ├── java-ai-agents.yaml
36-
│ └── java-spring-ai-agents.yaml
31+
├── workshop-template.yaml # Generated unified CloudFormation template
3732
├── scripts/
3833
│ ├── workshops/ # Workshop-specific orchestration scripts
3934
│ │ ├── ide.sh
@@ -103,6 +98,75 @@ public class WorkshopStack extends Stack {
10398
**CodeBuild**: Creates CodeBuild project for workshop setup automation
10499
**Roles**: Creates IAM roles and policies for workshop resources
105100

101+
### Lambda Function Architecture
102+
103+
#### Design Rationale
104+
The new design uses **minimal Lambda functions** with inline Python source code for CloudFormation template compatibility:
105+
- **Java CDK constructs** for infrastructure definition and type safety
106+
- **Single inline Python Lambda** for EC2 instance launching with intelligent failover
107+
- **EC2 User Data** for bootstrap processes instead of Lambda functions
108+
- **Native CDK implementations** for simple operations (prefix lists, secrets)
109+
110+
#### Simplified Architecture
111+
Instead of multiple Lambda functions, the new design uses:
112+
1. **One launcher Lambda** with inline Python code for intelligent EC2 failover
113+
2. **Native CDK features** for CloudFront prefix lists and Secrets Manager
114+
3. **EC2 User Data scripts** for instance bootstrap
115+
116+
#### Lambda Function Naming & Mapping
117+
118+
**Concise Naming Scheme**:
119+
- Instance-specific: `{instance-name}-{purpose}`
120+
- Workshop-wide: `workshop-{purpose}`
121+
122+
| Old Function Name Pattern | New Implementation | New Function Name | Scope | Purpose |
123+
|---------------------------|-------------------|-------------------|-------|---------|
124+
| `{instance}-prefix-list-lambda` | **CDK Native** | N/A | N/A | Static CloudFront prefix list reference |
125+
| `{instance}-instance-launcher` | **Inline Lambda** | `{instance}-launcher` | Instance | EC2 instance launching with intelligent failover |
126+
| `{instance}-password-lambda` | **CDK Native** | N/A | N/A | Direct secret value reference |
127+
| `{instance}-bootstrap-lambda` | **EC2 User Data** | N/A | N/A | Moved to EC2 User Data scripts |
128+
| `unicornstore-db-setup-lambda` | **Setup Scripts** | N/A | N/A | Moved to workshop setup scripts |
129+
130+
#### External Resource Approach
131+
The new design uses **external files** for all complex scripts and code, loaded via CDK for better maintainability while preserving CloudFormation template compatibility through inline code generation. This approach eliminates hard-to-maintain inline code blocks.
132+
133+
#### External Resource Organization
134+
```
135+
infra/cdk/src/main/resources/
136+
├── launcher.py # EC2 instance launching with multi-AZ/instance-type failover
137+
└── bootstrap.sh # EC2 User Data bootstrap script with CloudWatch logging
138+
```
139+
140+
#### Usage in IDE Construct
141+
```java
142+
// Create instance launcher Lambda loading from external file
143+
var instanceLauncherFunction = Function.Builder.create(this, "IdeInstanceLauncherFunction")
144+
.runtime(Runtime.PYTHON_3_13)
145+
.handler("index.lambda_handler")
146+
.code(Code.fromInline(loadFile("/launcher.py")))
147+
.timeout(Duration.minutes(5))
148+
.functionName(instanceName + "-launcher")
149+
.role(props.getLambdaRole())
150+
.build();
151+
152+
// Create User Data from external script with variable substitution
153+
var userData = UserData.forLinux();
154+
String bootstrapScript = loadFile("/bootstrap.sh")
155+
.replace("${stackName}", Aws.STACK_NAME)
156+
.replace("${awsRegion}", Aws.REGION)
157+
.replace("${idePassword}", ideSecretsManagerPassword.secretValueFromJson("password").unsafeUnwrap());
158+
userData.addCommands(bootstrapScript.split("\n"));
159+
160+
// Helper method for loading files
161+
private String loadFile(String filePath) {
162+
try {
163+
return Files.readString(Path.of(getClass().getResource(filePath).getPath()));
164+
} catch (IOException e) {
165+
throw new RuntimeException("Failed to load file " + filePath, e);
166+
}
167+
}
168+
```
169+
106170
### Script Organization
107171

108172
#### Convention-Based Script Discovery
@@ -142,10 +206,10 @@ echo "🔧 Generating unified template..."
142206

143207
cd cdk
144208
mvn clean package
145-
cdk synth stack --yaml --path-metadata false --version-reporting false > ../cfn/stack.yaml
209+
cdk synth WorkshopStack --yaml --path-metadata false --version-reporting false > ../workshop-template.yaml
146210
cd ..
147211

148-
echo "✅ Generated cfn/stack.yaml"
212+
echo "✅ Generated workshop-template.yaml"
149213
```
150214

151215
**scripts/cfn/sync.sh**:
@@ -160,13 +224,13 @@ for workshop in "${WORKSHOPS[@]}"; do
160224

161225
if [[ -d "$target_dir" ]]; then
162226
# Copy CloudFormation template
163-
cp "cfn/stack.yaml" "$target_dir/$workshop-stack.yaml"
164-
echo "✅ Synced stack.yaml to $workshop/static/$workshop-stack.yaml"
227+
cp "workshop-template.yaml" "$target_dir/$workshop-stack.yaml"
228+
echo "✅ Synced workshop-template.yaml to $workshop/static/$workshop-stack.yaml"
165229

166-
# Copy IAM policy if it exists
167-
if [[ -f "policies/policy.json" ]]; then
168-
cp "policies/policy.json" "$target_dir/"
169-
echo "✅ Synced policy to $workshop/static/"
230+
# Copy IAM policy from resources
231+
if [[ -f "cdk/src/main/resources/iam-policy.json" ]]; then
232+
cp "cdk/src/main/resources/iam-policy.json" "$target_dir/policy.json"
233+
echo "✅ Synced iam-policy.json to $workshop/static/policy.json"
170234
fi
171235
else
172236
echo "⚠️ Directory $target_dir not found, skipping sync for $workshop"
@@ -283,8 +347,8 @@ public class BuildConfig {
283347
*For any* migrated workshop type, the new template should produce equivalent infrastructure to the existing template
284348
**Validates: Requirements 5.5**
285349

286-
### Property 17: Lambda Function Consolidation
287-
*For any* consolidated Lambda handler, it should provide equivalent functionality to all original Python/JavaScript functions
350+
### Property 17: Lambda Function Modularity
351+
*For any* modular Lambda function, it should provide equivalent functionality to original functions while maintaining CloudFormation template compatibility through inline Python source code
288352
**Validates: Requirements 5.8**
289353

290354
## Error Handling

.kiro/specs/infra/requirements.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,6 @@ This document specifies the requirements for creating a new AWS workshop infrast
7777
5. WHEN each workshop migration completes, THE system SHALL validate that new templates produce equivalent infrastructure to existing ones before migrating the next workshop type
7878
6. WHEN migrating CDK constructs, THE system SHALL refactor existing code to use unified patterns and updated package names
7979
7. WHEN migrating setup scripts, THE system SHALL reorganize them into logical categories with improved error handling
80-
8. WHEN migrating Lambda functions, THE system SHALL consolidate existing Python/JavaScript functions into a single Java Lambda handler
80+
8. WHEN migrating Lambda functions, THE system SHALL create modular Python Lambda functions with inline source code stored in CDK resources for CloudFormation template compatibility
8181
9. WHEN the new system is ready, THE system SHALL enable parallel operation where both old and new systems function independently
8282

.kiro/specs/infra/tasks.md

Lines changed: 32 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,21 @@
22

33
## Infrastructure Setup (1.x)
44

5-
- [ ] 1.1 Create new infra directory structure
5+
- [x] 1.1 Create new infra directory structure
66
- Create infra/{cdk,cfn,scripts/{workshops,setup,lib,deploy,test,cleanup},policies} directories
77
- Create CDK Java package structure: infra/cdk/src/main/java/sample/com/{constructs,stacks}
88
- Create infra/cdk/src/main/resources directory for assets
99
- Ensure infrastructure/ directory remains untouched during setup
1010
- _Requirements: 5.1_
1111

12-
- [ ] 1.2 Initialize CDK project structure
13-
- Create infra/cdk/pom.xml with unified dependencies (CDK 2.167.1, Java 25)
12+
- [x] 1.2 Initialize CDK project structure
13+
- Create infra/cdk/pom.xml with unified dependencies (CDK 2.215.0, Java 25)
1414
- Create infra/cdk/cdk.json with CDK configuration
1515
- Set up Maven project structure with proper groupId (sample.com) and artifactId (infra)
1616
- Configure CDK app entry point
1717
- _Requirements: 5.6_
1818

19-
- [ ] 1.3 Create common script utilities
19+
- [x] 1.3 Create common script utilities
2020
- Create infra/scripts/lib/common.sh with emoji-based logging functions (log_info, log_success, log_error, log_warning)
2121
- Implement consistent error handling with handle_error function and trap setup
2222
- Create infra/scripts/lib/wait-for-resources.sh for resource readiness checking
@@ -25,86 +25,95 @@
2525

2626
## Build System (2.x)
2727

28-
- [ ] 2.1 Create template generation script
29-
- Create infra/scripts/cfn/generate.sh that builds CDK and generates single stack.yaml
28+
- [x] 2.1 Create template generation script
29+
- Create infra/scripts/cfn/generate.sh that builds CDK and generates workshop-template.yaml
3030
- Implement proper error handling and progress feedback with emoji logging
3131
- Include sed transformation for CloudFormation substitutions (AccountId pattern)
3232
- Test script execution and validate generated template structure
3333
- _Requirements: 4.1, 4.4_
3434

35-
- [ ] 2.2 Create workshop sync script
36-
- Create infra/scripts/cfn/sync.sh that copies stack.yaml to workshop directories as {workshop}-stack.yaml
37-
- Include policy.json copying from policies/ directory to workshop static/ directories
35+
- [x] 2.2 Create workshop sync script
36+
- Create infra/scripts/cfn/sync.sh that copies workshop-template.yaml to workshop directories as {workshop}-stack.yaml
37+
- Include iam-policy.json copying from cdk/src/main/resources/ directory to workshop static/ directories
3838
- Implement directory existence checking and error reporting
3939
- Support workshop list: ide, java-on-aws, java-on-eks, java-ai-agents, java-spring-ai-agents
4040
- _Requirements: 4.2, 4.5_
4141

42-
- [ ] 2.3 Set up build automation
42+
- [x] 2.3 Set up build automation
4343
- Create infra/package.json with generate and sync npm scripts
4444
- Make scripts executable and test npm run generate && npm run sync workflow
4545
- Validate that generated templates are copied to correct locations with proper naming
46-
- Create infra/policies directory and copy existing iam-policy.json as policy.json
46+
- Copy existing iam-policy.json to infra/cdk/src/main/resources/ for single source of truth
4747
- _Requirements: 4.3_
4848

4949
## Base IDE Stack (10.x)
5050

51-
- [ ] 10.1 Create core CDK constructs
51+
- [x] 10.1 Create core CDK constructs
5252
- Create infra/cdk/src/main/java/sample/com/constructs/Roles.java for IAM roles and policies
5353
- Create infra/cdk/src/main/java/sample/com/constructs/Vpc.java for VPC with 2 AZs and 1 NAT gateway
5454
- Create infra/cdk/src/main/java/sample/com/constructs/Ide.java for VS Code IDE environment
5555
- Create infra/cdk/src/main/java/sample/com/constructs/CodeBuild.java for workshop setup automation
5656
- _Requirements: 1.1, 5.6_
5757

58-
- [ ] 10.2 Migrate and refactor Roles construct
58+
- [x] 10.2 Migrate and refactor Roles construct
5959
- Copy infrastructure/cdk/src/main/java/com/unicorn/constructs/WorkshopFunction.java patterns for IAM setup
6060
- Update package names from com.unicorn to sample.com
6161
- Consolidate all IAM roles and policies into single Roles construct
6262
- Include Bedrock permissions for AI workshops in the unified roles
6363
- _Requirements: 5.6_
6464

65-
- [ ] 10.3 Migrate and refactor Vpc construct
65+
- [x] 10.3 Migrate and refactor Vpc construct
6666
- Copy infrastructure/cdk/src/main/java/com/unicorn/constructs/WorkshopVpc.java
6767
- Update package names and simplify to standard VPC pattern
6868
- Ensure VPC supports both IDE and EKS workloads with proper subnet configuration
6969
- Remove workshop-specific customizations, keep generic VPC setup
7070
- _Requirements: 5.6_
7171

72-
- [ ] 10.4 Migrate and refactor Ide construct
72+
- [x] 10.4 Create optimized Python Lambda function with direct CDK implementation
73+
- Create Python source file infra/cdk/src/main/resources/launcher.py for EC2 instance launching
74+
- Use direct Function.Builder.create() with Code.fromInline(loadFile()) approach
75+
- Replace prefix.py, password.py, database.py with native CDK implementations
76+
- Move bootstrap functionality to EC2 User Data script for simplicity
77+
- Maintain identical functionality while reducing Lambda complexity by 80%
78+
- _Requirements: 5.8_
79+
80+
- [x] 10.5 Migrate and refactor Ide construct
7381
- Copy infrastructure/cdk/src/main/java/com/unicorn/constructs/VSCodeIde.java
7482
- Update package names and integrate with new Roles and Vpc constructs
75-
- Ensure IDE construct works with unified IAM roles
76-
- Test IDE construct creates proper EC2 instance with VS Code setup
83+
- Replace existing Lambda functions with single launcher Lambda using direct CDK Function creation
84+
- Create comprehensive bootstrap script in infra/cdk/src/main/resources/bootstrap.sh
85+
- Ensure IDE construct creates proper EC2 instance with complete VS Code setup and CloudFront
7786
- _Requirements: 5.6_
7887

79-
- [ ] 10.5 Migrate and refactor CodeBuild construct
88+
- [ ] 10.6 Migrate and refactor CodeBuild construct
8089
- Copy infrastructure/cdk/src/main/java/com/unicorn/constructs/CodeBuildResource.java
8190
- Update to use new Roles construct and accept WORKSHOP_TYPE environment variable
8291
- Configure CodeBuild to run in VPC and execute workshop-specific setup scripts
8392
- Include proper error handling and timeout configuration (60 minutes)
8493
- _Requirements: 5.6, 3.6_
8594

86-
- [ ] 10.6 Create unified WorkshopStack
95+
- [x] 10.7 Create unified WorkshopStack
8796
- Create infra/cdk/src/main/java/sample/com/stacks/WorkshopStack.java
8897
- Implement environment variable logic: WORKSHOP_TYPE with "ide" default
8998
- Always create: Roles, Vpc, Ide, CodeBuild
9099
- Conditionally create resources based on workshop type (EKS, Database for non-ide workshops)
91100
- _Requirements: 1.2, 1.3_
92101

93-
- [ ] 10.7 Create CDK application entry point
102+
- [x] 10.8 Create CDK application entry point
94103
- Create infra/cdk/src/main/java/sample/com/WorkshopApp.java
95104
- Configure single WorkshopStack instantiation
96105
- Set up proper CDK app synthesis
97106
- Test CDK synth command produces valid CloudFormation template
98107
- _Requirements: 1.1_
99108

100-
- [ ] 10.8 Create base workshop setup scripts
109+
- [x] 10.9 Create base workshop setup scripts
101110
- Create infra/scripts/setup/base.sh for common tool installation (git, curl, wget, unzip)
102111
- Create infra/scripts/setup/ide.sh for IDE-specific configuration
103112
- Create infra/scripts/workshops/ide.sh that orchestrates base.sh and ide.sh
104113
- Implement convention-based script discovery (script name matches stack name)
105114
- _Requirements: 3.1, 3.3_
106115

107-
- [ ] 10.9 Test and validate IDE stack
116+
- [x] 10.10 Test and validate IDE stack
108117
- Generate CloudFormation template: npm run generate
109118
- Validate template contains only VPC, IDE, CodeBuild, and IAM resources
110119
- Test template deployment in AWS (optional, can be done manually)
@@ -205,9 +214,4 @@
205214
- Create migration checklist for workshop maintainers
206215
- _Requirements: 5.9_
207216

208-
- [ ] 1000.3 Lambda consolidation (future task)
209-
- Consolidate existing Python/JavaScript Lambda functions into single Java handler
210-
- Implement resource type routing for DatabaseSetup, InstanceLauncher, PasswordRetriever
211-
- Maintain identical functionality and interfaces to existing functions
212-
- Package all handlers into single deployment artifact
213-
- _Requirements: 5.8_
217+
name or just value?

infra/cdk/.gitignore

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
.classpath.txt
2+
target
3+
.classpath
4+
.project
5+
.idea
6+
.settings
7+
.vscode
8+
*.iml
9+
10+
# CDK asset staging directory
11+
.cdk.staging
12+
cdk.out
13+

infra/cdk/README.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
# Welcome to your CDK Java project!
2+
3+
This is a blank project for CDK development with Java.
4+
5+
The `cdk.json` file tells the CDK Toolkit how to execute your app.
6+
7+
It is a [Maven](https://maven.apache.org/) based project, so you can open this project with any Maven compatible Java IDE to build and run tests.
8+
9+
## Useful commands
10+
11+
* `mvn package` compile and run tests
12+
* `cdk ls` list all stacks in the app
13+
* `cdk synth` emits the synthesized CloudFormation template
14+
* `cdk deploy` deploy this stack to your default AWS account/region
15+
* `cdk diff` compare deployed stack with current state
16+
* `cdk docs` open CDK documentation
17+
18+
Enjoy!

0 commit comments

Comments
 (0)