10

I have a lambda layer which I keep updating. This lambda layer has multiple versions. How can I find the lambda layer ARN with latest version using aws cli?

Punter Vicky
  • 15,954
  • 56
  • 188
  • 315

3 Answers3

17

I am able to do this using the command listed below -

aws lambda list-layer-versions --layer-name <layer name> --region us-east-1 --query 'LayerVersions[0].LayerVersionArn'
Punter Vicky
  • 15,954
  • 56
  • 188
  • 315
  • 8
    I wasn't sure if the first position is always the latest, so I ended up sorting in the query: `aws lambda list-layer-versions --layer-name --region us-east-1 --query 'max_by(LayerVersions, &Version).LayerVersionArn'` – Tulio Casagrande Mar 13 '20 at 16:12
  • 1
    If you look at the full list you will notice the first one is always latest. Also add --output text flag so it returns as straight text instead of text surrouned by quotes – Andy N Jan 14 '21 at 01:33
  • --query is not supported in the list-layer-versions command – node_saini Dec 07 '21 at 14:29
5

Unfortunately, it's currently not possible (I have encountered the same issue).

You can keep the latest ARN in your own place (like DynamoDB) and update it whenever you publish a new version of the layer.

Ronyis
  • 1,803
  • 16
  • 17
1

You can create a custom macro to get the latest lambda layer version and use that as a reference.

The following function gets the latest version from the Lambda Layer stack:

import json
import boto3


def latest_lambdalayer(event, context):
    
    fragment = get_latestversion(event['fragment'])
    return {
      'requestId': event['requestId'],
      'status': 'success',
      'fragment': fragment
    }

def get_latestversion(fragment):
    cloudformation = boto3.resource('cloudformation')
    stack = cloudformation.Stack('ticketapp-layer-dependencies')
    for o in stack.outputs: 
        if o['OutputKey']=='TicketAppLambdaDependency':
            return o['OutputValue']
    #return "arn:aws:lambda:eu-central-1:899885580749:layer:ticketapp-dependencies-layer:16"

And you use this when defining the Lambda layer—here using same global template:

Globals:
  Function:
    Layers:
      - !Transform { "Name" : "LatestLambdaLayer"}
    Runtime: nodejs12.x
    MemorySize: 128
    Timeout: 101
Jeremy Caney
  • 7,102
  • 69
  • 48
  • 77
Nitin Datta
  • 19
  • 1
  • 1