Serverless Python and Example App @WIP

Posted: 2017-12-31 11:25:09

There are good docs on this http://serverless.com platform. For Python

I will cover some items here I need to do time after time.

  • Tagging
  • ENV settings
  • Testing

Testing

Example file test_cf_backuper.py:

import unittest
import mock
from mock import MagicMock
from get_buckets import GetBuckets
import boto3
real_client = boto3.client('cloudformation')
import logging
import json
from CFBackuper import CFBackuper

logging.basicConfig()
log = logging.getLogger()
log.setLevel(logging.DEBUG)


class TestHandler(unittest.TestCase):

    def test_get_one_template(self):
        client = CFBackuper()
        results = client.handle()
        self.assertTrue(results)

This allows me to mock the client for example of boto, more on that later.

python -m unittest test_cf_backuper.TestHandler.test_get_one_template

Pip Install

Along the way I make a requirments.txt for example

requests
boto3
mock
logging
python-dotenv

then

pip install -r requirements.txt

gets me setup locally

AWS Cli

It is key to have your profile setup per their docs here

Then shift the default as needed:

export AWS_DEFAULT_PROFILE=profile_name_here

Or in the servless.yml file:

provider:
  name: aws
  runtime: python2.7
  profile: profile_name_here

More info here