Skip to content

Commit c42f563

Browse files
committed
custom implementation of async response logic
1 parent 415cb95 commit c42f563

File tree

4 files changed

+67
-8
lines changed

4 files changed

+67
-8
lines changed

ab-testing/cdk/lib/__snapshots__/abTestingStack.test.ts.snap

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,7 +239,7 @@ exports[`The ID5 Baton Lambda stack > matches the CODE snapshot 1`] = `
239239
{
240240
"Ref": "AWS::AccountId"
241241
},
242-
":parameter/ab-testing-deploy/CODE/fastly-ab-testing-config"
242+
":parameter/ab-testing-deploy/CODE/fastly-config"
243243
]
244244
]
245245
}
@@ -574,7 +574,7 @@ exports[`The ID5 Baton Lambda stack > matches the PROD snapshot 1`] = `
574574
{
575575
"Ref": "AWS::AccountId"
576576
},
577-
":parameter/ab-testing-deploy/PROD/fastly-ab-testing-config"
577+
":parameter/ab-testing-deploy/PROD/fastly-config"
578578
]
579579
]
580580
}

ab-testing/cdk/lib/abTestingStack.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { GuStack } from "@guardian/cdk/lib/constructs/core/stack.js";
33
import { GuLambdaFunction } from "@guardian/cdk/lib/constructs/lambda/index.js";
44
import { GuS3Bucket } from "@guardian/cdk/lib/constructs/s3/index.js";
55
import type { App } from "aws-cdk-lib";
6-
import { CustomResource } from "aws-cdk-lib";
6+
import { CustomResource, Duration } from "aws-cdk-lib";
77
import { Runtime } from "aws-cdk-lib/aws-lambda";
88
import { StringParameter } from "aws-cdk-lib/aws-ssm";
99

@@ -40,7 +40,7 @@ export class AbTestingStack extends GuStack {
4040
this,
4141
"FastlyAbTestingConfigParameter",
4242
{
43-
parameterName: `/${app}/${this.stage}/fastly-ab-testing-config`,
43+
parameterName: `/${app}/${this.stage}/fastly-config`,
4444
},
4545
);
4646

@@ -64,6 +64,7 @@ export class AbTestingStack extends GuStack {
6464
// Trigger the Lambda to run upon deployment
6565
new CustomResource(this, "InvokeDictionaryDeployLambda", {
6666
serviceToken: lambda.functionArn,
67+
serviceTimeout: Duration.minutes(5),
6768
});
6869
}
6970
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import type { CloudFormationCustomResourceEvent, Context } from "aws-lambda";
2+
3+
const SUCCESS = "SUCCESS";
4+
const FAILED = "FAILED";
5+
6+
type Status = typeof SUCCESS | typeof FAILED;
7+
8+
const maskCredentialsAndSignature = (message: string) => {
9+
return message
10+
.replace(/X-Amz-Credential=[^&\s]+/i, "X-Amz-Credential=*****")
11+
.replace(/X-Amz-Signature=[^&\s]+/i, "X-Amz-Signature=*****");
12+
};
13+
14+
// Modernised implementation of cfn-response's send function using fetch
15+
// https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/cfn-lambda-function-code-cfnresponsemodule.html
16+
const send = async (
17+
event: CloudFormationCustomResourceEvent,
18+
context: Context,
19+
responseStatus: Status,
20+
responseData?: unknown,
21+
physicalResourceId?: string,
22+
) => {
23+
const responseBody = JSON.stringify({
24+
Status: responseStatus,
25+
Reason:
26+
"See the details in CloudWatch Log Stream: " +
27+
context.logStreamName,
28+
PhysicalResourceId: physicalResourceId ?? context.logStreamName,
29+
StackId: event.StackId,
30+
RequestId: event.RequestId,
31+
LogicalResourceId: event.LogicalResourceId,
32+
Data: responseData,
33+
});
34+
35+
console.log("Response body:\n", responseBody);
36+
console.log("Sending response to:", event.ResponseURL);
37+
38+
try {
39+
await fetch(event.ResponseURL, {
40+
method: "PUT",
41+
headers: {
42+
"content-type": "",
43+
},
44+
body: responseBody,
45+
});
46+
} catch (error) {
47+
console.log(
48+
"send(..) failed executing fetch: " +
49+
maskCredentialsAndSignature(String(error)),
50+
);
51+
throw error;
52+
}
53+
};
54+
55+
export { send, SUCCESS, FAILED };
56+
export type { Status };

ab-testing/dictionary-deploy-lambda/src/index.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import { GetParameterCommand, SSMClient } from "@aws-sdk/client-ssm";
22
import type { Handler } from "aws-cdk-lib/aws-lambda";
33
import type { CloudFormationCustomResourceEvent, Context } from "aws-lambda";
4-
import { send } from "cfn-response";
54
import { assert } from "superstruct";
65
import { configStruct } from "../../lib/config.ts";
76
import { FastlyClient } from "../../lib/fastly/client.ts";
7+
import { send } from "./custom-resource-response.ts";
88
import { fetchAndDeployArtifacts } from "./deploy.ts";
99

1010
const getSecureString = async (name: string) => {
@@ -73,13 +73,15 @@ export const handler: Handler = async (
7373
},
7474
]);
7575

76-
send(event, context, "SUCCESS");
76+
await send(event, context, "SUCCESS");
7777
} else {
7878
// For Delete requests, simply respond with SUCCESS
79-
send(event, context, "SUCCESS");
79+
await send(event, context, "SUCCESS");
8080
}
8181
} catch (error) {
8282
console.error("Error deploying dictionaries:", error);
83-
send(event, context, "FAILED", { error: (error as Error).message });
83+
await send(event, context, "FAILED", {
84+
error: (error as Error).message,
85+
});
8486
}
8587
};

0 commit comments

Comments
 (0)