Deploying AWS Lambdas with an NPM script or bash


Requires: AWS CLI (https://aws.amazon.com/cli) installed and working.

Update December 2020: use this bash script instead.

This the best version of the script so far.  Updates / improvements welcome.

You can get the ARN from the AWS website or you can run this code:
  aws lambda get-function --function-name <functionNameGoesHere> | grep FunctionArn

Then edit your package.json such that the "scripts" portion looks like this (append to, don't overwrite, existing keys):

  "scripts": {
    "deploy": "aws lambda update-function-code --function-name arn:some-crazy-numbers:function:hello-world --zip-file fileb://lambdaDeployment.zip",
    "predeploy": "rm -f lambdaDeployment.zip; zip -r lambdaDeployment.zip * -x *.git* *.zip package*.json *.log '*node_modules*' '*.DS_Store' '*spec/*'"
  },

OR, if you have all of your files in a 'src' directory, use these:
  "scripts": {
    "deploy": "aws lambda update-function-code --function-name arn:some-crazy-numbers:function:hello-world --zip-file fileb://lambdaDeployment.zip",
    "predeploy": "rm -f lambdaDeployment.zip; zip -r lambdaDeployment.zip . -i package*.json "src/*" "node_modules/*"
  },


The above whitelists the files to add to the zip and is more useful if you have configuration files (e.g. .eslint.rc) in the root of your project.

To use it, run:
npm run deploy

Comments

Popular posts from this blog

Optional object property access

A short guide to cross-site request forgery attacks

How is an application like a bride's outfit? - 1 minute read