Reputation: 5944
I am trying to achieve some automation using AWS CLI,
wherein I am able to update my AWS Lambda function using cli command -
aws lambda update-function-code --function-name --zip-file
And add a version to the layer using the command -
aws lambda publish-layer-version --layer-name wor --compatible-runtimes nodejs8.10 --zip-file
What I want to achieve is, Link the lastest version of a particular Layer to the Function,
Something like -
aws lambda get-layer-version --layer-name some_name_here --version-number $LATEST
How can I do so?
Upvotes: 6
Views: 5966
Reputation: 43
I'm using Windows Powershell to update all layers of a specific AWS Lambda function. First, I query the versions for the specific layer and save it into an object variable, then I create another variable that contains the exact version number. And finally I update the layers of my Lambda function.
I know that it's not the best solution, I'm not a Powershell guru, but this script works well :)
$CommonJson = aws lambda list-layer-versions --layer-name my-lib-common | ConvertFrom-Json
$RecentCommonLayerVersion = $CommonJson.LayerVersions[0].Version
$LibCommonJson = aws lambda list-layer-versions --layer-name aws-lib-common | ConvertFrom-Json
$RecentLibCommonLayerVersion = $LibCommonJson.LayerVersions[0].Version
aws lambda update-function-configuration --function-name notifyClients --layers `
"arn:aws:lambda:us-east-1:<accountId>:layer:my-lib-common:$RecentCommonLayerVersion" `
"arn:aws:lambda:us-east-1:<accountId>:layer:aws-lib-common:$RecentLibCommonLayerVersion"
Upvotes: 0
Reputation: 1593
Technically Shawn'd answer is correct but I've managed to work around it with
aws lambda list-layer-versions --layer-name <function> --region <region> --query 'LayerVersions[0].LayerVersionArn'
Upvotes: 1
Reputation: 3249
Not exactly the answer you are looking and @shawn has answered it already in the question's context.
But here's what I have, as I was also trying to do the same and end up building a plugin for the JetBrains IDE.
Have a look if it helps in your day 2 day lambda functions and layers deployments.
This plugin helps in deploying the AWS Lambda function and layers build right from the JetBrains IDE.
Supports deployment of AWS lambda functions.
Supports management of AWS Lambda Layers.
Create new versions, delete old versions and update lambda functions to any layer version right from the IDE.
Link: https://plugins.jetbrains.com/plugin/14742-aws-lambda-deployer
Links:
Youtube Demo 1 (25 July, 2020)
Youtube Demo 2 (27 March, 2021)
Upvotes: 0
Reputation: 9402
It isn't likely the answer you were looking for, but this is not something supported. So you can't do that. Layers are designed to be consumed via a specific version, and AWS doesn't provide a "latest" option that will always cause lambdas to pick up and use the latest layer version. This documentation says:
You choose a specific version of a layer to use. If you want to use a different version later, update your function's configuration.
and
You must specify the version of each layer to use by providing the full ARN of the layer version.
and
Functions that use the layer refer directly to a layer version.
To be a valid ARN, it needs to "uniquely identify" a resource, and "latest" would not do that. See https://docs.aws.amazon.com/IAM/latest/UserGuide/list_awslambda.html#awslambda-resources-for-iam-policies.
It goes on to note that
"When you delete a layer version, you can no longer configure functions to use it. However, any function that already uses the version continues to have access to it. Version numbers are never re-used for a layer name."
In order to allow functions that already use a version to keep working, they must keep the "deleted" version somewhere, and only when there are no more consumers do they actually delete it. If they were to allow "latest" there would be no direct way to determine if a "deleted" layer version was still referenced. And if they allowed the layer code to change, no doubt there would be many cases where other code would unexpectedly break due to the layer changes, and they want to try to prevent that sort of thing.
Your best bet will be to add some code to look up the layers and find the most recent version number to use in subsequent calls.
Upvotes: 7