gpt4 book ai didi

typescript - AWS CDK Typescript,如何从 lambda 触发步进函数?

转载 作者:行者123 更新时间:2023-12-04 13:15:55 24 4
gpt4 key购买 nike

我试图从 lambda 触发阶跃函数,所以我有这个配置:


let DIST_FOLDER = path.join(__dirname, "dist");

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

// State Machine code

let executorLambda = new lambda.Function(this, "executorFunction", {
runtime: lambda.Runtime.NODEJS_12_X,
handler: "main.handler",
code: new lambda.AssetCode(path.join(DIST_FOLDER, "executor-lambda")),
timeout: Duration.seconds(60)
});

let executorTask = new Task(this, "executorTask", {
task: new InvokeFunction(executorLambda)
});

let chain = Chain.start(executorTask);

let stateMachine = new StateMachine(this, "executorStateMachine", {
definition: chain
});

// Back-end and api
let backend = new lambda.Function(this, "backend", {
runtime: lambda.Runtime.NODEJS_12_X,
handler: "main.handler",
code: new lambda.AssetCode(path.join(DIST_FOLDER, "backend-lambda")),
environment: {
STEP_FUNCTION_ARN: stateMachine.stateMachineArn
}
});

new apigateway.LambdaRestApi(this, "strest-api", { handler: backend });
}
}

我的 api 网关连接的 lambda 是:

import { StartExecution } from "@aws-cdk/aws-stepfunctions-tasks";

export async function handler(event: any, context: any) {
let env = process.env;
let STEP_FUNCTION_ARN = env.STEP_FUNCTION_ARN || "STEP_FUNCTION_ARN";
let body = JSON.stringify({
msg: "Hello world",
stepFunctionArn: STEP_FUNCTION_ARN
});

let stateMachineExecution = new StartExecution({ // Here I get an error, I don't know how to pass the correct step function arn or resource
stateMachineArn: STEP_FUNCTION_ARN
})

return {
statusCode: 200,
body
};
}

有没有人有这方面的经验?

最佳答案

问题似乎出在您的 Lambda 函数中:

import { StartExecution } from "@aws-cdk/aws-stepfunctions-tasks";
StartExecution您从 CDK 导入的实际上是基础结构构造。为了再次调用 AWS 的 API,您需要 AWS 开发工具包:
import { StepFunctions } from 'aws-sdk'

const stepfunctions = new StepFunctions();

export async function handler(event: any, context: any) {
...

stepfunctions.startExecution({
stateMachineArn: STEP_FUNCTION_ARN,
name: '...',
input: JSON.stringify({msg: 'Hello World!'})
})

...
}

the respective docs想要查询更多的信息。并确保为您的 Lambda 提供调用相关 Step Functions 的必要权限。

希望有帮助!

关于typescript - AWS CDK Typescript,如何从 lambda 触发步进函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60512738/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com