Skip to content

Commit

Permalink
Allow for legacy lambda steps for step functions (#561)
Browse files Browse the repository at this point in the history
Allow context injection for legacy step function steps using lambda.

JIRA: https://datadoghq.atlassian.net/browse/SVLS-5638
  • Loading branch information
avangelillo authored Dec 18, 2024
1 parent d103702 commit 507e0d9
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 22 deletions.
20 changes: 10 additions & 10 deletions src/step-functions-helper.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import {
isDefaultLambdaApiStep,
isLambdaApiStep,
StateMachineDefinition,
updateDefinitionString,
updateDefinitionForStepFunctionInvocationStep,
Expand Down Expand Up @@ -223,7 +223,7 @@ describe("test updateDefinitionString", () => {
expect(definitionAfterUpdate.States?.InvokeLambda?.Parameters?.["Payload.$"]).toBe("something-customized");
});

it("test lambda basic legacy integration do nothing", async () => {
it("test lambda legacy integration with undefined parameters do nothing", async () => {
const definitionString = {
"Fn::Sub": [
'{"Comment":"fake comment","StartAt":"InvokeLambda","States":{"InvokeLambda":{"Type":"Task","Resource":"arn:aws:lambda:sa-east-1:601427271234:function:unit-test-function-name","End":true}}}',
Expand Down Expand Up @@ -254,7 +254,7 @@ describe("test updateDefinitionString", () => {
});
});

it("test legacy lambda api do nothing", async () => {
it("test legacy lambda context is injected", async () => {
const definitionString = {
"Fn::Sub": [
'{"Comment":"fake comment","StartAt":"InvokeLambda","States":{"InvokeLambda":{"Type":"Task","Parameters":{"FunctionName":"fake-function-name","Payload.$":"$"},"Resource":"arn:aws:lambda:sa-east-1:601427271234:function:unit-test-function-name","End":true}}}',
Expand All @@ -268,7 +268,7 @@ describe("test updateDefinitionString", () => {
End: true,
Parameters: {
FunctionName: "fake-function-name",
"Payload.$": "$",
"Payload.$": "States.JsonMerge($$, $, false)",
},
Resource: "arn:aws:lambda:sa-east-1:601427271234:function:unit-test-function-name",
Type: "Task",
Expand Down Expand Up @@ -381,29 +381,29 @@ describe("test updateDefinitionForStepFunctionInvocationStep", () => {
});
});

describe("test isDefaultLambdaApiStep", () => {
describe("test isLambdaApiStep", () => {
it("resource is default lambda", async () => {
const resource = "arn:aws:states:::lambda:invoke";
expect(isDefaultLambdaApiStep(resource)).toBeTruthy();
expect(isLambdaApiStep(resource)).toBeTruthy();
});

it("resource is lambda arn for legacy lambda api", async () => {
const resource = "arn:aws:lambda:sa-east-1:601427271234:function:hello-function";
expect(isDefaultLambdaApiStep(resource)).toBeFalsy();
expect(isLambdaApiStep(resource)).toBeTruthy();
});

it("resource of dynamodb", async () => {
const resource = "arn:aws:states:::dynamodb:updateItem";
expect(isDefaultLambdaApiStep(resource)).toBeFalsy();
expect(isLambdaApiStep(resource)).toBeFalsy();
});

it("resource of empty string", async () => {
const resource = "";
expect(isDefaultLambdaApiStep(resource)).toBeFalsy();
expect(isLambdaApiStep(resource)).toBeFalsy();
});

it("resource of undefined", async () => {
const resource = undefined;
expect(isDefaultLambdaApiStep(resource)).toBeFalsy();
expect(isLambdaApiStep(resource)).toBeFalsy();
});
});
20 changes: 8 additions & 12 deletions src/step-functions-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,16 +42,12 @@ export interface StateMachineStep {
End?: boolean;
}

export function isDefaultLambdaApiStep(resource: string | undefined): boolean {
// default means not legacy lambda api
if (resource === undefined) {
return false;
}
if (resource === "arn:aws:states:::lambda:invoke") {
// Legacy Lambda API resource.startsWith("arn:aws:lambda"), but it cannot inject context obj.
return true;
}
return false;
export function isLambdaApiStep(resource: string | undefined): boolean {
// Allow for either the standard or legacy definitions of a lambda step
return (
resource !== undefined &&
(resource?.startsWith("arn:aws:states:::lambda:invoke") || resource?.startsWith("arn:aws:lambda"))
);
}

export function isStepFunctionInvocation(resource: string | undefined): boolean {
Expand Down Expand Up @@ -99,8 +95,8 @@ export function updateDefinitionString(
// Step 2: Mutate the definition object
const states = definitionObj.States;
for (const [stepName, step] of Object.entries(states)) {
// only inject context into default Lambda API steps and Step Function invocation steps
if (isDefaultLambdaApiStep(step?.Resource)) {
// only inject context into Lambda API steps and Step Function invocation steps
if (isLambdaApiStep(step?.Resource)) {
updateDefinitionForDefaultLambdaApiStep(stepName, step, serverless, stateMachineName);
} else if (isStepFunctionInvocation(step?.Resource)) {
updateDefinitionForStepFunctionInvocationStep(stepName, step, serverless, stateMachineName);
Expand Down

0 comments on commit 507e0d9

Please sign in to comment.