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.