# TaskRole

Role to be assumed by the State Machine's execution role for invoking a task's resource.

Obtainable from: TaskRole.fromRole, TaskRole.fromRoleArnJsonPath

- Source: [`aws-stepfunctions/lib/task-credentials.ts`](https://github.com/aws/aws-cdk/blob/v2.277.0/packages/aws-cdk-lib/aws-stepfunctions/lib/task-credentials.ts#L23)
- Type last changed: v2.54.0 · 7 Dec 2022

## Example

TypeScript:

```ts
import * as lambda from 'aws-cdk-lib/aws-lambda';

declare const submitLambda: lambda.Function;
declare const iamRole: iam.Role;

// use a fixed role for all task invocations
const role = sfn.TaskRole.fromRole(iamRole);
// or use a json expression to resolve the role at runtime based on task inputs
//const role = sfn.TaskRole.fromRoleArnJsonPath('$.RoleArn');

const submitJob = new tasks.LambdaInvoke(this, 'Submit Job', {
  lambdaFunction: submitLambda,
  outputPath: '$.Payload',
  // use credentials
  credentials: { role },
});
```

Python:

```python
import aws_cdk.aws_lambda as lambda_

# submit_lambda: lambda.Function
# iam_role: iam.Role

# use a fixed role for all task invocations
role = sfn.TaskRole.from_role(iam_role)
# or use a json expression to resolve the role at runtime based on task inputs
# const role = sfn.TaskRole.fromRoleArnJsonPath('$.RoleArn');

submit_job = tasks.LambdaInvoke(self, "Submit Job",
    lambda_function=submit_lambda,
    output_path="$.Payload",
    # use credentials
    credentials=sfn.Credentials(role=role)
)
```

Java:

```java
import software.amazon.awscdk.services.lambda.*;

Function submitLambda;
Role iamRole;

// use a fixed role for all task invocations
TaskRole role = TaskRole.fromRole(iamRole);
// or use a json expression to resolve the role at runtime based on task inputs
//const role = sfn.TaskRole.fromRoleArnJsonPath('$.RoleArn');

LambdaInvoke submitJob = LambdaInvoke.Builder.create(this, "Submit Job")
        .lambdaFunction(submitLambda)
        .outputPath("$.Payload")
        // use credentials
        .credentials(Credentials.builder().role(role).build())
        .build();
```

C# / .NET:

```csharp
// Install: dotnet add package Amazon.CDK.Lib
using Amazon.CDK.AWS.Lambda;

Function submitLambda;
Role iamRole;

// use a fixed role for all task invocations
var role = TaskRole.FromRole(iamRole);
// or use a json expression to resolve the role at runtime based on task inputs
//const role = sfn.TaskRole.fromRoleArnJsonPath('$.RoleArn');

var submitJob = new LambdaInvoke(this, "Submit Job", new LambdaInvokeProps {
    LambdaFunction = submitLambda,
    OutputPath = "$.Payload",
    // use credentials
    Credentials = new Credentials { Role = role }
});
```

Golang:

```go
// Install: go get github.com/aws/aws-cdk-go/awscdk/v2
import lambda "github.com/aws/aws-cdk-go/awscdk"

var submitLambda Function
var iamRole Role

// use a fixed role for all task invocations
role := sfn.TaskRole_FromRole(iamRole)
// or use a json expression to resolve the role at runtime based on task inputs
//const role = sfn.TaskRole.fromRoleArnJsonPath('$.RoleArn');

submitJob := tasks.NewLambdaInvoke(this, jsii.String("Submit Job"), &LambdaInvokeProps{
	LambdaFunction: submitLambda,
	OutputPath: jsii.String("$.Payload"),
	// use credentials
	Credentials: &Credentials{
		Role: *Role,
	},
})
```

## Initializer

TypeScript:

```ts
new TaskRole()
```

Python:

```python
TaskRole()
```

Java:

```java
new TaskRole()
```

C# / .NET:

```csharp
new TaskRole()
```

Golang:

```go
NewTaskRole()
```

## Properties

- `resource`: string — Retrieves the resource for use in IAM Policies for this TaskRole.

- `roleArn`: string — Retrieves the roleArn for this TaskRole.

## Methods

- `fromRole(role: IRole): TaskRole // chainable` (static) — Construct a task role based on the provided IAM Role.

  **Parameters**

  - `role` — IAM Role.

- `fromRoleArnJsonPath(expression: string): TaskRole // chainable` (static) — Construct a task role retrieved from task inputs using a json expression.

  **Parameters**

  - `expression` — json expression to roleArn.

  ```ts
  sfn.TaskRole.fromRoleArnJsonPath('$.RoleArn');
  ```
