Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .git_templates/hooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,4 @@ black --config ./pyproject.toml .

# Run pylint and check the score
echo "pre-commit: Running lint check"
pylint --rcfile=./pylintrc projectname/ tests/
pylint --rcfile=./pylintrc src/projectname tests/unittests
2 changes: 1 addition & 1 deletion .git_templates/hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

# run the unittests if it fails abort the push
echo "pre-push: Run unittest"
coverage run --source=projectname -m pytest tests/unittests/
coverage run --source=src/projectname -m pytest tests/unittests/

# Above command doesn't exit when unittests fails
# It needs to be caught and exit non 0 to abort the push
Expand Down
54 changes: 54 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
dist: xenial
language: python

branches:
only: master

sudo: true

python: 3.7

before_install:
- export AUTHOR=`git --no-pager show -s --format='%an <%ae>'`
- export DATE=`TZ=America/New_York date "+%m-%d-%y-%H%M"`
- export DATE_TM=`TZ=America/New_York date "+%m-%d-%y %H:%M"`
- python --version

env:
global:
- AWS_DEFAULT_REGION=us-east-1
- PYTHONPATH=$TRAVIS_BUILD_DIR:$PYTHONPATH

install:
- pip install awscli --upgrade
- pip install -r requirements-travis.txt
- pip install awscli --upgrade

script:
# run tests
#- py.test -vv -r sxX
- export PYTHONPATH=$PWD/src:$PWD/tests:$PYTHONPATH
- BOTO_CONFIG=none pytest --cov-report xml --cov=src/projectname/ tests/unittests/
- coverage xml && python-codacy-coverage -r coverage.xml
- ls -alh

- make
# consult cloud engineer for set up below
#- mv ./dist/lambda.zip ./dist/[lamdba-name]:latest.zip
#- cp ./dist/[lamdba-name]:latest.zip ./dist/[lambda-name]:$DATE.zip
#- cp ./dist/[lamdba-name]:latest.zip ./dist/[lambda-name]:latest.zip
- ls -alh ./dist/


after_success:

# consult a cloud engineer for set up below
#- aws s3 cp ../dist/[lamdba-name]:$DATE.zip s3://biad-lambda-hub/[ProjectName]/ --acl public-read
# run integration test if exists
#- pytest -vv ./tests/integrationtests/ || exit $?

# consult a cloud engineer for heartbeat test

notifications:
# consult a cloud engineer for slack notification
#slack: clinicalbiomed:EphqNgtuf0NzIGaOE43vSFDc
22 changes: 22 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
default: clean install copy zip

install: build_path
pip install -r requirements-lambda.txt -t ./build

build_path:
mkdir ./build

build_dist:
mkdir ./dist

