AWS CDK-Lambda-Dynamo-步骤功能

时间:2020-11-06 22:11:23

标签: aws-cdk aws-step-functions

我正在努力开发CDK,以便创建一个启动lambda函数的步进函数,以便将一些数据从GetPocket传入Airtable。

这里是我要在CDK中重现的状态机(我很乐意为工作流程提供建议,但这不是主要问题):

enter image description here

这是我的代码(由于pocket_iteration_job已经是pocket_count_reach_choice的选择,因此无法编译...。

import * as cdk from '@aws-cdk/core';
import * as lambda from '@aws-cdk/aws-lambda';
import * as dynamodb from '@aws-cdk/aws-dynamodb';
import * as sfn from '@aws-cdk/aws-stepfunctions';
import * as tasks from '@aws-cdk/aws-stepfunctions-tasks';
import { Succeed } from '@aws-cdk/aws-stepfunctions';
const config = require ('../config.json');

export class PocketAirtableStack extends cdk.Stack {
  constructor(scope: cdk.Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    const dynamoTable = new dynamodb.Table(this, "pocket_db", {
      billingMode: dynamodb.BillingMode.PROVISIONED,
      sortKey: { name: "timestamp", type: dynamodb.AttributeType.NUMBER },
      partitionKey: { name: "id", type: dynamodb.AttributeType.STRING },
    });
    
    const pocketLambda = new lambda.Function(this, 'pocket', {
      runtime: lambda.Runtime.NODEJS_12_X,    // execution environment
      code: lambda.Code.fromAsset('lambda/pocket'),  // code loaded from "lambda" directory
      handler: 'pocket.handler',                // file is "hello", function is "handler"
      environment: { "pocket_consumer_key": config.pocket_consumer_key, "pocket_access_token": config.pocket_access_token, TABLE_NAME: dynamoTable.tableName},
      timeout: cdk.Duration.seconds(20)
    });

    dynamoTable.grantReadWriteData(pocketLambda);

    const airtableLambda = new lambda.Function(this, 'airtable', {
      runtime: lambda.Runtime.NODEJS_12_X,    // execution environment
      code: lambda.Code.fromAsset('lambda/airtable'),  // code loaded from "lambda" directory
      handler: 'airtable.handler',                // file is "hello", function is "handler"
      environment: { "airtable_api_key": config.airtable_api_key, "airtable_table_id": config.airtable_table_id, "airtable_table_name": config.airtable_table_name}
    });
   
    const define_initial_status_pass = new sfn.Pass(this, 'define_initial_status', {
      result: sfn.Result.fromObject({
        "pocket": {
          "action": "count"
        }
      }),
    });
    const pocket_get_count_job = new tasks.LambdaInvoke(this, 'pocket_get_count', {
      inputPath: '$.pocket',
      lambdaFunction: pocketLambda,
      resultPath: '$.pocket'
    });

    const pocket_iteration_job = new tasks.LambdaInvoke(this, 'pocket_iteration', {
      inputPath: '$.pocket',
      lambdaFunction: pocketLambda,
      resultPath: '$.pocket'
    });

    const airtable_add_job = new tasks.LambdaInvoke(this, 'airtable_add', {
      inputPath: '$.pocket',
      lambdaFunction: airtableLambda,
      resultPath: '$.pocket'
    });

    const verifications_pass = new sfn.Pass(this, 'verifications');

    const pocket_count_reach_choice =  new sfn.Choice(this, 'pocket_count_reach');
    pocket_count_reach_choice.when(sfn.Condition.booleanEquals('$.pocket.iterator.continue', true), pocket_iteration_job)
    pocket_count_reach_choice.otherwise(verifications_pass)

    const chain = sfn.Chain
      .start(define_initial_status_pass)
      .next(pocket_get_count_job)
      .next(pocket_count_reach_choice)
      .next(pocket_iteration_job)
      .next(airtable_add_job)
      .next(pocket_count_reach_choice)

    new sfn.StateMachine(this, 'pocket_airtable_state_machine', {
      definition: chain,
      timeout: cdk.Duration.seconds(30)
    });
  }
}

是否有某种我看不到的方式?

感谢您的帮助

1 个答案:

答案 0 :(得分:0)

我找到了解决方法

您可以在选择后链接步骤。

const pocket_count_reach_choice =  new sfn.Choice(this, 'pocket_count_reach');    pocket_count_reach_choice.when(sfn.Condition.booleanEquals('$.pocket.iterator.continue', true), pocket_iteration_job.next(airtable_add_job).next(pocket_count_reach_choice))
pocket_count_reach_choice.otherwise(verifications_pass)
    
const chain = sfn.Chain
.start(define_initial_status_pass)
.next(pocket_get_count_job).next(pocket_count_reach_choice)