Skip to content

Commit 6bcba4d

Browse files
authored
Merge pull request #2723 from guardian/deployment-id
feat(experimental-ec2-pattern): Echo `RiffRaffDeploymentId` in user-data
2 parents 93d129e + 44adc37 commit 6bcba4d

3 files changed

Lines changed: 45 additions & 0 deletions

File tree

.changeset/ninety-pants-leave.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
"@guardian/cdk": minor
3+
---
4+
5+
feat(experimental-ec2-pattern): Echo RiffRaffDeploymentId in user-data
6+
7+
This change adds a new CloudFormation parameter, `RiffRaffDeploymentId`, to be set by Riff-Raff during deployment (see guardian/riff-raff#1469).
8+
This parameter is echoed out in the user-data. This means a redeployment of the same build creates a CloudFormation changeset with a new launch template.
9+
Consequently, the running EC2 instances are cycled. This means scheduled deployments are possible.

src/experimental/patterns/__snapshots__/ec2-app.test.ts.snap

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ exports[`The GuEc2AppExperimental pattern matches the snapshot 1`] = `
2424
"GuApplicationLoadBalancer",
2525
"GuApplicationTargetGroup",
2626
"GuHttpsApplicationListener",
27+
"GuRiffRaffDeploymentIdParameter",
2728
],
2829
"gu:cdk:version": "TEST",
2930
},
@@ -53,6 +54,10 @@ exports[`The GuEc2AppExperimental pattern matches the snapshot 1`] = `
5354
"Description": "SSM parameter containing the Name (not ARN) on the kinesis stream",
5455
"Type": "AWS::SSM::Parameter::Value<String>",
5556
},
57+
"RiffRaffDeploymentId": {
58+
"Description": "Used by Riff-Raff to inject the deployment ID.",
59+
"Type": "String",
60+
},
5661
"VpcId": {
5762
"Default": "/account/vpc/primary/id",
5863
"Description": "Virtual Private Cloud to run EC2 instances within. Should NOT be the account default VPC.",
@@ -840,6 +845,11 @@ aws s3 cp 's3://",
840845
},
841846
"/test-stack/TEST/test-gu-ec2-app/test-gu-ec2-app-123.deb' '/test-gu-ec2-app/test-gu-ec2-app-123.deb'
842847
dpkg -i /test-gu-ec2-app/test-gu-ec2-app-123.deb
848+
echo "Launch template last updated by Riff-Raff deployment ID: ",
849+
{
850+
"Ref": "RiffRaffDeploymentId",
851+
},
852+
""
843853
# GuEc2AppExperimental Instance Health Check Start
844854
845855
TOKEN=$(curl -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 21600")

src/experimental/patterns/ec2-app.ts

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -342,6 +342,25 @@ export interface GuUserDataForRollingUpdateExperimentalProps {
342342
slowStartDuration?: Duration;
343343
}
344344

345+
class GuRiffRaffDeploymentIdParameter extends CfnParameter {
346+
private static instance: GuRiffRaffDeploymentIdParameter | undefined;
347+
348+
private constructor(scope: GuStack) {
349+
super(scope, "RiffRaffDeploymentId", {
350+
type: "String",
351+
description: "Used by Riff-Raff to inject the deployment ID.",
352+
});
353+
}
354+
355+
public static getInstance(stack: GuStack): GuRiffRaffDeploymentIdParameter {
356+
if (!this.instance || !isSingletonPresentInStack(stack, this.instance)) {
357+
this.instance = new GuRiffRaffDeploymentIdParameter(stack);
358+
}
359+
360+
return this.instance;
361+
}
362+
}
363+
345364
/**
346365
* Modifies the user-data script (which runs whenever an instance is launched) to make it compatible with the new
347366
* rolling update deployment mechanism.
@@ -357,6 +376,13 @@ export class GuUserDataForRollingUpdateExperimental {
357376
const { autoScalingGroup, targetGroup, applicationPort, buildIdentifier, slowStartDuration } = props;
358377
const cfnAutoScalingGroup = autoScalingGroup.node.defaultChild as CfnAutoScalingGroup;
359378

379+
const deploymentId = GuRiffRaffDeploymentIdParameter.getInstance(scope);
380+
381+
// This allows redeployment of the same build to cycle instances
382+
autoScalingGroup.userData.addCommands(
383+
`echo "Launch template last updated by Riff-Raff deployment ID: ${deploymentId.valueAsString}"`,
384+
);
385+
360386
/*
361387
`aws` is available via AMIgo baked AMIs.
362388
See https://github.com/guardian/amigo/tree/main/roles/aws-tools.

0 commit comments

Comments
 (0)