简体   繁体   中英

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.

I want to associate a specific Lambda alias to the DynamoDB table as its trigger.

I have this code so far which is working perfectly fine for the Lambda's $LATEST version:

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!


[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. 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 . 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. And that's because IFunction has no such feature.

This last code will result in the $LATEST version of the lambda to be used. In other words, I need an IAlias instance for this to work and not an IFunction instance.

Your first code is almost right - to import a Version, use its 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 .

Function.fromFunctionArn takes in 3 parameters, with the last one being the Lambda function ARN.

You can import any Lambda alias you want with the below ARN format:

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

eg if your function ARN is:

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

the ARN for the Lambda alias named Alias1 will be:

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.

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,
}));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM