- Newest
- Most votes
- Most comments
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.
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?
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:
- Go to the AWS SAR page for the layer that you want to deploy.
- Click the CloudFormation template link.
- Copy the template code.
- Open a CFN template file in a text editor.
- Paste the template code into the file.
- Replace the REPLACE_WITH_YOUR_ACCOUNT_ID placeholder with your account ID.
- 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:
- Install the AWS CLI.
- Run the following command to list the available layers:
aws serverlessrepo list-layers
- Find the ARN of the layer that you want to deploy.
- 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.
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 V2aws 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 theaws serverlessrepo
command (it's on theaws lambda
command). Nonetheless, your post did help me find a solution which I will be posting as a answer soon.
Relevant content
- asked 5 years ago
- Accepted Answerasked 5 years ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 3 years ago
- AWS OFFICIALUpdated 6 months ago
- AWS OFFICIALUpdated 8 months ago
Thanks. That's what I do now to USE the layer (I grab the ARN from the deployed CFN resource using the console). I want to automate the deployment process (and retrieval of the ARN) so I don't need to use the console.
Hi, above does not use the console, as it handles provisioning via CFN. You could store a configuration or mapping which Holds the information of the layer ARN WITHIN your cloud formation template, as the layer ARN is a static string.
If you instead want to dynamically retrieve the layer ARN from the SAR site, you can try and look into this: https://docs.aws.amazon.com/serverlessrepo/latest/devguide/serverlessrepo-api-permissions-ref.html or CLI https://docs.aws.amazon.com/cli/latest/reference/serverlessrepo/list-applications.html to list application programmatically
@alatech Thanks for your links that you posted. They helped get me down the road. I will be posting a detailed answer soon.