Skip to content

Instantly share code, notes, and snippets.

@RabinMallick
Forked from tdmalone/.travis-lambda-node.yml
Created December 12, 2018 02:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RabinMallick/1a7c580df6041a503fc3b5afe3b3a747 to your computer and use it in GitHub Desktop.
Save RabinMallick/1a7c580df6041a503fc3b5afe3b3a747 to your computer and use it in GitHub Desktop.
.travis.yml example for Node.js Lambda microservice testing and deployment
language: node_js
services: docker
node_js: 6.10
env:
global:
- AWS_ACCESS_KEY_ID=...
- AWS_DEFAULT_REGION=ap-southeast-2
- LAMBDA_NAME=...
- LAMBDA_DESCRIPTION=...
- LAMBDA_TIMEOUT=...
- LAMBDA_ROLE=arn:aws:iam::873114526714:role/genericLambdaRole
- LAMBDA_ALIAS=prod
- LAMBDA_RUNTIME=nodejs6.10
- LAMBDA_MODULE=index
- LAMBDA_HANDLER=handler
- NPM_EMAIL=tdmalone@gmail.com
# AWS_SECRET_ACCESS_KEY
- secure: ...
# NPM_TOKEN
- secure: ...
cache:
yarn: true
directories:
- node_modules
install:
# Install dependencies.
- yarn
# Install Terraform for checking infrastructure configuration.
- curl --location https://releases.hashicorp.com/terraform/0.11.5/terraform_0.11.5_linux_amd64.zip > terraform.zip
- unzip terraform.zip
script:
- yarn lint
- yarn test
# Check that Terraform config files are formatted correctly and that they validate.
- ./terraform fmt -check
- ./terraform init
- ./terraform validate
# If state is stored remotely, we can also check that the state is up-to-date - i.e. that no
# changes have been pushed without being applied.
#
# We only care about the exit code here, and don't want to expose any potentially sensitive data,
# so we also redirect the output.
#
# THIS SHOULD BE COMMENTED OUT IF STATE IS MANAGED LOCALLY, WHICH IS THE DEFAULT IN TERRAFORM.
# FOR SECURITY REASONS, STATE SHOULD NEVER BE COMMITTED TO THE REPOSITORY.
#
#- ./terraform plan -detailed-exitcode > /dev/null
before_deploy:
# Remove the AWS SDK as it's already available in the Lambda environment.
- yarn remove aws-sdk
# Clean up dependencies + unneeded files, and reinstall production dependencies only.
- rm -rf coverage node_modules tests .*rc.js
- yarn --prod
deploy:
# Deploy to $LATEST on dev branch. (i.e. don't publish a new version).
- on:
branch: dev
publish: false
provider: lambda
function_name: $LAMBDA_NAME
region: $AWS_DEFAULT_REGION
role: $LAMBDA_ROLE
description: $LAMBDA_DESCRIPTION
runtime: $LAMBDA_RUNTIME
timeout: $LAMBDA_TIMEOUT
module_name: $LAMBDA_MODULE
handler_name: $LAMBDA_HANDLER
access_key_id: $AWS_ACCESS_KEY_ID
secret_access_key: $AWS_SECRET_ACCESS_KEY
skip_cleanup: true
# Deploy and publish a new version on master branch.
- on:
branch: master
publish: true
provider: lambda
function_name: $LAMBDA_NAME
region: $AWS_DEFAULT_REGION
role: $LAMBDA_ROLE
description: $LAMBDA_DESCRIPTION
runtime: $LAMBDA_RUNTIME
timeout: $LAMBDA_TIMEOUT
module_name: $LAMBDA_MODULE
handler_name: $LAMBDA_HANDLER
access_key_id: $AWS_ACCESS_KEY_ID
secret_access_key: $AWS_SECRET_ACCESS_KEY
skip_cleanup: true
# Deploy to npm on master with a new tag.
- on:
branch: master
tags: true
provider: npm
email: $NPM_EMAIL
api_key: $NPM_TOKEN
skip_cleanup: true
after_deploy:
# Set a Lambda alias to the most recently deployed version.
- if [ "master" = "${TRAVIS_BRANCH}" ]; then
pip install awscli --upgrade --user;
export MOST_RECENT=$(aws lambda list-versions-by-function --function "${LAMBDA_NAME}" --max-items 10000 | node -e "let stdin=''; process.stdin.on('data',(chunk)=>{stdin+=chunk}).on('end',()=>{console.log(JSON.parse(stdin).Versions.pop().Version)})");
aws lambda update-alias --function-name "${LAMBDA_NAME}" --name "${LAMBDA_ALIAS}" --function-version "${MOST_RECENT}";
fi;
notifications:
email: false
webhooks:
urls: https://api.tm.id.au/v2/travis/jobStatus
slack:
on_start: always
rooms:
- secure: ...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment