Martin Thoma
Martin Thoma

Reputation: 136197

How can I update Code of AWS Lambda via command line?

Currently, when I update my Lambda function, I

  1. Open https://console.aws.amazon.com/lambda/ in a browser and navigate to the lambda function
  2. Choose 'Code entry type: Upload a file from Amazon S3'
  3. Enter the S3 URL I got before from uploading it via command line aws s3 cp [local] [bucket]

Is there a way to do all of this via command line?

Upvotes: 3

Views: 5440

Answers (2)

Etienne678
Etienne678

Reputation: 454

i like to do it like this:

clear && zip -r function.zip . -x "*.git*" -x ".env" -x "event.json" -x "function.zip" > /dev/null && aws lambda update-function-code --function-name YOUR_FUNCTION_NAME --zip-file fileb://function.zip > /dev/null && rm function.zip

Command Explanation

clear

Clears the terminal screen.

zip -r function.zip . -x "*.git*" -x ".env" -x "event.json" -x "function.zip"

This command creates a zip archive (function.zip) of the current directory (.) while recursively including all files and subdirectories, but excluding:

  • Files and directories matching *.git* (usually Git repository metadata and history).
  • The .env file (commonly used for environment variables).
  • The event.json file (often used for testing events in AWS Lambda).
  • The function.zip file itself to avoid including the zip archive being created in the archive.

The > /dev/null part suppresses the output of the zip command, sending it to the null device, effectively hiding it from the terminal.

aws lambda update-function-code --function-name YOUR_FUNCTION_NAME --zip-file fileb://function.zip

This command updates the AWS Lambda function code.

  • --function-name YOUR_FUNCTION_NAME: Specifies the name of the Lambda function to update. Replace YOUR_FUNCTION_NAME with the actual name of your Lambda function.
  • --zip-file fileb://function.zip: Points to the zip file (function.zip) containing the updated function code to upload.

Again, > /dev/null suppresses the output of this command.

rm function.zip

Deletes the function.zip file after the Lambda function code has been updated.

Upvotes: 1

Martin Thoma
Martin Thoma

Reputation: 136197

$ aws lambda update-function-code \
    --function-name your-lambda-name \
    --s3-bucket your-bucket \
    --s3-key your-key

See update-function-code of the aws command line program.

Upvotes: 7

Related Questions