Skip to content

Commit

Permalink
fix(stepfunction-tasks): grant step function permissions to invoke al…
Browse files Browse the repository at this point in the history
…l versions of a lambda function
  • Loading branch information
mrgrain committed Jul 19, 2022
1 parent 910bd85 commit e2c5a4c
Showing 1 changed file with 30 additions and 1 deletion.
31 changes: 30 additions & 1 deletion packages/@aws-cdk/aws-stepfunctions-tasks/lib/lambda/invoke.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as iam from '@aws-cdk/aws-iam';
import * as lambda from '@aws-cdk/aws-lambda';
import { IAlias, IFunction, IVersion } from '@aws-cdk/aws-lambda';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as cdk from '@aws-cdk/core';
import { Construct } from 'constructs';
Expand Down Expand Up @@ -120,7 +121,7 @@ export class LambdaInvoke extends sfn.TaskStateBase {

this.taskPolicies = [
new iam.PolicyStatement({
resources: this.props.lambdaFunction.resourceArnsForGrantInvoke,
resources: this.determineResourceArnsForGrantInvoke(props.lambdaFunction),
actions: ['lambda:InvokeFunction'],
}),
];
Expand Down Expand Up @@ -161,6 +162,34 @@ export class LambdaInvoke extends sfn.TaskStateBase {
};
}
}

/**
* Determine the ARN(s) to put into the resource field of the generated
* IAM policy based on the type of the provided lambda function.
*
* When invoking versions, we need to give permissions to all qualifiers.
* Otherwise in-flight StepFunctions executions will fail with missing
* permissions, due to the changed version causing the policy to update and
* to remove permissions to invoke the previous version.
*
* @see https://github.com/aws/aws-cdk/issues/17515
*/
private determineResourceArnsForGrantInvoke(lambdaFunction: IFunction): string[] {
if (isVersion(lambdaFunction)) {
return lambdaFunction.lambda.resourceArnsForGrantInvoke;
}

return lambdaFunction.resourceArnsForGrantInvoke;
}
}

/**
* Type guard to determine if a given `IFunction` implements IVersion
*/
function isVersion(lambdaFunction: IFunction | IAlias | IVersion): lambdaFunction is IVersion {
return !(lambdaFunction as IAlias).aliasName
&& (lambdaFunction as IVersion).lambda
&& Boolean((lambdaFunction as IVersion).version);
}

/**
Expand Down

0 comments on commit e2c5a4c

Please sign in to comment.