Skip to content

Commit ff87ac5

Browse files
author
Yuriy Bezsonov
committed
refactor(infra): simplify Unicorn construct and reorganize CloudFormation templates
1 parent 4461b88 commit ff87ac5

File tree

7 files changed

+280
-102
lines changed

7 files changed

+280
-102
lines changed

infra/cdk/src/main/java/sample/com/WorkshopStack.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ public WorkshopStack(final Construct scope, final String id, final StackProps pr
120120
// Unicorn construct: Roles + DB Setup (uses unicorn* naming for workshop content compatibility)
121121
Unicorn unicorn = new Unicorn(this, "Unicorn", Unicorn.UnicornProps.builder()
122122
.vpc(vpc.getVpc())
123-
.eksRolesEnabled(true)
124-
.ecsRolesEnabled(false)
125123
.database(database)
126124
.workshopBucket(workshopBucket.getBucket())
127125
.build());

infra/cdk/src/main/java/sample/com/constructs/Unicorn.java

Lines changed: 2 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,10 @@ public Unicorn(final Construct scope, final String id, final UnicornProps props)
7070
.build();
7171

7272
// === EKS ROLES ===
73-
if (props.isEksRolesEnabled()) {
74-
createEksRoles(props);
75-
}
73+
createEksRoles(props);
7674

7775
// === ECS ROLES ===
78-
if (props.isEcsRolesEnabled()) {
79-
createEcsRoles(props);
80-
}
76+
createEcsRoles(props);
8177

8278
// === DATABASE SETUP ===
8379
if (props.getDatabase() != null) {
@@ -278,15 +274,11 @@ public Role getEcsTaskExecutionRole() {
278274

279275
// Props class
280276
public static class UnicornProps {
281-
private final boolean eksRolesEnabled;
282-
private final boolean ecsRolesEnabled;
283277
private final Database database;
284278
private final IBucket workshopBucket;
285279
private final software.amazon.awscdk.services.ec2.IVpc vpc;
286280

287281
private UnicornProps(Builder builder) {
288-
this.eksRolesEnabled = builder.eksRolesEnabled;
289-
this.ecsRolesEnabled = builder.ecsRolesEnabled;
290282
this.database = builder.database;
291283
this.workshopBucket = builder.workshopBucket;
292284
this.vpc = builder.vpc;
@@ -296,14 +288,6 @@ public static Builder builder() {
296288
return new Builder();
297289
}
298290

299-
public boolean isEksRolesEnabled() {
300-
return eksRolesEnabled;
301-
}
302-
303-
public boolean isEcsRolesEnabled() {
304-
return ecsRolesEnabled;
305-
}
306-
307291
public Database getDatabase() {
308292
return database;
309293
}
@@ -317,22 +301,10 @@ public software.amazon.awscdk.services.ec2.IVpc getVpc() {
317301
}
318302

319303
public static class Builder {
320-
private boolean eksRolesEnabled = false;
321-
private boolean ecsRolesEnabled = false;
322304
private Database database;
323305
private IBucket workshopBucket;
324306
private software.amazon.awscdk.services.ec2.IVpc vpc;
325307

326-
public Builder eksRolesEnabled(boolean eksRolesEnabled) {
327-
this.eksRolesEnabled = eksRolesEnabled;
328-
return this;
329-
}
330-
331-
public Builder ecsRolesEnabled(boolean ecsRolesEnabled) {
332-
this.ecsRolesEnabled = ecsRolesEnabled;
333-
return this;
334-
}
335-
336308
public Builder database(Database database) {
337309
this.database = database;
338310
return this;

infra/cfn/base-stack.yaml

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -676,18 +676,6 @@ Resources:
676676
Fn::GetAtt:
677677
- IdeInstanceLauncherFunction803C5A2A
678678
- Arn
679-
InstanceName: ide
680-
IamInstanceProfileArn:
681-
Fn::GetAtt:
682-
- IdeInstanceProfile61B92038
683-
- Arn
684-
VolumeSize: "50"
685-
SubnetIds:
686-
Fn::Join:
687-
- ""
688-
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
689-
- ","
690-
- Ref: VpcPublicSubnet2SubnetA811849C
691679
SecurityGroupIds:
692680
Fn::Join:
693681
- ""
@@ -698,8 +686,19 @@ Resources:
698686
- Fn::GetAtt:
699687
- IdeInternalSecurityGroupB0A5D76B
700688
- GroupId
701-
ImageId:
702-
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61x8664C96584B6F00A464EAD1953AFF4B05118Parameter
689+
SubnetIds:
690+
Fn::Join:
691+
- ""
692+
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
693+
- ","
694+
- Ref: VpcPublicSubnet2SubnetA811849C
695+
VolumeSize: "50"
696+
IamInstanceProfileArn:
697+
Fn::GetAtt:
698+
- IdeInstanceProfile61B92038
699+
- Arn
700+
InstanceName: ide
701+
InstanceTypes: m6i.xlarge,m5.xlarge,m6a.xlarge,m7i-flex.xlarge,m7a.xlarge,t3.xlarge
703702
UserData:
704703
Fn::Base64:
705704
Fn::Join:
@@ -836,7 +835,8 @@ Resources:
836835
"
837836
exit 1
838837
fi
839-
InstanceTypes: m6i.xlarge,m5.xlarge,m6a.xlarge,m7i-flex.xlarge,m7a.xlarge,t3.xlarge
838+
ImageId:
839+
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61x8664C96584B6F00A464EAD1953AFF4B05118Parameter
840840
UpdateReplacePolicy: Delete
841841
DeletionPolicy: Delete
842842
IdeEipAssociationDFF81215:

infra/cfn/java-ai-agents-stack.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ Resources:
760760
Fn::GetAtt:
761761
- IdeInstanceLauncherFunction803C5A2A
762762
- Arn
763+
InstanceName: ide
763764
IamInstanceProfileArn:
764765
Fn::GetAtt:
765766
- IdeInstanceProfile61B92038
@@ -920,7 +921,6 @@ Resources:
920921
exit 1
921922
fi
922923
InstanceTypes: m6i.xlarge,m5.xlarge,m6a.xlarge,m7i-flex.xlarge,m7a.xlarge,t3.xlarge
923-
InstanceName: ide
924924
UpdateReplacePolicy: Delete
925925
DeletionPolicy: Delete
926926
IdeEipAssociationDFF81215:

infra/cfn/java-on-amazon-eks-stack.yaml

Lines changed: 123 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -780,13 +780,6 @@ Resources:
780780
Fn::GetAtt:
781781
- IdeInstanceLauncherFunction803C5A2A
782782
- Arn
783-
SubnetIds:
784-
Fn::Join:
785-
- ""
786-
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
787-
- ","
788-
- Ref: VpcPublicSubnet2SubnetA811849C
789-
VolumeSize: "50"
790783
IamInstanceProfileArn:
791784
Fn::GetAtt:
792785
- IdeInstanceProfile61B92038
@@ -941,6 +934,13 @@ Resources:
941934
- Fn::GetAtt:
942935
- IdeInternalSecurityGroupB0A5D76B
943936
- GroupId
937+
SubnetIds:
938+
Fn::Join:
939+
- ""
940+
- - Ref: VpcPublicSubnet1Subnet8E8DEDC0
941+
- ","
942+
- Ref: VpcPublicSubnet2SubnetA811849C
943+
VolumeSize: "50"
944944
UpdateReplacePolicy: Delete
945945
DeletionPolicy: Delete
946946
IdeEipAssociationDFF81215:
@@ -1319,12 +1319,12 @@ Resources:
13191319
Environment:
13201320
ComputeType: BUILD_GENERAL1_MEDIUM
13211321
EnvironmentVariables:
1322-
- Name: TEMPLATE_TYPE
1323-
Type: PLAINTEXT
1324-
Value: java-on-amazon-eks
13251322
- Name: GIT_BRANCH
13261323
Type: PLAINTEXT
13271324
Value: new-ws-infra
1325+
- Name: TEMPLATE_TYPE
1326+
Type: PLAINTEXT
1327+
Value: java-on-amazon-eks
13281328
Image: aws/codebuild/amazonlinux2-x86_64-standard:5.0
13291329
ImagePullCredentialsType: CODEBUILD
13301330
PrivilegedMode: false
@@ -1529,12 +1529,12 @@ Resources:
15291529
Description: workshop-setup build complete
15301530
EventPattern:
15311531
detail:
1532-
project-name:
1533-
- Ref: CodeBuildProjectA0FF5539
15341532
build-status:
15351533
- SUCCEEDED
15361534
- FAILED
15371535
- STOPPED
1536+
project-name:
1537+
- Ref: CodeBuildProjectA0FF5539
15381538
detail-type:
15391539
- CodeBuild Build State Change
15401540
source:
@@ -1566,13 +1566,13 @@ Resources:
15661566
Fn::GetAtt:
15671567
- CodeBuildStartLambdaFunction8349284F
15681568
- Arn
1569-
ProjectName:
1570-
Ref: CodeBuildProjectA0FF5539
1571-
ContentHash: "1766758207993"
1569+
ContentHash: "1766773872588"
15721570
CodeBuildIamRoleArn:
15731571
Fn::GetAtt:
15741572
- CodeBuildRoleE9A44575
15751573
- Arn
1574+
ProjectName:
1575+
Ref: CodeBuildProjectA0FF5539
15761576
DependsOn:
15771577
- CodeBuildCompleteRuleAllowEventRuleWorkshopStackCodeBuildReportLambdaFunctionD77C60919E0B0C89
15781578
- CodeBuildCompleteRuleEE9277E8
@@ -1884,7 +1884,7 @@ Resources:
18841884
- Ref: AWS::AccountId
18851885
- "-"
18861886
- Ref: AWS::Region
1887-
- "-20251226151008"
1887+
- "-20251226193112"
18881888
PublicAccessBlockConfiguration:
18891889
BlockPublicAcls: true
18901890
BlockPublicPolicy: true
@@ -2465,6 +2465,110 @@ Resources:
24652465
PolicyName: UnicornUnicornStoreEksPodRoleDefaultPolicy0D527B93
24662466
Roles:
24672467
- Ref: UnicornUnicornStoreEksPodRoleB15D12B7
2468+
UnicornUnicornStoreEcsInfrastructureRoleEDFFC1E6:
2469+
Type: AWS::IAM::Role
2470+
Properties:
2471+
AssumeRolePolicyDocument:
2472+
Statement:
2473+
- Action: sts:AssumeRole
2474+
Effect: Allow
2475+
Principal:
2476+
Service: ecs.amazonaws.com
2477+
Version: "2012-10-17"
2478+
Description: ECS infrastructure role for Express Mode services
2479+
ManagedPolicyArns:
2480+
- Fn::Join:
2481+
- ""
2482+
- - "arn:"
2483+
- Ref: AWS::Partition
2484+
- :iam::aws:policy/service-role/AmazonECSInfrastructureRoleforExpressGatewayServices
2485+
Path: /service-role/
2486+
RoleName: unicornstore-ecs-infrastructure-role
2487+
UnicornUnicornStoreEcsTaskExecutionRoleC2148AE8:
2488+
Type: AWS::IAM::Role
2489+
Properties:
2490+
AssumeRolePolicyDocument:
2491+
Statement:
2492+
- Action: sts:AssumeRole
2493+
Effect: Allow
2494+
Principal:
2495+
Service: ecs-tasks.amazonaws.com
2496+
Version: "2012-10-17"
2497+
Description: ECS task execution role for pulling images and injecting secrets
2498+
ManagedPolicyArns:
2499+
- Fn::Join:
2500+
- ""
2501+
- - "arn:"
2502+
- Ref: AWS::Partition
2503+
- :iam::aws:policy/service-role/AmazonECSTaskExecutionRolePolicy
2504+
Path: /service-role/
2505+
RoleName: unicornstore-ecs-task-execution-role
2506+
UnicornUnicornStoreEcsTaskExecutionRoleDefaultPolicy3FC9EFEE:
2507+
Type: AWS::IAM::Policy
2508+
Properties:
2509+
PolicyDocument:
2510+
Statement:
2511+
- Action: logs:CreateLogGroup
2512+
Effect: Allow
2513+
Resource: "*"
2514+
- Action:
2515+
- secretsmanager:DescribeSecret
2516+
- secretsmanager:GetSecretValue
2517+
Effect: Allow
2518+
Resource:
2519+
Ref: DatabaseSecret3B817195
2520+
- Action:
2521+
- ssm:DescribeParameters
2522+
- ssm:GetParameter
2523+
- ssm:GetParameterHistory
2524+
- ssm:GetParameters
2525+
Effect: Allow
2526+
Resource:
2527+
Fn::Join:
2528+
- ""
2529+
- - "arn:"
2530+
- Ref: AWS::Partition
2531+
- ":ssm:"
2532+
- Ref: AWS::Region
2533+
- ":"
2534+
- Ref: AWS::AccountId
2535+
- :parameter/
2536+
- Ref: DatabaseConnectionString52D1E98E
2537+
Version: "2012-10-17"
2538+
PolicyName: UnicornUnicornStoreEcsTaskExecutionRoleDefaultPolicy3FC9EFEE
2539+
Roles:
2540+
- Ref: UnicornUnicornStoreEcsTaskExecutionRoleC2148AE8
2541+
UnicornUnicornStoreEcsTaskRoleD7FBB789:
2542+
Type: AWS::IAM::Role
2543+
Properties:
2544+
AssumeRolePolicyDocument:
2545+
Statement:
2546+
- Action: sts:AssumeRole
2547+
Effect: Allow
2548+
Principal:
2549+
Service: ecs-tasks.amazonaws.com
2550+
Version: "2012-10-17"
2551+
Description: ECS task role for application runtime permissions
2552+
Path: /service-role/
2553+
RoleName: unicornstore-ecs-task-role
2554+
UnicornUnicornStoreEcsTaskRoleDefaultPolicy477138EA:
2555+
Type: AWS::IAM::Policy
2556+
Properties:
2557+
PolicyDocument:
2558+
Statement:
2559+
- Action: xray:PutTraceSegments
2560+
Effect: Allow
2561+
Resource: "*"
2562+
- Action: events:PutEvents
2563+
Effect: Allow
2564+
Resource:
2565+
Fn::GetAtt:
2566+
- UnicornUnicornEventBusB728845C
2567+
- Arn
2568+
Version: "2012-10-17"
2569+
PolicyName: UnicornUnicornStoreEcsTaskRoleDefaultPolicy477138EA
2570+
Roles:
2571+
- Ref: UnicornUnicornStoreEcsTaskRoleD7FBB789
24682572
UnicornUnicornStoreDatabaseSetupFunctionServiceRole61942171:
24692573
Type: AWS::IAM::Role
24702574
Properties:
@@ -2717,6 +2821,9 @@ Resources:
27172821
Fn::GetAtt:
27182822
- UnicornUnicornStoreDatabaseSetupFunction04E12F8B
27192823
- Arn
2824+
SqlStatements: |
2825+
CREATE TABLE IF NOT EXISTS unicorns(id TEXT DEFAULT gen_random_uuid() PRIMARY KEY, name TEXT, age TEXT, size TEXT, type TEXT);
2826+
CREATE EXTENSION IF NOT EXISTS vector;
27202827
SecretName:
27212828
Fn::Join:
27222829
- "-"
@@ -2747,9 +2854,6 @@ Resources:
27472854
- Fn::Split:
27482855
- ":"
27492856
- Ref: DatabaseSecret3B817195
2750-
SqlStatements: |
2751-
CREATE TABLE IF NOT EXISTS unicorns(id TEXT DEFAULT gen_random_uuid() PRIMARY KEY, name TEXT, age TEXT, size TEXT, type TEXT);
2752-
CREATE EXTENSION IF NOT EXISTS vector;
27532857
DependsOn:
27542858
- DatabaseClusterDatabaseWriterF4C0B9A6
27552859
- DatabaseCluster5B53A178

0 commit comments

Comments
 (0)