Deploy to AWS Lambda from Travis CI

AWS Credentials for Testing and Deploying Lambda Functions from Travis-CI

Contributed by Rich

These steps were used to setup, test and deploy Lambda functions which use AWS services in the code. Since deploying to production and development will be using different AWS accounts, different AWS credentials are necessary.

Configure a repo at with AWS Credentials

First a repo that will Travis-CI for testing and deployment needs to be activated by going to (see Making Repo Travis CI and Coveralls Compliant for details).

Next, go to the settings for that repo a Here environment variables that will be accessible when a repo is installed and tested. For each branch that has different AWS credentials, make three variables prefixed with the branch name and in all lowercase such as follows:

  • master_aws_region us-west-2
  • master_aws_access_key_id blahblah
  • master_aws_secret_access_key blahblahblahblah

Make sure “Display value in Build Log” is OFF

Configure .travis.yml File

Here is a copy of the .travis.yml file that uses the $TRAVIS_BRANCH environment variable to create the AWS_REGION, AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY variables for the given branch:

- python
- '2.7'
- sudo apt-get install -y curl tar
- bash
- eval export AWS_ACCESS_KEY_ID=\$${TRAVIS_BRANCH}_aws_access_key_id
- eval export AWS_SECRET_ACCESS_KEY=\$${TRAVIS_BRANCH}_aws_secret_access_key
- eval export AWS_REGION=\$${TRAVIS_BRANCH}_aws_region
- bash
- pip install coveralls
- coverage run --source=obs test
- coveralls
- provider: script
  skip_cleanup: true
  script: ./apex deploy 
    branch: develop
- provider: script
  skip_cleanup: true
  script: ./apex deploy
    branch: master

There are few important parts to this:

  • The eval command is what will make an new environment variable from the existing $TRAVIS_BRANCH environment variable to get the correct AWS environment variables for this branch.
  • These AWS variables will be available to the python functions.
  • When the deploy: section is called, these variables are also used to determine where to deploy the Lambda functions to.
  • The bash will run a script to install the apex binary in the repo’s root directory for the deploy: section
  • The bash will run a script that will find all your functions with a requirements.txt file and install them. You do not need to modify the script as it runs for all functions/*/requirements.txt files