Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Support sfn->Lambda context injection when Payload.$ exists (case 4) #531

Merged
merged 1 commit into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions src/step-functions-helper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ describe("test updateDefinitionString", () => {
expect(definitionAfterUpdate.States?.InvokeLambda?.Parameters).toBe("Just a string!");
});

it("test lambda step with default payload of '$'", async () => {
it("Case 4.1: test lambda step with default payload of '$'", async () => {
const definitionString = {
"Fn::Sub": [
'{"Comment":"fake comment","StartAt":"InvokeLambda","States":{"InvokeLambda":{"Type":"Task","Parameters":{"FunctionName":"fake-function-name","Payload.$":"$"},"Resource":"arn:aws:states:::lambda:invoke","End":true}}}',
Expand Down Expand Up @@ -179,7 +179,37 @@ describe("test updateDefinitionString", () => {
});
});

it("test lambda step already has customized payload set do nothing", async () => {
it(`Case 4.2: test lambda step already has context injection set up using "Payload.$": "States.JsonMerge($$, $, false)"`, async () => {
const definitionString = {
"Fn::Sub": [
'{"Comment":"fake comment","StartAt":"InvokeLambda","States":{"InvokeLambda":{"Type":"Task","Parameters":{"FunctionName":"fake-function-name","Payload.$":"States.JsonMerge($$, $, false)"},"Resource":"arn:aws:states:::lambda:invoke","End":true}}}',
{},
],
};
updateDefinitionString(definitionString, serverless, stateMachineName);

const definitionAfterUpdate: StateMachineDefinition = JSON.parse(definitionString["Fn::Sub"][0] as string);
expect(definitionAfterUpdate.States?.InvokeLambda?.Parameters?.["Payload.$"]).toBe(
"States.JsonMerge($$, $, false)",
);
});

it(`Case 4.2: test lambda step already has context injection set up using "Payload.$": "$$['Execution', 'State', 'StateMachine']"`, async () => {
const definitionString = {
"Fn::Sub": [
`{"Comment":"fake comment","StartAt":"InvokeLambda","States":{"InvokeLambda":{"Type":"Task","Parameters":{"FunctionName":"fake-function-name","Payload.$":"$$['Execution', 'State', 'StateMachine']"},"Resource":"arn:aws:states:::lambda:invoke","End":true}}}`,
{},
],
};
updateDefinitionString(definitionString, serverless, stateMachineName);

const definitionAfterUpdate: StateMachineDefinition = JSON.parse(definitionString["Fn::Sub"][0] as string);
expect(definitionAfterUpdate.States?.InvokeLambda?.Parameters?.["Payload.$"]).toBe(
`$$['Execution', 'State', 'StateMachine']`,
);
});

it("Case 4.3: test lambda step has custom Payload.$ do nothing", async () => {
const definitionString = {
"Fn::Sub": [
'{"Comment":"fake comment","StartAt":"InvokeLambda","States":{"InvokeLambda":{"Type":"Task","Parameters":{"FunctionName":"fake-function-name","Payload.$":"something-customized"},"Resource":"arn:aws:states:::lambda:invoke","End":true}}}',
Expand Down
36 changes: 27 additions & 9 deletions src/step-functions-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,20 +207,38 @@ merge these traces, check out https://docs.datadoghq.com/serverless/step_functio

return;
}
}
} else {
// Case 4: Parameters has "Payload.$" field

// Case 4.1: default "Payload.$"
if (step.Parameters["Payload.$"] === "$") {
step.Parameters!["Payload.$"] = "States.JsonMerge($$, $, false)";
serverless.cli.log(
`JsonMerge Step Functions context object with payload in step: ${stepName} of state machine: ${stateMachineName}.`,
);
return;
}

// Case 4.2: context injection is already set up using "Payload.$"
if (
step.Parameters["Payload.$"] === "States.JsonMerge($$, $, false)" ||
step.Parameters["Payload.$"] === "$$['Execution', 'State', 'StateMachine']"
) {
serverless.cli.log(
`[Warn] Step ${stepName} of state machine ${stateMachineName}: Context injection is already set up. Skipping context injection.\n`,
);

if (step.Parameters["Payload.$"] === "$") {
// $ % is the default original unchanged payload
step.Parameters!["Payload.$"] = "States.JsonMerge($$, $, false)";
return;
}

// Case 4.3: custom "Payload.$"
serverless.cli.log(
`JsonMerge Step Functions context object with payload in step: ${stepName} of state machine: ${stateMachineName}.`,
`[Warn] Step ${stepName} of state machine ${stateMachineName} has a custom Payload field. Step Functions Context Object injection \
skipped. Your Step Functions trace will not be merged with downstream Lambda traces. To manually merge these traces, \
check out https://docs.datadoghq.com/serverless/step_functions/troubleshooting/\n`,
);
return;
}

serverless.cli.log(
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Deleting this code because it's unreachable.
BTW I will refactor this function to make it hopefully more clear.

`[Warn] Parameters.Payload has been set. Merging traces failed for step: ${stepName} of state machine: ${stateMachineName}`,
);
}

function updateDefinitionForStepFunctionInvocationStep(step: StateMachineStep): void {
Expand Down
Loading