How can I download/pull lambda code to a local machine from command line?

2.3k views Asked by At

I am using the sam deploy command with the AWS SAM command line tool to deploy. Now I made some changes with the web IDE in the AWS Console. How can I pull the changes to the local machine, so that the next sam deploy command won't override them? (I am looking for something similar to a git pull I guess)

2

There are 2 answers

0
Chris Williams On BEST ANSWER

To do this you will need to use the AWS CLI, the start of this process will require you to use the get-function function in the AWS CLI.

This will return a pre signed URL in the Code > Location structure, if you then download this (using a CLI tool such as curl) you can then download a zip file containing the contents of the Lambda function.

The expected function would look similar to the below

curl $(aws lambda get-function --function-name $FUNCTION_NAME --output text --query "Code.[Location]")
0
Mark B On

You should have a single source of truth for your source code. And that should really be your source control repository (Git). If you make changes to your source code in the web IDE then you should copy those changes into your Git repo.

To your original question, to download a Lambda function's source code from the command line, you would use the aws lambda get-function --function-name command to download information about the function. Part of the information included in the response is a URL to download the function's deployment package, which is valid for 10 minutes. Then you could download the deployment package at that URL using something like curl.