copy:
ls -alh
cp -R src/* ./build/
ls -alh ./build

zip: build_dist
cd ./build && zip -r9 ./dist/lambda.zip . 2>&1 >>/dev/null

clean:
rm -rf ./build
rm -rf ./dist
81 changes: 32 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
# lambda_localstack_template
Template for lambda with localstack and sam integration for localtest
# nci-aws-lambda-localstack-template

This provides a templates for aws lambda test in local environment using localstack or local server instances (i.e. dynamoDB). This examples shows only fraction of the services localstack provides. The full list of services are [here](https://github.com/localstack/localstack). This template provides the basic settings for local test which can be used in different projects with minimal configuration changes. Details for the configuration changes is explained `How to use this template` section below.

## Prerequesite (on Mac)

* [Python3](https://www.python.org/downloads/)

* [AWS client](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html) (pip3 install awscli --upgrade --user)
* [Docker](https://docs.docker.com/docker-for-mac/install/) - need for the local lambda function testing with sam

* [AWS SAM client](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install-mac.html#serverless-sam-cli-install-mac-pip) (pip3 install --user aws-sam-cli)

* Reference: [Adjust path to use sam cli command](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install-mac-path.html)
* [AWS client](https://docs.aws.amazon.com/cli/latest/userguide/cli-chap-install.html)

* [Docker](https://docs.docker.com/docker-for-mac/install/) - need for the local lambda function testing
* [AWS SAM client](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install-mac.html)


## getting the source code

`git clone https://github.com/uujo/lambda_localstack_template.git`
`git clone https://github.com/BIAD/nci-aws-lambda-localstack-template.git`

`cd lambda_localstack_template`
`cd nci-aws-lambda-localstack-template`


## Set up git hooks (pre-commit, pre-push) - This is one time setting. Doesn't have to be repeated every time.
Expand All @@ -39,67 +39,50 @@ For special cases, if you still need to commit, push the code use _--no-verify_
> git push origin [branch_name] --no-verify
```

## Local test set up using aws sam and localstack (with python3 venv)
## set PYTHONPATH for unittest

`export PYTHONPATH=$PWD/src:$PWD/tests:$PYTHONPATH`

If virtual environment is used below, you can set the above path in __.venv/bin/activate__ file


## Local test set up using aws sam and localstack (with python3 venv) - virtual environment setting is optional.

* set the virtual environment (optional, you can use other virtual env tools or without it)

`cd lambda_localstack_template`
`cd nci-aws-lambda-localstack-template`

`python3 -m venv .venv`

`source .venv/bin/activate` - activate virtual environment

`pip install -r requirements.txt` - one time setting
(If install fails with __xcun__ error, run __xcode-select --install__)


* To start local test setup

* Now it is ready to set up the test below.

* After all the test is done. get out of virtual environment.

`./start_local_test_template.sh`
`deactivate`

_-i_ option sets interactive mode on localstack instead of running it background, if you want to monitor localstack log use this option. Without this option, it automatically create the sample data in S3 bucket.

## Tests Examples

* To check the table, sqs and bucket are created (**Use this setting only if you use _-i_ option, without _-i_, these steps runs automatically**)

`aws dynamodb list-tables --endpoint-url http://localhost:4569`

`aws sqs list-queues --endpoint-url=http://localhost:4576`

`aws s3api list-buckets --endpoint-url=http://localhost:4572`

* put data in S3

`aws s3api put-object --endpoint-url=http://localhost:4572 --bucket TEST_BUCKET --key test_path/data.json --body tests/s3_test_data.json`
### [Lambda with dynamo, API gateway testing (Localstack)](https://github.com/BIAD/nci-aws-lambda-localstack-template/wiki/Lambda-Dynamo-Test)

* To check S3 object is created

### [Lambda with dynamo, s3, sqs testing (Localstack)](https://github.com/BIAD/nci-aws-lambda-localstack-template/wiki/Lambda-S3-SQS-Dynamo-Test)

`aws s3api list-objects --bucket=TEST_BUCKET --endpoint-url=http://localhost:4572`


* To test lambdas
### [Lambda with dynamo, API gateway testing (local dynamo server)](https://github.com/BIAD/nci-aws-lambda-localstack-template/wiki/Lambda-Local-Dynamo-Server-Test)

* invoke SQS event

`sam local invoke TestLambda -t build/template.yaml --docker-network lambda_localstack_template_local_aws_network -e tests/sqs_test_event.json`

* To check whether table has an entry inserted.
## Integration Test: To be added

`aws dynamodb scan --table-name TEST_TABLE --endpoint-url http://localhost:4569`

* To check sqs messge is queued

`aws sqs receive-message --queue-url http://localhost:4576/queue/TEST_QUEUE --endpoint-url=http://localhost:4576 --max-number-of-messages=10`


* To stop the local test and clean up docker

`./stop_local_test_template.sh`

## [How to use this template](https://github.com/BIAD/nci-aws-lambda-localstack-template/wiki/How-to-use-this-template)

## Integration Test: To be added
* goal: With minimal configuation change, testing environment can be set up for different project.
* There are two main configuration - One for localstack and one for sam. If starting and stop scrip need to be changed as well if you want to reuse the script.


## Reference
* [AWS SAM guick start guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-quick-start.html)
* [SAM template](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md)
Expand Down
63 changes: 63 additions & 0 deletions local-test-configs/apigw-dynamo-config/api-gw-template-sam.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31

Globals:
Function:
Runtime: python3.7
Timeout: 60
Environment:
Variables:
# Set localtest env
# Set environment variable so amazon service can be switched in local settings
# This has to be matched with localstack config in docker-compose.yaml
# Current example contains DynamoDB
TEST_ENV: LOCAL

Resources:
PostTest:
Type: AWS::Serverless::Function # More info about Function Resource: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#awsserverlessfunction
Properties:
CodeUri: .
Handler: projectname.handler.dynamo_handler.post_handler.post
Events:
Validation:
Type: Api # More info about API Event Source: https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md#api
Properties:
Path: /api/v1/testapi
Method: post

GetTest:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: projectname.handler.dynamo_handler.get_handler.get
Events:
Annotation:
Type: Api
Properties:
Path: /api/v1/testapi/{id}
Method: get

PutTest:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: projectname.handler.dynamo_handler.put_handler.put
Events:
Annotation:
Type: Api
Properties:
Path: /api/v1/testapi/{id}
Method: put

DeleteTest:
Type: AWS::Serverless::Function
Properties:
CodeUri: .
Handler: projectname.handler.dynamo_handler.delete_handler.delete
Events:
Annotation:
Type: Api
Properties:
Path: /api/v1/testapi/{id}
Method: delete
51 changes: 51 additions & 0 deletions local-test-configs/apigw-dynamo-config/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
version: "3"

services:
localstack:
image: localstack/localstack:0.11.1
ports:
- "4566-4599:4566-4599"
- "${PORT_WEB_UI-8080}:${PORT_WEB_UI-8080}"
environment:
- DOCKER_HOST=unix:///var/run/docker.sock
- SERVICES=dynamodb
- DEFAULT_REGION=us-east-1
- EDGE_PORT=4566
volumes:
- /var/run/docker.sock:/var/run/docker.sock
- /private${TMPDIR}:/tmp/localstack
networks:
- local_aws_network

aws-cli:
image: mesosphere/aws-cli
volumes:
- ./dev_env:/project/dev_env
environment:
- AWS_ACCESS_KEY_ID=1234
- AWS_SECRET_ACCESS_KEY=1234
- AWS_DEFAULT_REGION=us-east-1
entrypoint: /bin/sh -c
command: >
"
# Sleep is needed so all localstack components will startup correctly (There's a better way to do this)
sleep 10;

# Initialize dynamodb in localstack
aws dynamodb create-table --endpoint-url=http://localstack:4566 \
--table-name TEST_TABLE \
--attribute-definitions \
AttributeName=id,AttributeType=S \
--key-schema \
AttributeName=id,KeyType=HASH \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5;

# you can run more command here
"
networks:
- local_aws_network
depends_on:
- localstack

networks:
local_aws_network:
Loading