Skip to content

Commit

Permalink
Poll for instance health on the ALB
Browse files Browse the repository at this point in the history
  • Loading branch information
akash1810 committed Jul 16, 2024
1 parent f5b01f0 commit 2b7eb90
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
14 changes: 6 additions & 8 deletions cdk/lib/__snapshots__/cdk-playground.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": "elasticloadbalancing:DescribeInstanceHealth",
"Action": "elasticloadbalancing:DescribeTargetHealth",
"Effect": "Allow",
"Resource": "*",
},
Expand Down Expand Up @@ -1765,15 +1765,13 @@ aws s3 cp 's3://",
"/playground/PROD/cdk-playground/cdk-playground-TEST.deb' '/cdk-playground/cdk-playground-TEST.deb'
dpkg -i /cdk-playground/cdk-playground-TEST.deb
until [ \\"$state\\" == \\"\\\\\\"InService\\\\\\"\\" ]; do
state=$(aws --region eu-west-1 elb describe-instance-health --load-balancer-name ",
until [ \\"$state\\" == \\"\\\\\\"healthy\\\\\\"\\" ]; do
state=$(aws elbv2 describe-target-health --target-group-arn ",
Object {
"Fn::GetAtt": Array [
"LoadBalancerCdkplayground7C6B4D97",
"LoadBalancerName",
],
"Ref": "TargetGroupCdkplayground7A453FC2",
},
" --instances $(curl -s http://169.254.169.254/latest/meta-data/instance-id) --query InstanceStates[0].State);
" --region eu-west-1 --targets Id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id),Port=9000 --query \\"TargetHealthDescriptions[0].TargetHealth.State\\");
echo \\"Current state $state. Sleeping for 10 seconds...\\";
sleep 10;
done
",
Expand Down
87 changes: 46 additions & 41 deletions cdk/lib/cdk-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,43 +49,46 @@ export class CdkPlayground extends GuStack {

const buildNumber = process.env.GITHUB_RUN_NUMBER ?? 'DEV';

const { loadBalancer, autoScalingGroup } = new GuPlayApp(this, {
app: ec2App,
instanceType: InstanceType.of(InstanceClass.T4G, InstanceSize.MICRO),
access: { scope: AccessScope.PUBLIC },
userData: {
distributable: {
fileName: `${ec2App}-${buildNumber}.deb`,
executionStatement: `dpkg -i /${ec2App}/${ec2App}-${buildNumber}.deb`,
const { loadBalancer, autoScalingGroup, targetGroup } = new GuPlayApp(
this,
{
app: ec2App,
instanceType: InstanceType.of(InstanceClass.T4G, InstanceSize.MICRO),
access: { scope: AccessScope.PUBLIC },
userData: {
distributable: {
fileName: `${ec2App}-${buildNumber}.deb`,
executionStatement: `dpkg -i /${ec2App}/${ec2App}-${buildNumber}.deb`,
},
},
certificateProps: {
domainName: ec2AppDomainName,
},
monitoringConfiguration: { noMonitoring: true },
scaling: {
minimumInstances: 2,
maximumInstances: 4,
},
applicationLogging: {
enabled: true,
systemdUnitName: 'cdk-playground',
},
imageRecipe: 'developerPlayground-arm64-java11',
updatePolicy: UpdatePolicy.replacingUpdate(),
roleConfiguration: {
additionalPolicies: [
new GuAllowPolicy(this, 'SignalResourePolicy', {
actions: ['cloudformation:SignalResource'],
resources: ['*'],
}),
new GuAllowPolicy(this, 'DescribeInstanceHealthPolicy', {
actions: ['elasticloadbalancing:DescribeTargetHealth'],
resources: ['*'],
}),
],
},
},
certificateProps: {
domainName: ec2AppDomainName,
},
monitoringConfiguration: { noMonitoring: true },
scaling: {
minimumInstances: 2,
maximumInstances: 4,
},
applicationLogging: {
enabled: true,
systemdUnitName: 'cdk-playground',
},
imageRecipe: 'developerPlayground-arm64-java11',
updatePolicy: UpdatePolicy.replacingUpdate(),
roleConfiguration: {
additionalPolicies: [
new GuAllowPolicy(this, 'SignalResourePolicy', {
actions: ['cloudformation:SignalResource'],
resources: ['*'],
}),
new GuAllowPolicy(this, 'DescribeInstanceHealthPolicy', {
actions: ['elasticloadbalancing:DescribeInstanceHealth'],
resources: ['*'],
}),
],
},
});
);

const createPolicy: CfnCreationPolicy = {
autoScalingCreationPolicy: {
Expand All @@ -96,14 +99,16 @@ export class CdkPlayground extends GuStack {
timeout: 'PT15M',
},
};

// aws elbv2 describe-target-health --targets Id=i-03bd1b162e8ef3187,Port=9000
autoScalingGroup.userData.addCommands(
`
until [ "$state" == "\\"InService\\"" ]; do
state=$(aws --region ${this.region} elb describe-instance-health \
--load-balancer-name ${loadBalancer.loadBalancerName} \
--instances $(curl -s http://169.254.169.254/latest/meta-data/instance-id) \
--query InstanceStates[0].State);
until [ "$state" == "\\"healthy\\"" ]; do
state=$(aws elbv2 describe-target-health \
--target-group-arn ${targetGroup.targetGroupArn} \
--region ${this.region} \
--targets Id=$(curl -s http://169.254.169.254/latest/meta-data/instance-id),Port=9000 \
--query "TargetHealthDescriptions[0].TargetHealth.State");
echo "Current state $state. Sleeping for 10 seconds...";
sleep 10;
done
`,
Expand Down

0 comments on commit 2b7eb90

Please sign in to comment.