简体   繁体   English

如何使用 AWS CDK 将现有 Lambda 函数的别名指定为 DynamoDB 触发器?

[英]How do I specify an existing Lambda function's alias as a DynamoDB trigger using the AWS CDK?

I'm trying to set a trigger for a DynamoDB table using CDK, with the lambda and its alias being already created in a different stack.我正在尝试使用 CDK 为 DynamoDB 表设置触发器,其中 lambda 及其别名已在不同的堆栈中创建。

I want to associate a specific Lambda alias to the DynamoDB table as its trigger.我想将特定的 Lambda 别名关联到 DynamoDB 表作为其触发器。

I have this code so far which is working perfectly fine for the Lambda's $LATEST version:到目前为止,我有这个代码,它对于 Lambda 的$LATEST版本工作得非常好:

const lambdaArn = `arn:aws:lambda:${stack.region}:${stack.account}:function:SOME_LAMBDA`;
const lamdba = Function.fromFunctionArn(stack, "lambda", lambdaArn);

lamdba.addEventSource(new DynamoEventSource(table, {
    startingPosition: StartingPosition.LATEST,
}));

How can I instead specify a specific alias based on its name?我如何才能根据名称指定特定的别名?


[UPDATE] [更新]

The way I see it, I need to do something like this:在我看来,我需要做这样的事情:

const lambdaArn = `arn:aws:lambda:${stack.region}:${stack.account}:function:SOME_LAMBDA`;
const lamdba = Function.fromFunctionArn(stack, "lambda", lambdaArn);

const version = Version.fromVersionAttributes(stack, "version", {
    lambda: lamdba,
    version: ???,//Supposedly a string
});

const alias = Alias.fromAliasAttributes(stack, "alias", {
    aliasName: "SOME_ALIAS",
    aliasVersion: version,
});

alias.addEventSource(new DynamoEventSource(table, {
    startingPosition: StartingPosition.LATEST,
}));

But the problem with this code is that I don't know how to find the version of the alias in CDK!但是这段代码的问题是我不知道如何在CDK中找到别名的版本!


[UPDATE] [更新]

Perhaps it was my fault not to mention all the different ways I've tried and failed.也许是我的错,没有提到我尝试过和失败的所有不同方式。 But in any case, just simply adding the alias name to the end of the ARN and using the Function.fromFunctionArn does not help.但在任何情况下,仅将别名添加到 ARN 的末尾并使用Function.fromFunctionArn都无济于事。 Here's what I mean:这就是我的意思:

const lambdaArn = `arn:aws:lambda:${stack.region}:${stack.account}:function:SOME_LAMBDA:SOME_ALIAS`;
const lamdba = Function.fromFunctionArn(stack, "lambda", lambdaArn);

lamdba.addEventSource(new DynamoEventSource(table, {
    startingPosition: StartingPosition.LATEST,
}));

The above code behaves just like the one without the SOME_ALIAS .上面的代码就像没有SOME_ALIAS代码一样。 This was my very first attempt at solving this problem.这是我第一次尝试解决这个问题。 And the reason why it does not work is that the returned IFunction has no notion of alias in it.它不起作用的原因是返回的IFunction中没有别名的概念。 And that's because IFunction has no such feature.那是因为IFunction没有这样的功能。

This last code will result in the $LATEST version of the lambda to be used.最后的代码将导致使用 lambda 的$LATEST版本。 In other words, I need an IAlias instance for this to work and not an IFunction instance.换句话说,我需要一个IAlias实例才能工作,而不是一个IFunction实例。

Your first code is almost right - to import a Version, use its ARN:您的第一个代码几乎是正确的 - 要导入版本,请使用其 ARN:

const lambdaArn = `arn:aws:lambda:${stack.region}:${stack.account}:function:SOME_LAMBDA`;
const lamdba = Function.fromFunctionArn(stack, "lambda", lambdaArn);

const versionArn = `arn:aws:lambda:${stack.region}:${stack.account}:function:SOME_LAMBDA:VERSION_NAME`;

const version = Version.fromVersionArn(stack, "version", versionArn);

const alias = Alias.fromAliasAttributes(stack, "alias", {
    aliasName: "SOME_ALIAS",
    aliasVersion: version,
});

You specified the version name when you created your alias.您在创建别名时指定了版本名称。

You're missing 1 main point - each alias has a unique ARN .您缺少 1 个要点 - 每个别名都有一个唯一的 ARN

Function.fromFunctionArn takes in 3 parameters, with the last one being the Lambda function ARN. Function.fromFunctionArn接受 3 个参数,最后一个是 Lambda 函数 ARN。

You can import any Lambda alias you want with the below ARN format:您可以使用以下 ARN 格式导入所需的任何 Lambda 别名:

arn:aws:lambda:${REGION}:${ACCOUNT}:function:${FUNCTION_NAME}:${ALIAS_NAME}

eg if your function ARN is:例如,如果您的函数 ARN 是:

arn:aws:lambda:eu-west-1:585470346692:function:MyFunction

the ARN for the Lambda alias named Alias1 will be:名为Alias1的 Lambda 别名的 ARN 将是:

arn:aws:lambda:eu-west-1:585470346692:function:MyFunction:Alias1

Set the event source to the Lambda alias by importing the alias for your Lambda function using the alias ARN & then adding the event source to the imported alias.通过使用别名 ARN 导入 Lambda 函数的别名,然后将事件源添加到导入的别名,将事件源设置为 Lambda 别名。

This will work:这将起作用:

const lambdaArn = `arn:aws:lambda:${stack.region}:${stack.account}:function:SOME_LAMBDA`;

const aliasName = "SOME_ALIAS";
const aliasArn = lambdaArn + ":" + aliasName;

const lambdaAlias = Function.fromFunctionArn(stack, "lambda", aliasArn);

lambdaAlias.addEventSource(new DynamoEventSource(table, {
    startingPosition: StartingPosition.LATEST,
}));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 aws cdk (JAVA) 每 15 分钟创建一个 cloudwatch 规则来触发 lambda function? - How can i create a cloudwatch rule to trigger a lambda function every 15 minutes using aws cdk (JAVA)? 如何使用 CloudFormation 或 CDK 将另一个 AWS 账户的 Event Bus 指定为 EventBridge 规则的目标? - How do I specify another AWS account's Event Bus as a target of an EventBridge rule using CloudFormation or CDK? 如何使用 AWS CDK 将域别名添加到现有 CloudFront 分配 - How to add domain alias to existing CloudFront distribution using AWS CDK AWS CDK Typescript,如何从 lambda 触发步进函数? - AWS CDK Typescript, how to trigger step function from lambda? aws cdk 没有为 lambda 创建 s3 触发器 - aws cdk not creating s3 trigger for lambda 如何使用 AWS CLI 向 AWS Lambda 函数添​​加触发器? - How do I add trigger to an AWS Lambda function using AWS CLI? 如何使用AWS SDK PHP将触发器添加到AWS Lambda函数? - How do I add trigger to an AWS Lambda function using AWS SDK PHP? 使用特定别名使 AWS IoT 规则触发 Lambda 函数 - Make AWS IoT rule trigger Lambda function using specific alias 如何使用 AWS CDK 中的 InvokeLambda 将 JSON 传递给 AWS StepFunction 中的 lambda - How do I pass JSON to lambda in AWS StepFunction using InvokeLambda in AWS CDK 只有在 S3 上完成批量上传后,如何触发 AWS lambda 函数? - How do I trigger a AWS lambda function only if bulk upload finished on S3?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM