CDK - 将特定的 lambda 版本传递给步骤函数任务
CDK - Pass specific lambda version to a step function task
我有一个创建 lambda 函数和任务的 CDK 堆栈。每个任务都使用一个 lambda 函数。目前,step function 任务仅指向 $LATEST
别名。我的目标是更新 lambda 函数版本或别名并相应地更新步骤函数任务。
Lambda 创建:
createLambda(scope: cdk.Construct, name: string, lambdaPath: string, handler: string = 'app.handler', timeout: number = 900): lambda.Function {
const lambdaFunction = new lambda.Function(scope, name, {
functionName: name,
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.asset(lambdaPath),
handler: handler,
timeout: Duration.seconds(timeout),
description: `Generated on: ${new Date().toISOString()}`
});
const version = lambdaFunction.addVersion(new Date().toISOString());
new lambda.Alias(scope, `alias-${new Date().toISOString()}`, {
aliasName: 'live',
version: version,
});
return lambdaFunction;
}
任务创建:
createTask(scope: cdk.Construct,lambdaFunction: lambda.Function,duration: number = 1200,name: string): sfn.Task {
const task = new sfn.Task(scope, name, {
task: new tasks.InvokeFunction(lambdaFunction),
timeout: Duration.seconds(duration)
});
return task;
}
所以我的问题是:
我可以在步骤函数任务中指向特定的 lambda 版本吗?
结束使用 lambda ARN,只要 lambda 发生变化,CDK 就会创建新版本并且 return lambda 版本 ARN:
createLambda(scope: cdk.Construct, name: string, lambdaPath: string, handler: string = 'app.handler', timeout: number = 900): string {
const lambdaFunction = new lambda.Function(scope, name, {
functionName: name,
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.asset(lambdaPath),
handler: handler,
timeout: Duration.seconds(timeout),
});
const lambdaFunctionARN = lambdaFunction.addVersion(`${new Date().toISOString()}`).functionArn;
return lambdaFunctionARN;
}
稍后,Step functions 任务获取 ARN 作为输入并使用 :
导入 lambda 函数
const lambdaVersion = lambda.Function.fromFunctionArn(scope,'LambdaImportUsingARN',lambdaVersionARN)
我有一个创建 lambda 函数和任务的 CDK 堆栈。每个任务都使用一个 lambda 函数。目前,step function 任务仅指向 $LATEST
别名。我的目标是更新 lambda 函数版本或别名并相应地更新步骤函数任务。
Lambda 创建:
createLambda(scope: cdk.Construct, name: string, lambdaPath: string, handler: string = 'app.handler', timeout: number = 900): lambda.Function {
const lambdaFunction = new lambda.Function(scope, name, {
functionName: name,
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.asset(lambdaPath),
handler: handler,
timeout: Duration.seconds(timeout),
description: `Generated on: ${new Date().toISOString()}`
});
const version = lambdaFunction.addVersion(new Date().toISOString());
new lambda.Alias(scope, `alias-${new Date().toISOString()}`, {
aliasName: 'live',
version: version,
});
return lambdaFunction;
}
任务创建:
createTask(scope: cdk.Construct,lambdaFunction: lambda.Function,duration: number = 1200,name: string): sfn.Task {
const task = new sfn.Task(scope, name, {
task: new tasks.InvokeFunction(lambdaFunction),
timeout: Duration.seconds(duration)
});
return task;
}
所以我的问题是: 我可以在步骤函数任务中指向特定的 lambda 版本吗?
结束使用 lambda ARN,只要 lambda 发生变化,CDK 就会创建新版本并且 return lambda 版本 ARN:
createLambda(scope: cdk.Construct, name: string, lambdaPath: string, handler: string = 'app.handler', timeout: number = 900): string {
const lambdaFunction = new lambda.Function(scope, name, {
functionName: name,
runtime: lambda.Runtime.NODEJS_10_X,
code: lambda.Code.asset(lambdaPath),
handler: handler,
timeout: Duration.seconds(timeout),
});
const lambdaFunctionARN = lambdaFunction.addVersion(`${new Date().toISOString()}`).functionArn;
return lambdaFunctionARN;
}
稍后,Step functions 任务获取 ARN 作为输入并使用 :
导入 lambda 函数const lambdaVersion = lambda.Function.fromFunctionArn(scope,'LambdaImportUsingARN',lambdaVersionARN)