/elb-cloudwatch

Primary LanguagePythonMIT LicenseMIT

ELB BackendSuccessRate

A custom CloudWatch metric for calculating:

BackendSuccessRate = HTTPCode_Backend_2XX / (HTTPCode_Backend_2XX + HTTPCode_Backend_5XX)

Development

Set appropriate values in config.yaml. To run without publishing to CloudWatch:

make invoke  # Runs `STUB=true python index.py`

Local Lambda Toolkit

A set of conventions for local AWS Lambda software development.

Directory Structure

.
├── Makefile                           # Definition of `make` targets.
├── builds                             # Builds directory.
│   ├── deploy-2016-08-15_16-50.zip
│   └── deploy-2016-08-15_16-54.zip
├── cloudformation                     # CloudFormation template and parameters.
│   └── parameters.json
│   └── template.yaml
├── index.py                           # Entry point for the Lambda function.
├── my_lambda_package                  # Python package `my_lambda_package`.
│   ├── __init__.py
│   ├── localcontext.py
│   ├── utility.py
├── requirements                       # External dependencies.
│   ├── common.txt
│   ├── dev.txt
│   └── lambda.txt
└── tests                              # Unit tests for the package.
    ├── __init__.py
    └── my_lambda_package
        ├── __init__.py
        ├── test_localcontext.py
        └── test_utility.py

1. Initial AWS Setup

Creates a CloudFormation stack with the Lambda function, an execution role, and an optional CloudWatch event to run on a recurring basis.

make create-stack

For updates:

make update-stack

2. Initial Local Setup

Sets up your local environment for local Python development.

IAM

Assume the role created by CloudFormation by creating a new entry in ~/.aws/config:

# ~/.aws/config

[profile regular]
output = json
region = us-west-2

[profile development]
output = json
region = us-west-2
source_profile = regular
role_arn = arn:aws:iam::111111111111:role/LambdaFunctionExecutionRo-34K8PIBFMONR

Set development as the current profile via export AWS_PROFILE=development.

Python

Installs the development requirements from requirements/dev.txt.

In a new virtualenv (pyenv-virtualenv is great), install the local dependencies:

cd local-lambda-toolkit
make init

3. Run tests

Runs all the unit tests in the tests/ directory.

make test

4. Invoke the function locally

Runs the Python code on your local machine.

make invoke

5. Build a package for Lambda

Creates a deployable Lambda zip file, and places into builds.

make build

6. Deploy

Sends the build to a Lambda ARN.

ARN=arn:aws:lambda:us-west-2:111111111111:function:my-function-name make deploy