我在玩AWS CDK (打字稿),以便自我学习.具体来说,我想让lambda函数每天在特定的时间执行,或者说每N分钟执行一次。特别是我认为我希望有许多这样的函数,所以使用封装lambda函数的构造、EventBridge规则以及一些与之配套的日志记录工具可能不是个坏主意。
我没有编写自己的构造,而是意识到存在事件桥-lambda,所以我尝试了一下。在我的项目中,根中有一个lambda文件夹,其中包含hello.py,它具有非常简单的lambda_handler定义。然后,我的堆栈如下:
import { Stack, StackProps, Duration } from 'aws-cdk-lib';
import { Construct } from 'constructs';
import { EventbridgeToLambdaProps, EventbridgeToLambda } from '@aws-solutions-constructs/aws-eventbridge-lambda';
import * as lambda from 'aws-cdk-lib/aws-lambda';
import * as events from 'aws-cdk-lib/aws-events';
export class TimedLambdaStack extends Stack {
constructor(scope: Construct, id: string, props?: StackProps) {
super(scope, id, props);
const constructProps: EventbridgeToLambdaProps = {
lambdaFunctionProps: {
code: lambda.Code.fromAsset('lambda'),
runtime: lambda.Runtime.PYTHON_3_9,
handler: 'hello.lambda_handler'
},
eventRuleProps: {
schedule: events.Schedule.rate(Duration.minutes(5))
}
};
new EventbridgeToLambda(this, 'test-eventbridge-lambda', constructProps);
}
}我的cdk部署很好。我是通过一个管道做的,该管道基本上是从CDK讲习班复制的。
import * as cdk from 'aws-cdk-lib';
import * as codecommit from 'aws-cdk-lib/aws-codecommit';
import { Construct } from 'constructs';
import {CodeBuildStep, CodePipeline, CodePipelineSource} from "aws-cdk-lib/pipelines";
export class TimedPipelineStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
const repo = new codecommit.Repository(this, 'TimedRepo', {
repositoryName: "TimedRepo"
});
const pipeline = new CodePipeline(this, 'Pipeline', {
pipelineName: 'TimedLambdaPipeline',
synth: new CodeBuildStep('SynthStep', {
input: CodePipelineSource.codeCommit(repo, 'master'),
installCommands: [
'npm install -g aws-cdk'
#'npm install -s @aws-solutions-constructs/aws-eventbridge-lambda'
],
commands: [
'npm ci',
'npm run build',
'npx cdk synth'
]
}
)
});
}
}查看结果的CloudFormation堆栈,我很困惑。我看到有一个EventBridge规则,但是没有提供Lambda函数。
我是误解了这个构造是为了什么,还是做了一些愚蠢的事情?
发布于 2022-06-15 09:23:37
你的管道没有部署任何东西。要让管道部署堆栈,需要向其添加阶段。
在你链接的CDK讲习班中,它解释了您复制的代码的如下内容:
此时,您有一个完全操作的CDK管道,它将在每次提交时自动更新自己,但现在,这就是它所做的一切。我们需要在管道中添加一个部署应用程序的阶段。
这里有一个来自车间的简单的阶段代码,唯一的改变就是它部署的堆栈的名称。您需要修改导入以反映堆栈定义的位置。
import { TimedLambdaStack } from './timed-lambda-stack';
import { Stage, StageProps } from 'aws-cdk-lib';
import { Construct } from 'constructs';
export class WorkshopPipelineStage extends Stage {
constructor(scope: Construct, id: string, props?: StageProps) {
super(scope, id, props);
new TimedLambdaStack(this, 'TimedLambdaStack');
}
}然后,您需要将这个阶段添加到管道中,如研讨会中所示:
import * as cdk from 'aws-cdk-lib';
import * as codecommit from 'aws-cdk-lib/aws-codecommit';
import { Construct } from 'constructs';
import {WorkshopPipelineStage} from './pipeline-stage';
import {CodeBuildStep, CodePipeline, CodePipelineSource} from "aws-cdk-lib/pipelines";
export class TimedPipelineStack extends cdk.Stack {
constructor(scope: Construct, id: string, props?: cdk.StackProps) {
super(scope, id, props);
// This creates a new CodeCommit repository called 'TimedRepo'
const repo = new codecommit.Repository(this, 'TimedRepo', {
repositoryName: "TimedRepo"
});
// The basic pipeline declaration. This sets the initial structure
// of our pipeline
const pipeline = new CodePipeline(this, 'Pipeline', {
pipelineName: 'TimedLambdaPipeline',
synth: new CodeBuildStep('SynthStep', {
input: CodePipelineSource.codeCommit(repo, 'master'),
installCommands: [
'npm install -g aws-cdk'
],
commands: [
'npm ci',
'npm run build',
'npx cdk synth'
]
}
)
});
const deploy = new WorkshopPipelineStage(this, 'Deploy');
const deployStage = pipeline.addStage(deploy);
}
}https://stackoverflow.com/questions/72621563
复制相似问题