Skip to content

Commit

Permalink
Handle another case when sfn->Lambda context injection is already set…
Browse files Browse the repository at this point in the history
… up (case 4.2)
  • Loading branch information
lym953 committed Sep 26, 2024
1 parent d36a07e commit 2cc8717
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
13 changes: 13 additions & 0 deletions src/commands/stepfunctions/__tests__/helpers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,19 @@ describe('stepfunctions command helpers tests', () => {
expect(injectContextForLambdaFunctions(step, context, 'Lambda Invoke')).toBeFalsy()
})

test('Case 4.2: context injection already set up', () => {
const step: StepType = {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke',
Parameters: {
FunctionName: 'arn:aws:lambda:sa-east-1:425362991234:function:unit-test-lambda-function',
'Payload.$': `$$['Execution', 'State', 'StateMachine']`,
},
End: true,
}
expect(injectContextForLambdaFunctions(step, context, 'Lambda Invoke')).toBeFalsy()
})

test('Case 1: no Payload or Payload.$', () => {
const step: StepType = {
Type: 'Task',
Expand Down
8 changes: 6 additions & 2 deletions src/commands/stepfunctions/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ export type ParametersType = {
// 2.3 | "Payload" object has no Execution, State or StateMachine | true
// 3 | "Payload" is not object | false
// 4.1 | "Payload.$": "$" (default payload) | true
// 4.2 | "Payload.$": "States.JsonMerge($$, $, false)" | false
// 4.2 | "Payload.$": "States.JsonMerge($$, $, false)" or | false
// | "Payload.$": "$$['Execution', 'State', 'StateMachine']" |
// 4.3 | Custom "Payload.$" | false
export const injectContextForLambdaFunctions = (step: StepType, context: BaseContext, stepName: string): boolean => {
if (step.Resource?.startsWith('arn:aws:lambda')) {
Expand Down Expand Up @@ -244,7 +245,10 @@ merge these traces, check out https://docs.datadoghq.com/serverless/step_functio
}

// Case 4.2: context injection is already set up using "Payload.$"
if (step.Parameters['Payload.$'] === 'States.JsonMerge($$, $, false)') {
if (
step.Parameters['Payload.$'] === 'States.JsonMerge($$, $, false)' ||
step.Parameters['Payload.$'] === `$$['Execution', 'State', 'StateMachine']`
) {
context.stdout.write(` Step ${stepName}: Context injection is already set up. Skipping context injection.\n`)

return false
Expand Down

0 comments on commit 2cc8717

Please sign in to comment.