Skip to content

feat: support Arguments for Lambda when query language is JSONata #645

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

Merged
merged 3 commits into from
May 3, 2025
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
18 changes: 10 additions & 8 deletions lib/deploy/stepFunctions/compileIamRole.js
Original file line number Diff line number Diff line change
Expand Up @@ -360,9 +360,18 @@ function getRedshiftDataPermissions(action, state) {
}

function getLambdaPermissions(state) {
if (isJsonPathParameter(state, 'FunctionName') || isJsonataArgument(state, 'FunctionName')) {
const allowedFunctions = getParameterOrArgument(state, 'AllowedFunctions');
return [{
action: 'lambda:InvokeFunction',
resource: allowedFunctions || '*',
}];
}

// function name can be name-only, name-only with alias, full arn or partial arn
// https://docs.aws.amazon.com/lambda/latest/dg/API_Invoke.html#API_Invoke_RequestParameters
const functionName = state.Parameters.FunctionName;
const functionName = getParameterOrArgument(state, 'FunctionName');

if (_.isString(functionName)) {
const segments = functionName.split(':');

Expand Down Expand Up @@ -429,13 +438,6 @@ function getLambdaPermissions(state) {
}];
}

if (state.Parameters['FunctionName.$']) {
return [{
action: 'lambda:InvokeFunction',
resource: state.Parameters.AllowedFunctions ? state.Parameters.AllowedFunctions : '*',
}];
}

// hope for the best...
return [{
action: 'lambda:InvokeFunction',
Expand Down
141 changes: 113 additions & 28 deletions lib/deploy/stepFunctions/compileIamRole.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3587,7 +3587,49 @@ describe('#compileIamRole', () => {
]);
});

it('should support variable FunctionName', () => {
itParam('should resolve FunctionName: ${value}', ['JSONPath', 'JSONata'], (queryLanguage) => {
serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
id: 'StateMachine1',
definition: {
StartAt: 'A',
States: {
A: {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke',
...getParamsOrArgs(
queryLanguage,
{
FunctionName: 'arn:aws:lambda:us-west-2:1234567890:function:foo',
'Payload.$': '$.Payload',
},
{
FunctionName: 'arn:aws:lambda:us-west-2:1234567890:function:foo',
Payload: '{% $states.input.Payload %}',
},
),
End: true,
},
},
},
},
},
};

serverlessStepFunctions.compileIamRole();
const statements = serverlessStepFunctions.serverless.service
.provider.compiledCloudFormationTemplate.Resources.StateMachine1Role
.Properties.Policies[0].PolicyDocument.Statement;
const lambdaPermissions = statements.filter(s => _.isEqual(s.Action, ['lambda:InvokeFunction']));
expect(lambdaPermissions).to.have.lengthOf(1);
expect(lambdaPermissions[0].Resource).to.deep.equal([
'arn:aws:lambda:us-west-2:1234567890:function:foo',
'arn:aws:lambda:us-west-2:1234567890:function:foo:*',
]);
});

itParam('should support variable FunctionName: ${value}', ['JSONPath', 'JSONata'], (queryLanguage) => {
serverless.service.stepFunctions = {
stateMachines: {
myStateMachine1: {
Expand All @@ -3598,26 +3640,47 @@ describe('#compileIamRole', () => {
A: {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke.waitForTaskToken',
Parameters: {
'FunctionName.$': '$.functionName',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
...getParamsOrArgs(
queryLanguage,
{
'FunctionName.$': '$.functionName',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
},
},
},
{
FunctionName: '{% $states.input.functionName %}',
Payload: {
model: '{% $states.input.new_model %}',
token: '{% $states.context.Task.Token %}',
},
},
),
Next: 'B',
},
B: {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke.waitForTaskToken',
Parameters: {
'FunctionName.$': '$.functionName',
AllowedFunctions: '*limited*',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
...getParamsOrArgs(
queryLanguage,
{
'FunctionName.$': '$.functionName',
AllowedFunctions: '*limited*',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
},
},
},
{
FunctionName: '{% $states.input.functionName %}',
AllowedFunctions: '*limited*',
Payload: {
model: '{% $states.input.new_model %}',
token: '{% $states.context.Task.Token %}',
},
},
),
End: true,
},
},
Expand All @@ -3643,27 +3706,49 @@ describe('#compileIamRole', () => {
A: {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke.waitForTaskToken',
Parameters: {
'FunctionName.$': '$.functionName',
AllowedFunctions: 'arn:aws:lambda:us-west-2:1234567890:function:foo',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
...getParamsOrArgs(
queryLanguage,
{
'FunctionName.$': '$.functionName',
AllowedFunctions: 'arn:aws:lambda:us-west-2:1234567890:function:foo',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
},
},
},
{
FunctionName: '{% $states.input.functionName %}',
AllowedFunctions: 'arn:aws:lambda:us-west-2:1234567890:function:foo',
Payload: {
model: '{% $states.input.new_model %}',
token: '{% $states.context.Task.Token %}',
},
},
),
Next: 'B',
},
B: {
Type: 'Task',
Resource: 'arn:aws:states:::lambda:invoke.waitForTaskToken',
Parameters: {
'FunctionName.$': '$.functionName',
AllowedFunctions: '*limited*',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
...getParamsOrArgs(
queryLanguage,
{
'FunctionName.$': '$.functionName',
AllowedFunctions: '*limited*',
Payload: {
'model.$': '$.new_model',
'token.$': '$$.Task.Token',
},
},
},
{
FunctionName: '{% $states.input.functionName %}',
AllowedFunctions: '*limited*',
Payload: {
model: '{% $states.input.new_model %}',
token: '{% $states.context.Task.Token %}',
},
},
),
End: true,
},
},
Expand Down
Loading