Skip to content

Commit

Permalink
docs(stepfunctions-tasks): make examples compile (aws#17143)
Browse files Browse the repository at this point in the history
----

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
  • Loading branch information
kaizencc authored and TikiTDO committed Feb 21, 2022
1 parent 296501a commit d5c7493
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 106 deletions.
100 changes: 54 additions & 46 deletions packages/@aws-cdk/aws-stepfunctions-tasks/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,10 @@ The following example provides the field named `input` as the input to the `Task
state that runs a Lambda function.

```ts
declare const fn: lambda.Function;
const submitJob = new tasks.LambdaInvoke(this, 'Invoke Handler', {
lambdaFunction: fn,
inputPath: '$.input'
inputPath: '$.input',
});
```

Expand All @@ -130,9 +131,10 @@ as well as other metadata.
The following example assigns the output from the Task to a field named `result`

```ts
declare const fn: lambda.Function;
const submitJob = new tasks.LambdaInvoke(this, 'Invoke Handler', {
lambdaFunction: fn,
outputPath: '$.Payload.result'
outputPath: '$.Payload.result',
});
```

Expand All @@ -149,6 +151,7 @@ The following example extracts the output payload of a Lambda function Task and
it with some static values and the state name from the context object.

```ts
declare const fn: lambda.Function;
new tasks.LambdaInvoke(this, 'Invoke Handler', {
lambdaFunction: fn,
resultSelector: {
Expand All @@ -159,7 +162,7 @@ new tasks.LambdaInvoke(this, 'Invoke Handler', {
},
stateName: sfn.JsonPath.stringAt('$$.State.Name'),
},
})
});
```

### ResultPath
Expand All @@ -174,9 +177,10 @@ The following example adds the item from calling DynamoDB's `getItem` API to the
input and passes it to the next state.

```ts
declare const myTable: dynamodb.Table;
new tasks.DynamoPutItem(this, 'PutItem', {
item: {
MessageId: tasks.DynamoAttributeValue.fromString('message-id')
MessageId: tasks.DynamoAttributeValue.fromString('message-id'),
},
table: myTable,
resultPath: `$.Item`,
Expand All @@ -199,6 +203,7 @@ The following example provides the field named `input` as the input to the Lambd
and invokes it asynchronously.

```ts
declare const fn: lambda.Function;
const submitJob = new tasks.LambdaInvoke(this, 'Invoke Handler', {
lambdaFunction: fn,
payload: sfn.TaskInput.fromJsonPathAt('$.input'),
Expand Down Expand Up @@ -258,14 +263,14 @@ const publishMessage = new tasks.SnsPublish(this, 'Publish message', {
});

const wait = new sfn.Wait(this, 'Wait', {
time: sfn.WaitTime.secondsPath('$.waitSeconds')
time: sfn.WaitTime.secondsPath('$.waitSeconds'),
});

new sfn.StateMachine(this, 'StateMachine', {
definition: convertToSeconds
.next(createMessage)
.next(publishMessage)
.next(wait)
.next(wait),
});
```

Expand All @@ -286,15 +291,13 @@ Previous-generation REST APIs currently offer more features. More details can be
The `CallApiGatewayRestApiEndpoint` calls the REST API endpoint.

```ts
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as tasks from `@aws-cdk/aws-stepfunctions-tasks`;

const restApi = new apigateway.RestApi(stack, 'MyRestApi');
import * as apigateway from '@aws-cdk/aws-apigateway';
const restApi = new apigateway.RestApi(this, 'MyRestApi');

const invokeTask = new tasks.CallApiGatewayRestApiEndpoint(stack, 'Call REST API', {
const invokeTask = new tasks.CallApiGatewayRestApiEndpoint(this, 'Call REST API', {
api: restApi,
stageName: 'prod',
method: HttpMethod.GET,
method: tasks.HttpMethod.GET,
});
```

Expand All @@ -303,15 +306,13 @@ const invokeTask = new tasks.CallApiGatewayRestApiEndpoint(stack, 'Call REST API
The `CallApiGatewayHttpApiEndpoint` calls the HTTP API endpoint.

```ts
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as tasks from `@aws-cdk/aws-stepfunctions-tasks`;

const httpApi = new apigatewayv2.HttpApi(stack, 'MyHttpApi');
import * as apigatewayv2 from '@aws-cdk/aws-apigatewayv2';
const httpApi = new apigatewayv2.HttpApi(this, 'MyHttpApi');

const invokeTask = new tasks.CallApiGatewayHttpApiEndpoint(stack, 'Call HTTP API', {
const invokeTask = new tasks.CallApiGatewayHttpApiEndpoint(this, 'Call HTTP API', {
apiId: httpApi.apiId,
apiStack: cdk.Stack.of(httpApi),
method: HttpMethod.GET,
apiStack: Stack.of(httpApi),
method: tasks.HttpMethod.GET,
});
```

Expand All @@ -324,6 +325,7 @@ You can use Step Functions' AWS SDK integrations to call any of the over two hun
directly from your state machine, giving you access to over nine thousand API actions.

```ts
declare const myBucket: s3.Bucket;
const getObject = new tasks.CallAwsService(this, 'GetObject', {
service: 's3',
action: 'getObject',
Expand All @@ -348,7 +350,7 @@ const listBuckets = new tasks.CallAwsService(this, 'ListBuckets', {
service: 's3',
action: 'ListBuckets',
iamResources: ['*'],
iamAction: 's3:ListAllMyBuckets'
iamAction: 's3:ListAllMyBuckets',
});
```

Expand Down Expand Up @@ -416,11 +418,15 @@ Step Functions supports [Batch](https://docs.aws.amazon.com/step-functions/lates

The [SubmitJob](https://docs.aws.amazon.com/batch/latest/APIReference/API_SubmitJob.html) API submits an AWS Batch job from a job definition.

```ts fixture=with-batch-job
```ts
import * as batch from '@aws-cdk/aws-batch';
declare const batchJobDefinition: batch.JobDefinition;
declare const batchQueue: batch.JobQueue;

const task = new tasks.BatchSubmitJob(this, 'Submit Job', {
jobDefinitionArn: batchJobDefinitionArn,
jobDefinitionArn: batchJobDefinition.jobDefinitionArn,
jobName: 'MyJob',
jobQueueArn: batchQueueArn,
jobQueueArn: batchQueue.jobQueueArn,
});
```

Expand Down Expand Up @@ -471,6 +477,7 @@ Read more about calling DynamoDB APIs [here](https://docs.aws.amazon.com/step-fu
The [GetItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_GetItem.html) operation returns a set of attributes for the item with the given primary key.

```ts
declare const myTable: dynamodb.Table;
new tasks.DynamoGetItem(this, 'Get Item', {
key: { messageId: tasks.DynamoAttributeValue.fromString('message-007') },
table: myTable,
Expand All @@ -482,6 +489,7 @@ new tasks.DynamoGetItem(this, 'Get Item', {
The [PutItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_PutItem.html) operation creates a new item, or replaces an old item with a new item.

```ts
declare const myTable: dynamodb.Table;
new tasks.DynamoPutItem(this, 'PutItem', {
item: {
MessageId: tasks.DynamoAttributeValue.fromString('message-007'),
Expand All @@ -497,6 +505,7 @@ new tasks.DynamoPutItem(this, 'PutItem', {
The [DeleteItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_DeleteItem.html) operation deletes a single item in a table by primary key.

```ts
declare const myTable: dynamodb.Table;
new tasks.DynamoDeleteItem(this, 'DeleteItem', {
key: { MessageId: tasks.DynamoAttributeValue.fromString('message-007') },
table: myTable,
Expand All @@ -510,6 +519,7 @@ The [UpdateItem](https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/
to the table if it does not already exist.

```ts
declare const myTable: dynamodb.Table;
new tasks.DynamoUpdateItem(this, 'UpdateItem', {
key: {
MessageId: tasks.DynamoAttributeValue.fromString('message-007')
Expand Down Expand Up @@ -547,8 +557,6 @@ The latest ACTIVE revision of the passed task definition is used for running the
The following example runs a job from a task definition on EC2

```ts
import * as ecs from '@aws-cdk/aws-ecs';

const vpc = ec2.Vpc.fromLookup(this, 'Vpc', {
isDefault: true,
});
Expand Down Expand Up @@ -579,7 +587,7 @@ const runTask = new tasks.EcsRunTask(this, 'Run', {
ecs.PlacementStrategy.randomly(),
],
placementConstraints: [
ecs.PlacementConstraint.memberOf('blieptuut')
ecs.PlacementConstraint.memberOf('blieptuut'),
],
}),
});
Expand All @@ -602,8 +610,6 @@ task definition is used for running the task. Learn more about
The following example runs a job from a task definition on Fargate

```ts
import * as ecs from '@aws-cdk/aws-ecs';

const vpc = ec2.Vpc.fromLookup(this, 'Vpc', {
isDefault: true,
});
Expand Down Expand Up @@ -648,7 +654,6 @@ Creates and starts running a cluster (job flow).
Corresponds to the [`runJobFlow`](https://docs.aws.amazon.com/emr/latest/APIReference/API_RunJobFlow.html) API in EMR.

```ts

const clusterRole = new iam.Role(this, 'ClusterRole', {
assumedBy: new iam.ServicePrincipal('ec2.amazonaws.com'),
});
Expand Down Expand Up @@ -689,7 +694,8 @@ and 256 inclusive, where the default concurrency of 1 means no step concurrency

```ts
new tasks.EmrCreateCluster(this, 'Create Cluster', {
// ...
instances: {},
name: sfn.TaskInput.fromJsonPathAt('$.ClusterName').value,
stepConcurrencyLevel: 10,
});
```
Expand All @@ -715,7 +721,7 @@ Corresponds to the [`terminateJobFlows`](https://docs.aws.amazon.com/emr/latest/

```ts
new tasks.EmrTerminateCluster(this, 'Task', {
clusterId: 'ClusterId'
clusterId: 'ClusterId',
});
```

Expand Down Expand Up @@ -793,17 +799,15 @@ The following code snippet includes a Task state that uses eks:call to list the

```ts
import * as eks from '@aws-cdk/aws-eks';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as tasks from '@aws-cdk/aws-stepfunctions-tasks';

const myEksCluster = new eks.Cluster(this, 'my sample cluster', {
version: eks.KubernetesVersion.V1_18,
clusterName: 'myEksCluster',
});

new tasks.EksCall(stack, 'Call a EKS Endpoint', {
new tasks.EksCall(this, 'Call a EKS Endpoint', {
cluster: myEksCluster,
httpMethod: MethodType.GET,
httpMethod: tasks.HttpMethods.GET,
httpPath: '/api/v1/namespaces/default/pods',
});
```
Expand All @@ -824,14 +828,12 @@ The following code snippet includes a Task state that uses events:putevents to s

```ts
import * as events from '@aws-cdk/aws-events';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as tasks from '@aws-cdk/aws-stepfunctions-tasks';

const myEventBus = events.EventBus(stack, 'EventBus', {
const myEventBus = new events.EventBus(this, 'EventBus', {
eventBusName: 'MyEventBus1',
});

new tasks.EventBridgePutEvents(stack, 'Send an event to EventBridge', {
new tasks.EventBridgePutEvents(this, 'Send an event to EventBridge', {
entries: [{
detail: sfn.TaskInput.fromObject({
Message: 'Hello from Step Functions!',
Expand All @@ -855,8 +857,8 @@ new tasks.GlueStartJobRun(this, 'Task', {
arguments: sfn.TaskInput.fromObject({
key: 'value',
}),
timeout: cdk.Duration.minutes(30),
notifyDelayAfter: cdk.Duration.minutes(5),
timeout: Duration.minutes(30),
notifyDelayAfter: Duration.minutes(5),
});
```

Expand Down Expand Up @@ -884,6 +886,7 @@ The following snippet invokes a Lambda Function with the state input as the payl
by referencing the `$` path.

```ts
declare const fn: lambda.Function;
new tasks.LambdaInvoke(this, 'Invoke with state input', {
lambdaFunction: fn,
});
Expand All @@ -899,6 +902,7 @@ The following snippet invokes a Lambda Function by referencing the `$.Payload` p
to reference the output of a Lambda executed before it.

```ts
declare const fn: lambda.Function;
new tasks.LambdaInvoke(this, 'Invoke with empty object as payload', {
lambdaFunction: fn,
payload: sfn.TaskInput.fromObject({}),
Expand All @@ -915,6 +919,7 @@ The following snippet invokes a Lambda and sets the task output to only include
the Lambda function response.

```ts
declare const fn: lambda.Function;
new tasks.LambdaInvoke(this, 'Invoke and set function response as task output', {
lambdaFunction: fn,
outputPath: '$.Payload',
Expand All @@ -927,6 +932,7 @@ Lambda function ARN directly in the "Resource" string, but it conflicts with the
integrationPattern, invocationType, clientContext, and qualifier properties.

```ts
declare const fn: lambda.Function;
new tasks.LambdaInvoke(this, 'Invoke and combine function response with task input', {
lambdaFunction: fn,
payloadResponseOnly: true,
Expand All @@ -945,6 +951,7 @@ The following snippet invokes a Lambda with the task token as part of the input
to the Lambda.

```ts
declare const fn: lambda.Function;
new tasks.LambdaInvoke(this, 'Invoke with callback', {
lambdaFunction: fn,
integrationPattern: sfn.IntegrationPattern.WAIT_FOR_TASK_TOKEN,
Expand Down Expand Up @@ -998,11 +1005,11 @@ new tasks.SageMakerCreateTrainingJob(this, 'TrainSagemaker', {
},
resourceConfig: {
instanceCount: 1,
instanceType: new ec2.InstanceType(JsonPath.stringAt('$.InstanceType')),
volumeSize: cdk.Size.gibibytes(50),
instanceType: new ec2.InstanceType(sfn.JsonPath.stringAt('$.InstanceType')),
volumeSize: Size.gibibytes(50),
}, // optional: default is 1 instance of EC2 `M4.XLarge` with `10GB` volume
stoppingCondition: {
maxRuntime: cdk.Duration.hours(2),
maxRuntime: Duration.hours(2),
}, // optional: default is 1 hour
});
```
Expand All @@ -1017,7 +1024,7 @@ new tasks.SageMakerCreateTransformJob(this, 'Batch Inference', {
modelName: 'MyModelName',
modelClientOptions: {
invocationsMaxRetries: 3, // default is 0
invocationsTimeout: cdk.Duration.minutes(5), // default is 60 seconds
invocationsTimeout: Duration.minutes(5), // default is 60 seconds
},
transformInput: {
transformDataSource: {
Expand Down Expand Up @@ -1111,7 +1118,7 @@ const task1 = new tasks.SnsPublish(this, 'Publish1', {
},
pic: {
// BINARY must be explicitly set
type: MessageAttributeDataType.BINARY,
dataType: tasks.MessageAttributeDataType.BINARY,
value: sfn.JsonPath.stringAt('$.pic'),
},
people: {
Expand Down Expand Up @@ -1170,6 +1177,7 @@ via the `associateWithParent` property. This allows the Step Functions UI to lin
executions from parent executions, making it easier to trace execution flow across state machines.

```ts
declare const child: sfn.StateMachine;
const task = new tasks.StepFunctionsStartExecution(this, 'ChildTask', {
stateMachine: child,
associateWithParent: true,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,8 +206,12 @@ export interface ResourceConfig {
/**
* ML compute instance type.
*
* @example To provide an instance type from the task input, write
* `new ec2.InstanceType(sfn.JsonPath.stringAt('$.path.to.instanceType'))`, where the value in the task input is an EC2 instance type prepended with "ml.".
* To provide an instance type from the task input, supply an instance type in the following way
* where the value in the task input is an EC2 instance type prepended with "ml.":
*
* ```ts
* new ec2.InstanceType(sfn.JsonPath.stringAt('$.path.to.instanceType'));
* ```
* @see https://docs.aws.amazon.com/sagemaker/latest/APIReference/API_ResourceConfig.html#sagemaker-Type-ResourceConfig-InstanceType
*
* @default ec2.InstanceType(ec2.InstanceClass.M4, ec2.InstanceType.XLARGE)
Expand Down
Loading

0 comments on commit d5c7493

Please sign in to comment.