Skip to content

Commit

Permalink
Custom exit command as cfn-signal is at a different path from the AWS…
Browse files Browse the repository at this point in the history
… CDK default
  • Loading branch information
akash1810 committed Jul 16, 2024
1 parent 1363768 commit 0fb7d81
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
13 changes: 10 additions & 3 deletions cdk/lib/__snapshots__/cdk-playground.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -1754,7 +1754,13 @@ Object {
"#!/bin/bash
function exitTrap(){
exitCode=$?
/opt/aws/bin/cfn-signal --stack CdkPlayground --resource AutoScalingGroupCdkplaygroundASGD6E49F0F --region eu-west-1 -e $exitCode || echo 'Failed to send Cloudformation Signal'
cfn-signal --stack ",
Object {
"Ref": "AWS::StackId",
},
" --resource AutoScalingGroupCdkplaygroundASGD6E49F0F --region eu-west-1 --exit-code $exitCode || echo 'Failed to send Cloudformation Signal'
}
trap exitTrap EXIT
mkdir -p $(dirname '/cdk-playground/cdk-playground-TEST.deb')
Expand All @@ -1769,13 +1775,14 @@ dpkg -i /cdk-playground/cdk-playground-TEST.deb
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id -H \\"X-aws-ec2-metadata-token: $TOKEN\\")
until [ \\"$state\\" == \\"\\\\\\"healthy\\\\\\"\\" ]; do
echo \\"Instance not yet healthy within target group. Sleeping for 10 seconds.\\";
sleep 10;
state=$(aws elbv2 describe-target-health --target-group-arn ",
Object {
"Ref": "TargetGroupCdkplayground7A453FC2",
},
" --region eu-west-1 --targets Id=$INSTANCE_ID,Port=9000 --query \\"TargetHealthDescriptions[0].TargetHealth.State\\");
echo \\"Current state $state. Sleeping for 10 seconds...\\";
sleep 10;
echo \\"Current state $state.\\";
done
",
],
Expand Down
29 changes: 22 additions & 7 deletions cdk/lib/cdk-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import {
InstanceClass,
InstanceSize,
InstanceType,
UserData,

Check warning on line 28 in cdk/lib/cdk-playground.ts

View workflow job for this annotation

GitHub Actions / CI

'UserData' is defined but never used
} from 'aws-cdk-lib/aws-ec2';
import { Runtime } from 'aws-cdk-lib/aws-lambda';

Expand Down Expand Up @@ -99,26 +100,40 @@ export class CdkPlayground extends GuStack {
timeout: 'PT15M',
},
};
// aws elbv2 describe-target-health --targets Id=i-03bd1b162e8ef3187,Port=9000

const { region } = this;
const asg = autoScalingGroup.node.defaultChild as CfnAutoScalingGroup;
const asgLogicalId = asg.logicalId;

const portNumber = 9000;

autoScalingGroup.userData.addCommands(
`
TOKEN=$(curl -X PUT http://169.254.169.254/latest/api/token -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")
INSTANCE_ID=$(curl -s http://169.254.169.254/latest/meta-data/instance-id -H "X-aws-ec2-metadata-token: $TOKEN")
until [ "$state" == "\\"healthy\\"" ]; do
echo "Instance not yet healthy within target group. Sleeping for 10 seconds.";
sleep 10;
state=$(aws elbv2 describe-target-health \
--target-group-arn ${targetGroup.targetGroupArn} \
--region ${this.region} \
--targets Id=$INSTANCE_ID,Port=9000 \
--region ${region} \
--targets Id=$INSTANCE_ID,Port=${portNumber} \
--query "TargetHealthDescriptions[0].TargetHealth.State");
echo "Current state $state. Sleeping for 10 seconds...";
sleep 10;
echo "Current state $state.";
done
`,
);
autoScalingGroup.userData.addSignalOnExitCommand(autoScalingGroup);

const asg = autoScalingGroup.node.defaultChild as CfnAutoScalingGroup;
autoScalingGroup.userData.addOnExitCommands(
`
cfn-signal --stack ${this.stackId} \
--resource ${asgLogicalId} \
--region ${region} \
--exit-code $exitCode || echo 'Failed to send Cloudformation Signal'
`,
);

asg.cfnOptions.creationPolicy = createPolicy;

new GuCname(this, 'EC2AppDNS', {
Expand Down

0 comments on commit 0fb7d81

Please sign in to comment.