Skip to content

Commit

Permalink
Input the raw command for checking ELB status instead of relying on c…
Browse files Browse the repository at this point in the history
…fn-init
  • Loading branch information
AshCorr committed Jul 16, 2024
1 parent e37c3ba commit f5b01f0
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 71 deletions.
124 changes: 60 additions & 64 deletions cdk/lib/__snapshots__/cdk-playground.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ exports[`The Deploy stack matches the snapshot 1`] = `
Object {
"Metadata": Object {
"gu:cdk:constructs": Array [
"GuAllowPolicy",
"GuAllowPolicy",
"GuVpcParameter",
"GuSubnetListParameter",
"GuSubnetListParameter",
Expand Down Expand Up @@ -123,22 +125,6 @@ Object {
"Timeout": "PT15M",
},
},
"Metadata": Object {
"AWS::CloudFormation::Init": Object {
"config": Object {
"commands": Object {
"000": Object {
"command": "echo \\"Hello, World!\\" > /tmp/hello.txt",
},
},
},
"configSets": Object {
"default": Array [
"config",
],
},
},
},
"Properties": Object {
"HealthCheckGracePeriod": 120,
"HealthCheckType": "ELB",
Expand Down Expand Up @@ -318,6 +304,27 @@ Object {
},
"Type": "AWS::IAM::Policy",
},
"DescribeInstanceHealthPolicy48A60A85": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": "elasticloadbalancing:DescribeInstanceHealth",
"Effect": "Allow",
"Resource": "*",
},
],
"Version": "2012-10-17",
},
"PolicyName": "DescribeInstanceHealthPolicy48A60A85",
"Roles": Array [
Object {
"Ref": "InstanceRoleCdkplaygroundC280027A",
},
],
},
"Type": "AWS::IAM::Policy",
},
"EC2AppDNS": Object {
"Properties": Object {
"Name": "cdk-playground.gutools.co.uk",
Expand Down Expand Up @@ -607,32 +614,6 @@ Object {
},
"Type": "AWS::IAM::Role",
},
"InstanceRoleCdkplaygroundDefaultPolicy7D8FF52F": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": Array [
"cloudformation:DescribeStackResource",
"cloudformation:SignalResource",
],
"Effect": "Allow",
"Resource": Object {
"Ref": "AWS::StackId",
},
},
],
"Version": "2012-10-17",
},
"PolicyName": "InstanceRoleCdkplaygroundDefaultPolicy7D8FF52F",
"Roles": Array [
Object {
"Ref": "InstanceRoleCdkplaygroundC280027A",
},
],
},
"Type": "AWS::IAM::Policy",
},
"LambdaDNS": Object {
"Properties": Object {
"Name": "cdk-playground-lambda.gutools.co.uk",
Expand Down Expand Up @@ -863,6 +844,27 @@ Object {
},
"Type": "AWS::IAM::Policy",
},
"SignalResourePolicy715B276A": Object {
"Properties": Object {
"PolicyDocument": Object {
"Statement": Array [
Object {
"Action": "cloudformation:SignalResource",
"Effect": "Allow",
"Resource": "*",
},
],
"Version": "2012-10-17",
},
"PolicyName": "SignalResourePolicy715B276A",
"Roles": Array [
Object {
"Ref": "InstanceRoleCdkplaygroundC280027A",
},
],
},
"Type": "AWS::IAM::Policy",
},
"SsmSshPolicy4CFC977E": Object {
"Properties": Object {
"PolicyDocument": Object {
Expand Down Expand Up @@ -1651,7 +1653,6 @@ Object {
},
"playgroundPRODcdkplayground7B64111F": Object {
"DependsOn": Array [
"InstanceRoleCdkplaygroundDefaultPolicy7D8FF52F",
"InstanceRoleCdkplaygroundC280027A",
],
"Properties": Object {
Expand Down Expand Up @@ -1751,36 +1752,31 @@ Object {
"",
Array [
"#!/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'
}
trap exitTrap EXIT
mkdir -p $(dirname '/cdk-playground/cdk-playground-TEST.deb')
aws s3 cp 's3://",
Object {
"Ref": "DistributionBucketName",
},
"/playground/PROD/cdk-playground/cdk-playground-TEST.deb' '/cdk-playground/cdk-playground-TEST.deb'
dpkg -i /cdk-playground/cdk-playground-TEST.deb
# fingerprint: e9324f4a83cf71be
(
set +e
/opt/aws/bin/cfn-init -v --region ",
Object {
"Ref": "AWS::Region",
},
" --stack ",
Object {
"Ref": "AWS::StackName",
},
" --resource AutoScalingGroupCdkplaygroundASGD6E49F0F -c default
/opt/aws/bin/cfn-signal -e $? --region ",
Object {
"Ref": "AWS::Region",
},
" --stack ",
until [ \\"$state\\" == \\"\\\\\\"InService\\\\\\"\\" ]; do
state=$(aws --region eu-west-1 elb describe-instance-health --load-balancer-name ",
Object {
"Ref": "AWS::StackName",
"Fn::GetAtt": Array [
"LoadBalancerCdkplayground7C6B4D97",
"LoadBalancerName",
],
},
" --resource AutoScalingGroupCdkplaygroundASGD6E49F0F
cat /var/log/cfn-init.log >&2
)",
" --instances $(curl -s http://169.254.169.254/latest/meta-data/instance-id) --query InstanceStates[0].State);
sleep 10;
done
",
],
],
},
Expand Down
37 changes: 30 additions & 7 deletions cdk/lib/cdk-playground.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ import { GuCertificate } from '@guardian/cdk/lib/constructs/acm';
import type { GuStackProps } from '@guardian/cdk/lib/constructs/core';
import { GuStack, GuStringParameter } from '@guardian/cdk/lib/constructs/core';
import { GuCname } from '@guardian/cdk/lib/constructs/dns';
import { GuFastlyLogsIamRole } from '@guardian/cdk/lib/constructs/iam';
import {
GuAllowPolicy,
GuFastlyLogsIamRole,
GuPolicy,

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

View workflow job for this annotation

GitHub Actions / CI

'GuPolicy' is defined but never used
} from '@guardian/cdk/lib/constructs/iam';
import type {
App,
CfnAutoScalingReplacingUpdate,

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

View workflow job for this annotation

GitHub Actions / CI

'CfnAutoScalingReplacingUpdate' is defined but never used
Expand Down Expand Up @@ -69,6 +73,18 @@ export class CdkPlayground extends GuStack {
},
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 = {
Expand All @@ -81,14 +97,21 @@ export class CdkPlayground extends GuStack {
},
};

const asg = autoScalingGroup.node.defaultChild as CfnAutoScalingGroup;
asg.cfnOptions.creationPolicy = createPolicy;

const init = CloudFormationInit.fromElements(
InitCommand.shellCommand('echo "Hello, World!" > /tmp/hello.txt'),
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);
sleep 10;
done
`,
);
autoScalingGroup.userData.addSignalOnExitCommand(autoScalingGroup);

autoScalingGroup.applyCloudFormationInit(init);
const asg = autoScalingGroup.node.defaultChild as CfnAutoScalingGroup;
asg.cfnOptions.creationPolicy = createPolicy;

new GuCname(this, 'EC2AppDNS', {
app: ec2App,
Expand Down

0 comments on commit f5b01f0

Please sign in to comment.