We use Lambda aliases to manage versions in our environments. The _dev_ alias always points to $LATEST, while _test_ and _prod_ points to a specific version number. When I've made a new deploy to dev and made sure that it works as intended I need to up the _test_ alias to the latest version number, but I find it quite hard to retrieve. It seems the only way to get it via aws-cli is to loop over the lambda list-versions-by-function
command until I reach the last page and then echo out the last item in the list. Is there an easier way? I would be nice if the version number was listed in the list-aliases
command. Currenty it gives me version numbers for _test_ and _prod_, but only $LATEST for _dev_.
Hi @pjarts, $LATEST is a version, so it is expected that list-aliases would return that for your "dev" alias. If you want to test a specific snapshot while continuing to change $LATEST, publish a new version first (and maybe give it a description indicating that you plan to test something). If you do this by running the publish-version command in the CLI, the output should contain the new version number that you can then use to set your test alias.
Thank @klaytaybai. Our problem was that publishing new versions and changing the alias pointer was done in two different places, but thanks to your guidance with the publish-version command I could tweak the publish system to display the latest published version. However, I do think it's inconvenient to get the lambda version in ascending order when running the list-versions-by-function command. In the console, they are listed in descending order.. A --sort
option would be handy 馃憤
Hi @pjarts, sorry for a delay in follow-up. You can get the max version by utilizing the query options and JMESPath. See CLI global options and JMESPath Specification.
aws lambda list-versions-by-function --function-name <value> --query "max_by(Versions, &Version).{Version:Version}"
FYI, there is also a sort_by and reverse function.
Hi @klaytaybai, thanks for your response and I'm sorry for this late reply. Just wanted to add that jmespath queries will only operate on the result of the current page. If there is more than one page of results the --query
option with the query you provided won't return the correct version until you reach the last page.
Hi @klaytaybai, I'm also interested in getting the newest of published version efficiently. And I guess @pjarts is right. JMESPath solution is only getting the max value within the pagenated result.
After commenting that, I tried aws lambda publish-version
several times, and when the code is not changed, the version is not incremented and get the up-to-date version number.
So, we can get the latest version invoking aws lambda publish-version
even if the lambda code is not changed.
The solution aws lambda list-versions-by-function --function-name <value> --query "max_by(Versions, &Version).{Version:Version}"
provided by @klaytaybai is incorrect if you have more than 9 versions because it sorts alphabetically, e.g. "10" is LESS THAN "9".
Thanks for the corrections. I'm going to reopen this and see it resolved to a simple solution.
Is this solution effective?
aws lambda list-versions-by-function --function-name <value> --query "Versions[-1].[Version]"
As you all know, the "query" CLI option is only formatted at the time of output, so it will eventually reach its limit.
@mokoaki Yes that works for me!
@mokoaki THANK YOU!
It looks like you can add the option --no-paginate
in order to get the entire result set in one request.
Looks like this is resolved. Thanks everyone for your contributions!
Most helpful comment
Is this solution effective?
As you all know, the "query" CLI option is only formatted at the time of output, so it will eventually reach its limit.