CFN or CLI to deploy lambda layer from Serverless Application Repository?

0

My project is using canvas-nodejs from the AWS Serverless Application Repository. When at the referenced page, you click deploy, the layer is installed into your current account. Once this is done you can get its ARN and use it as a layer in your Cloudformation file. I gather that this happens using a SAM template--I see a stack in my account that creates the lambda layer resource.

I'd like to do this automatically (via Cloudformation or CLI) when I build my application (no user button click required). I should be able to query the SAR for info on this layer and use that information to create a shell script or CFN file to deploy the layer (and get a reference to it for my subsequent Lambda creations). I see serverlessrepo stuff in the CLI manual but get permission errors (as admin?) when I try to run any of it and I'm not sure if this is the way to go.

Lost and would appreciate some pointers on this--there's got to be a way to automate it.

已提問 1 年前檢視次數 583 次
3 個答案
0
已接受的答案

OK, I'll answer my own question. If accepting it as the correct answer is viewed as gauche, please let me know in the comments. The answers provided by @Neerajskydiver and @Antonio_Lagrotteria didn't do the trick, but they contained clues that led me to a solution.

I've written a blog post on this topic and created a companion GitHub repo to demonstrate the automatic deployment of the layer from the Serverless Application Repository. I'll just summarize here.

First, get the ARN of the layer from the Serverless Application Repository. I'm using lambda-layer-canvas-nodejs. The ARN is in tiny text directly under the layer name at the top of the page.

Second, use the ARN to download a CloudFormation template that you can use to deploy the layer to your account:

aws serverlessrepo create-cloud-formation-template     \
    --application-id SOURCE_ARN_FROM_SERVERLESS_REPO   \
    --query='TemplateURL'                              \
    | xargs curl > TEMPORARY_CFN_FILE.yml

Finally, use CloudFormation, with the downloaded template, to instal the layer into your account:

aws cloudformation deploy                              \
    --stack-name DEPLOY_LAYER_STACK_NAME               \
    --template-file TEMPORARY_CFN_FILE.yml

Please see the blog post for all the details—and there are a lot of details.

已回答 1 年前
0

Hi,

One option could be, as pointed out here: https://docs.aws.amazon.com/serverlessrepo/latest/devguide/sharing-lambda-layers.html,

To reference the arn under the layers section:

Resources:
  MyFunction:
    Type: AWS::Serverless::Function
    Properties:
      Handler: index.handler
      Runtime: python3.7
      CodeUrl: source/app-code/
      Layers:
        - arn:aws:lambda:us-east-1:012345678901:layer:shared-layer:1

This case would fork if you can just keep track of layer arns. Does it fit your case?

profile picture
專家
已回答 1 年前
0

There are two ways to deploy a Lambda layer from the AWS Serverless Application Repository (SAR) depending on your preferences and familiarity:

  • Using the AWS CloudFormation (CFN) template
  • Using the AWS Command-Line Interface (CLI)

Using the AWS CloudFormation template

The AWS SAR provides a CloudFormation template for each layer. You can use this template to deploy the layer to your account. To do this, follow these steps:

  1. Go to the AWS SAR page for the layer that you want to deploy.
  2. Click the CloudFormation template link.
  3. Copy the template code.
  4. Open a CFN template file in a text editor.
  5. Paste the template code into the file.
  6. Replace the REPLACE_WITH_YOUR_ACCOUNT_ID placeholder with your account ID.
  7. Deploy the template using the aws cloudformation deploy command.

Using the AWS CLI

You can also use the AWS CLI to deploy a Lambda layer from the AWS SAR. To do this, follow these steps:

  1. Install the AWS CLI.
  2. Run the following command to list the available layers:
aws serverlessrepo list-layers
  1. Find the ARN of the layer that you want to deploy.
  2. Run the following command to deploy the layer:
aws serverlessrepo deploy-layer --layer-arn <layer-arn>

If you're experiencing difficulties deploying a Lambda layer from the AWS SAR, you should check if you are using the correct account ID. Next, verify that the layer you are attempting to deploy is available in your designated region. Lastly, double-check that you possess the necessary permissions to deploy layers.

已回答 1 年前
  • Thanks for the detailed response. I'd like to reiterate that I want to do all of this in a CI/CD process, so I can not use the console. I could do everything you suggest in "Using CFN Template", but if I'm in the console I may as well just hit the Deploy button and manually copy the ARN as I do now.

    Second, I do not see list-layers as an option in the AWC CLI V2 aws serverlessrepo command?

    Thanks again for taking the time to post this detailed reply.

  • Hi Los Alamos Al,

    Thanks for your feedback. I understand that you want to automate the deployment of the Lambda layer from the AWS Serverless Application Repository (SAR) in your CI/CD process.

    To do this, you can use the AWS CLI to deploy the layer from a shell script or CFN file. The following code snippet shows how to deploy the layer from a shell script:

    #!/bin/bash
    
    # Install the AWS CLI
    aws-cli
    
    # List the available layers
    aws serverlessrepo list-layers
    
    # Find the ARN of the layer that you want to deploy
    layer_arn=$(aws serverlessrepo list-layers | jq -r ".Layers[].Arn")
    
    # Deploy the layer
    aws serverlessrepo deploy-layer --layer-arn $layer_arn
    

    To deploy the layer from a CFN file, you can use the following code snippet:

    {
      "AWSTemplateFormatVersion": "2010-09-09",
      "Resources": {
        "MyLambdaLayer": {
          "Type": "AWS::Serverlessrepo::Layer",
          "Properties": {
            "LayerName": "MyLambdaLayer",
            "ContentUri": "https://example.com/my-lambda-layer.zip"
          }
        }
      }
    }
    

    Once you have created the shell script or CFN file, you can deploy the layer by running the following command:

    # Deploy the layer from a shell script
    bash deploy_lambda_layer.sh
    
    # Deploy the layer from a CFN file
    aws cloudformation deploy -f deploy_lambda_layer.yaml
    

    I hope this helps!

  • @Neerajskydiver Thanks again for taking the time to post a detailed response. However, as I mentioned in my first comment, list-layers is not a subcommand on the aws serverlessrepo command (it's on the aws lambda command). Nonetheless, your post did help me find a solution which I will be posting as a answer soon.

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南