Skip to content

Commit 8bdc923

Browse files
author
Yuriy Bezsonov
committed
WIP
1 parent 3896d5c commit 8bdc923

File tree

5 files changed

+66
-44
lines changed

5 files changed

+66
-44
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public WorkshopStack(final Construct scope, final String id, final StackProps pr
5858
// CodeBuild for workshop setup
5959
CodeBuild codeBuild = new CodeBuild(this, "CodeBuild",
6060
CodeBuild.CodeBuildProps.builder()
61-
.projectName(templateType + "-setup")
61+
.projectName("workshop-setup")
6262
.vpc(vpc.getVpc())
6363
.environmentVariables(Map.of(
6464
"STACK_NAME", Aws.STACK_NAME,

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,12 +133,12 @@ public CodeBuild(final Construct scope, final String id, final CodeBuildProps pr
133133

134134
// Create start build Lambda function
135135
var startLambda = new Lambda(this, "StartLambda",
136-
"/lambda/codebuild-start.py", "ide-codebuild-start", Duration.minutes(2), lambdaRole);
136+
"/lambda/codebuild-start.py", "setup-codebuild-start", Duration.minutes(2), lambdaRole);
137137
Function startBuildFunction = startLambda.getFunction();
138138

139139
// Create report build Lambda function
140140
var reportLambda = new Lambda(this, "ReportLambda",
141-
"/lambda/codebuild-report.py", "ide-codebuild-report", Duration.minutes(2), lambdaRole);
141+
"/lambda/codebuild-report.py", "setup-codebuild-report", Duration.minutes(2), lambdaRole);
142142
Function reportBuildFunction = reportLambda.getFunction();
143143

144144
// Create EventBridge rule for build completion

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ public Ide(final Construct scope, final String id, final IdeProps props) {
157157
lambdaRole.addToPolicy(lambdaPermissions);
158158

159159
// Set up wait condition handle for bootstrap completion (needed for User Data)
160-
var waitHandle = CfnWaitConditionHandle.Builder.create(this, "IdeBootstrapWaitConditionHandle")
160+
var waitHandle = CfnWaitConditionHandle.Builder.create(this, "BootstrapWaitConditionHandle")
161161
.build();
162162

163163
// Create CloudFront prefix list lookup Lambda function
@@ -262,6 +262,7 @@ public Ide(final Construct scope, final String id, final IdeProps props) {
262262
STACK_NAME="%s"
263263
AWS_REGION="%s"
264264
TEMPLATE_TYPE="%s"
265+
WAIT_CONDITION_HANDLE_URL="%s"
265266
266267
# Setup logging
267268
LOG_GROUP_NAME="ide-bootstrap-$(date +%%Y%%m%%d-%%H%%M%%S)"
@@ -344,24 +345,29 @@ public Ide(final Construct scope, final String id, final IdeProps props) {
344345
345346
echo "Executing full bootstrap script..."
346347
# Run bootstrap script as root from the cloned directory
347-
if bash -c "cd /home/ec2-user/java-on-aws && infra/scripts/ide/bootstrap.sh '$GIT_BRANCH' '$STACK_NAME' '$TEMPLATE_TYPE'"; then
348+
if bash -c "cd /home/ec2-user/java-on-aws && WAIT_CONDITION_HANDLE_URL='%s' infra/scripts/ide/bootstrap.sh '$GIT_BRANCH' '$STACK_NAME' '$TEMPLATE_TYPE'"; then
348349
echo "Bootstrap completed successfully"
349-
/opt/aws/bin/cfn-signal -e 0 --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION"
350+
/opt/aws/bin/cfn-signal -e 0 '%s'
350351
else
351352
echo "FATAL: Bootstrap script failed"
352-
/opt/aws/bin/cfn-signal -e 1 --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION"
353+
/opt/aws/bin/cfn-signal -e 1 '%s'
353354
exit 1
354355
fi
355356
else
356357
echo "FATAL: Could not clone repository"
357-
/opt/aws/bin/cfn-signal -e 1 --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION"
358+
/opt/aws/bin/cfn-signal -e 1 '%s'
358359
exit 1
359360
fi
360361
""",
361362
gitBranch,
362363
Aws.STACK_NAME,
363364
Aws.REGION,
364-
templateType
365+
templateType,
366+
waitHandle.getRef(),
367+
waitHandle.getRef(),
368+
waitHandle.getRef(),
369+
waitHandle.getRef(),
370+
waitHandle.getRef()
365371
);
366372

367373
userData.addCommands(userDataContent);
@@ -426,7 +432,7 @@ public Ide(final Construct scope, final String id, final IdeProps props) {
426432
distribution.applyRemovalPolicy(RemovalPolicy.DESTROY);
427433
distribution.getNode().addDependency(ipAssociation);
428434

429-
var waitCondition = CfnWaitCondition.Builder.create(this, "IdeBootstrapWaitCondition")
435+
var waitCondition = CfnWaitCondition.Builder.create(this, "BootstrapWaitCondition")
430436
.count(1)
431437
.handle(waitHandle.getRef())
432438
.timeout(String.valueOf(props.getBootstrapTimeoutMinutes() * 60))

infra/scripts/ide/bootstrap.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ export TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-e
7575
export AWS_REGION=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/dynamic/instance-identity/document | grep region | awk -F\" '{print $4}')
7676

7777
# Now that we have AWS_REGION, set up error trap for CloudFormation signaling
78-
trap 'echo "Bootstrap failed at line $LINENO"; /opt/aws/bin/cfn-signal -e 1 --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION" 2>/dev/null || true; exit 1' ERR
78+
trap 'echo "Bootstrap failed at line $LINENO"; /opt/aws/bin/cfn-signal -e 1 "$WAIT_CONDITION_HANDLE_URL" 2>/dev/null || true; exit 1' ERR
7979

8080
export EC2_PRIVATE_IP=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/local-ipv4)
8181
export EC2_DOMAIN=$(curl -H "X-aws-ec2-metadata-token: $TOKEN" -s http://169.254.169.254/latest/meta-data/public-hostname)
@@ -143,4 +143,4 @@ fi
143143
echo "$(date '+%Y-%m-%d %H:%M:%S') - Bootstrap completed successfully"
144144

145145
# Signal CloudFormation completion
146-
/opt/aws/bin/cfn-signal -e $? --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION"
146+
/opt/aws/bin/cfn-signal -e $? "$WAIT_CONDITION_HANDLE_URL"

infra/workshop-template.yaml

Lines changed: 48 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,7 @@ Resources:
417417
PolicyName: IdeIdeLambdaRoleDefaultPolicy6F800745
418418
Roles:
419419
- Ref: IdeIdeLambdaRoleCC8748CC
420-
IdeIdeBootstrapWaitConditionHandleEA0A1D15:
420+
IdeBootstrapWaitConditionHandleD7141CA8:
421421
Type: AWS::CloudFormation::WaitConditionHandle
422422
IdePrefixListLookupFunction9E5A63DE:
423423
Type: AWS::Lambda::Function
@@ -714,30 +714,19 @@ Resources:
714714
Fn::GetAtt:
715715
- IdeInstanceLauncherFunction803C5A2A
716716
- Arn
717-
InstanceName: ide
718-
IamInstanceProfileArn:
719-
Fn::GetAtt:
720-
- IdeIdeInstanceProfile8BD997EA
721-
- Arn
722-
VolumeSize: "50"
723717
SubnetIds:
724718
Fn::Join:
725719
- ""
726720
- - Ref: VpcWorkshopVpcPublicSubnet1SubnetBCB45C45
727721
- ","
728722
- Ref: VpcWorkshopVpcPublicSubnet2SubnetF8F9426F
729-
SecurityGroupIds:
730-
Fn::Join:
731-
- ""
732-
- - Fn::GetAtt:
733-
- IdeIdeSecurityGroup5C503C8A
734-
- GroupId
735-
- ","
736-
- Fn::GetAtt:
737-
- IdeIdeInternalSecurityGroupD5D3B421
738-
- GroupId
739-
ImageId:
740-
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61x8664C96584B6F00A464EAD1953AFF4B05118Parameter
723+
VolumeSize: "50"
724+
IamInstanceProfileArn:
725+
Fn::GetAtt:
726+
- IdeIdeInstanceProfile8BD997EA
727+
- Arn
728+
InstanceName: ide
729+
InstanceTypes: m5.xlarge,m6i.xlarge,t3.xlarge
741730
UserData:
742731
Fn::Base64:
743732
Fn::Join:
@@ -758,9 +747,13 @@ Resources:
758747
"
759748
AWS_REGION="
760749
- Ref: AWS::Region
761-
- |
750+
- |-
762751
"
763752
TEMPLATE_TYPE="base"
753+
WAIT_CONDITION_HANDLE_URL="
754+
- Ref: IdeBootstrapWaitConditionHandleD7141CA8
755+
- |-
756+
"
764757
765758
# Setup logging
766759
LOG_GROUP_NAME="ide-bootstrap-$(date +%Y%m%d-%H%M%S)"
@@ -843,20 +836,43 @@ Resources:
843836
844837
echo "Executing full bootstrap script..."
845838
# Run bootstrap script as root from the cloned directory
846-
if bash -c "cd /home/ec2-user/java-on-aws && infra/scripts/ide/bootstrap.sh '$GIT_BRANCH' '$STACK_NAME' '$TEMPLATE_TYPE'"; then
839+
if bash -c "cd /home/ec2-user/java-on-aws && WAIT_CONDITION_HANDLE_URL='
840+
- Ref: IdeBootstrapWaitConditionHandleD7141CA8
841+
- |-
842+
' infra/scripts/ide/bootstrap.sh '$GIT_BRANCH' '$STACK_NAME' '$TEMPLATE_TYPE'"; then
847843
echo "Bootstrap completed successfully"
848-
/opt/aws/bin/cfn-signal -e 0 --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION"
844+
/opt/aws/bin/cfn-signal -e 0 '
845+
- Ref: IdeBootstrapWaitConditionHandleD7141CA8
846+
- |-
847+
'
849848
else
850849
echo "FATAL: Bootstrap script failed"
851-
/opt/aws/bin/cfn-signal -e 1 --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION"
850+
/opt/aws/bin/cfn-signal -e 1 '
851+
- Ref: IdeBootstrapWaitConditionHandleD7141CA8
852+
- |-
853+
'
852854
exit 1
853855
fi
854856
else
855857
echo "FATAL: Could not clone repository"
856-
/opt/aws/bin/cfn-signal -e 1 --stack "$STACK_NAME" --resource IdeBootstrapWaitCondition --region "$AWS_REGION"
858+
/opt/aws/bin/cfn-signal -e 1 '
859+
- Ref: IdeBootstrapWaitConditionHandleD7141CA8
860+
- |
861+
'
857862
exit 1
858863
fi
859-
InstanceTypes: m5.xlarge,m6i.xlarge,t3.xlarge
864+
ImageId:
865+
Ref: SsmParameterValueawsserviceamiamazonlinuxlatestal2023amikernel61x8664C96584B6F00A464EAD1953AFF4B05118Parameter
866+
SecurityGroupIds:
867+
Fn::Join:
868+
- ""
869+
- - Fn::GetAtt:
870+
- IdeIdeSecurityGroup5C503C8A
871+
- GroupId
872+
- ","
873+
- Fn::GetAtt:
874+
- IdeIdeInternalSecurityGroupD5D3B421
875+
- GroupId
860876
UpdateReplacePolicy: Delete
861877
DeletionPolicy: Delete
862878
IdeIdeEipAssociation6C6C215D:
@@ -938,12 +954,12 @@ Resources:
938954
- IdeIdeEipAssociation6C6C215D
939955
UpdateReplacePolicy: Delete
940956
DeletionPolicy: Delete
941-
IdeIdeBootstrapWaitCondition94599134:
957+
IdeBootstrapWaitConditionE4059F8E:
942958
Type: AWS::CloudFormation::WaitCondition
943959
Properties:
944960
Count: 1
945961
Handle:
946-
Ref: IdeIdeBootstrapWaitConditionHandleEA0A1D15
962+
Ref: IdeBootstrapWaitConditionHandleD7141CA8
947963
Timeout: "1800"
948964
DependsOn:
949965
- IdeIdeEC2InstanceResource17A0C9F7
@@ -1119,7 +1135,7 @@ Resources:
11191135
ImagePullCredentialsType: CODEBUILD
11201136
PrivilegedMode: false
11211137
Type: LINUX_CONTAINER
1122-
Name: base-setup
1138+
Name: workshop-setup
11231139
ServiceRole:
11241140
Fn::GetAtt:
11251141
- CodeBuildCodeBuildRoleBA9C6D5C
@@ -1238,7 +1254,7 @@ Resources:
12381254
responseData = {'Error': tb_err}
12391255
12401256
cfnresponse.send(event, context, status, responseData, physical_id)
1241-
FunctionName: ide-codebuild-start
1257+
FunctionName: setup-codebuild-start
12421258
Handler: index.lambda_handler
12431259
Role:
12441260
Fn::GetAtt:
@@ -1304,7 +1320,7 @@ Resources:
13041320
'error': str(e)
13051321
})
13061322
}
1307-
FunctionName: ide-codebuild-report
1323+
FunctionName: setup-codebuild-report
13081324
Handler: index.lambda_handler
13091325
Role:
13101326
Fn::GetAtt:
@@ -1318,7 +1334,7 @@ Resources:
13181334
CodeBuildBuildCompleteRule06AAF17D:
13191335
Type: AWS::Events::Rule
13201336
Properties:
1321-
Description: base-setup build complete
1337+
Description: workshop-setup build complete
13221338
EventPattern:
13231339
detail:
13241340
project-name:
@@ -1360,11 +1376,11 @@ Resources:
13601376
- Arn
13611377
ProjectName:
13621378
Ref: CodeBuildProjectA0FF5539
1379+
ContentHash: "1765692209597"
13631380
CodeBuildIamRoleArn:
13641381
Fn::GetAtt:
13651382
- CodeBuildCodeBuildRoleBA9C6D5C
13661383
- Arn
1367-
ContentHash: "1765690723450"
13681384
DependsOn:
13691385
- CodeBuildBuildCompleteRuleAllowEventRuleWorkshopStackCodeBuildReportLambdaFunctionD77C6091DA4A4BD8
13701386
- CodeBuildBuildCompleteRule06AAF17D

0 commit comments

Comments
 (0)