Skip to content

Commit

Permalink
Merge pull request #2 from cm-wada-yusuke/iot-messaging=pattern-sample
Browse files Browse the repository at this point in the history
Iot messaging=pattern sample
  • Loading branch information
cm-wada-yusuke authored Jun 15, 2018
2 parents b2f6084 + 789d9bb commit c4bd9b8
Show file tree
Hide file tree
Showing 11 changed files with 98 additions and 47 deletions.
48 changes: 14 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,50 +25,30 @@ Unit Test
Using pytest.

```bash
python -m pytest
make test-unit
```

Integration Test
Deploy
---

Using:

* [AWS SAM Local] (https://github.com/awslabs/aws-sam-local)
* [LocalStack (Docker)] (https://hub.docker.com/r/localstack/localstack/)
* [Bats - Bash Automated Testing System] (https://github.com/sstephenson/bats)

```Bash
# Startup LocalStack
docker-compose up-d

# Execute SAM Local
sam local invoke \
--docker-network a27c0476cb8e \
-t template_heroes.yaml \
--event test / functions / heroes / examples / get_payload.json \
- env ​​- vars environments / sam - local.json GetHeroes
```

The test by Bats executes the above contents and compares the output.
Using [AWS Serverless Application Model \(AWS SAM\)](https://github.com/awslabs/serverless-application-model).

```Bash
bats test / functions / heroes / integration.bats
```
Your task:

Deploy
---
1. Edit `Makevars` for your environment.
1. Create AWS SAM template in `templates/` directory.
1. Create Lambda handler in `src/functions/handlers/` directory.

Task:
The name of the following `${resource_name}` has to be matched:

1. Zip sources and libraries.
2. Upload to S3.
3. Create Cloud Formation stack.
4. CloudFormation Deploy.
* `templates/template_${resource_name}.yaml`
* `src/functions/handlers/${resource_name}/example_handler.py`
* Make task: `make deploy-${resource_name}`

Before deploy, you should set your AccessKey for your AWS account.
Before deploy, you should set your AccessKey and Secret for your AWS account. Deploy example:

```bash
./deploy.sh
make deploy-heroes env=test account_id=9999999999999
```

CI / CD on AWS CodeBuild
Expand All @@ -77,7 +57,7 @@ CI / CD on AWS CodeBuild
### Create S3 bucket for deoloy.

```Bash
aws s3 mb s3: // hero-lambda-deploy --profile your-profile
aws s3 mb s3://hero-lambda-deploy --profile your-profile
```

### Create CodeBuild project and build.
Expand Down
File renamed without changes.
40 changes: 40 additions & 0 deletions src/functions/handlers/dispatchers/iot_heroes_dispatcher.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import os
import boto3
import base64
import json

import logging
logger = logging.getLogger()
logger.setLevel(logging.INFO)


PUT_HEROES_FUNCTION_NAME = os.getenv('PUT_HEROES_FUNCTION_NAME')
PUT_OFFICES_FUNCTION_NAME = os.getenv('PUT_OFFICES_FUNCTION_NAME')
PUT_SPONSORS_FUNCTION_NAME = os.getenv('PUT_SPONSORS_FUNCTION_NAME')

LAMBDA = boto3.client('lambda')


def lambda_handler(event, context):
for record in event['Records']:
binary_message = base64.b64decode(record["kinesis"]["data"])
json_message = json.loads(binary_message.decode('utf-8'))
topic_name = json_message['topic']

if 'heroes/activity/heroes' in topic_name:
function_name = PUT_HEROES_FUNCTION_NAME
elif 'heroes/activity/offices' in topic_name:
function_name = PUT_OFFICES_FUNCTION_NAME
elif 'heroes/activity/sponsors' in topic_name:
function_name = PUT_SPONSORS_FUNCTION_NAME
else:
raise Exception(f'UnknownTopicName:{topic_name}')

LAMBDA.invoke(
FunctionName=function_name,
InvocationType='Event',
Payload=binary_message
)
message = f'Finished: dispatch message to function_name:{function_name}. '
logger.info(message)
return message
11 changes: 9 additions & 2 deletions src/functions/handlers/heroes/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
import uuid
from builtins import Exception
import os
from src.functions.handlers.heroes.utils import *
import time
from core.utils import *

DYNAMODB_ENDPOINT = os.getenv('DYNAMODB_ENDPOINT')
HERO_TABLE_NAME = os.getenv('HERO_TABLE_NAME')
Expand Down Expand Up @@ -38,23 +39,29 @@ def get(event, context):
except Exception as error:
raise error


def put(event, context):
try:
hero_id = str(uuid.uuid4())

name = event.get('name')
office = event.get('office')
sponsor = event.get('sponsor')
activity = event.get('activity')
updated_at = epoc_by_second_precision(datetime.now())

response = DYNAMODB_TABLE.put_item(
Item={
'id': hero_id,
'name': name,
'office': office,
'sponsor': sponsor,
'activity': activity,
'updated_at': updated_at,
'created_at': updated_at,
}
)
time.sleep(1)
return response
except Exception as error:
raise error
raise error
6 changes: 3 additions & 3 deletions src/functions/handlers/offices/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
from builtins import Exception
import os
from src.functions.handlers.heroes.utils import *
from core.utils import *

ENV = os.getenv('ENV')
DYNAMODB_ENDPOINT = os.getenv('DYNAMODB_ENDPOINT')
Expand All @@ -22,7 +22,6 @@
DYNAMODB_TABLE = DYNAMO.Table(OFFICE_TABLE_NAME)



def get(event, context):
try:
office_id = event['id']
Expand All @@ -40,6 +39,7 @@ def get(event, context):
except Exception as error:
raise error


def put(event, context):
try:
office_id = str(uuid.uuid4())
Expand All @@ -59,4 +59,4 @@ def put(event, context):
)
return response
except Exception as error:
raise error
raise error
2 changes: 1 addition & 1 deletion src/functions/handlers/sponsors/index.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import uuid
from builtins import Exception
import os
from src.functions.handlers.heroes.utils import *
from core.utils import *

ENV = os.getenv('ENV')
DYNAMODB_ENDPOINT = os.getenv('DYNAMODB_ENDPOINT')
Expand Down
24 changes: 24 additions & 0 deletions templates/template_dispatchers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: Application logger to CloudWatch Logs.
Resources:
IotHeroesDispathcer:
Type: AWS::Serverless::Function
Properties:
FunctionName:
Fn::Sub: ${Env}-iot_heroes_dispatcher
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: handlers/dispatchers/iot_heroes_dispatcher.lambda_handler
Runtime: python3.6
CodeUri: src/functions
Environment:
Variables:
ENV: !Ref Env
PUT_HEROES_FUNCTION_NAME:
Fn::Sub: ${Env}-${PutHeroesFunctionName}
PUT_OFFICES_FUNCTION_NAME:
Fn::Sub: ${Env}-${PutOfficesFunctionName}
PUT_SPONSORS_FUNCTION_NAME:
Fn::Sub: ${Env}-${PutSponsorsFunctionName}

4 changes: 2 additions & 2 deletions templates/template_heroes.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Resources:
Fn::Sub: ${Env}-${GetHeroesFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/handlers/heroes/index.get
Handler: handlers/heroes/index.get
Runtime: python3.6
CodeUri: src/functions
Environment:
Expand All @@ -25,7 +25,7 @@ Resources:
Fn::Sub: ${Env}-${PutHeroesFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/heroes/index.put
Handler: handlers/heroes/index.put
Runtime: python3.6
CodeUri: src/functions
Environment:
Expand Down
4 changes: 2 additions & 2 deletions templates/template_offices.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Resources:
Fn::Sub: ${Env}-${GetOfficesFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/hadlers/offices/index.get
Handler: handlers/offices/index.get
Runtime: python3.6
CodeUri: src/functions
Environment:
Expand All @@ -25,7 +25,7 @@ Resources:
Fn::Sub: ${Env}-${PutOfficesFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/offices/index.put
Handler: handlers/offices/index.put
Runtime: python3.6
CodeUri: src/functions
Environment:
Expand Down
4 changes: 2 additions & 2 deletions templates/template_sponsors.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Resources:
Fn::Sub: ${Env}-${GetSponsorsFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/hadlers/sponsors/index.get
Handler: handlers/sponsors/index.get
Runtime: python3.6
CodeUri: src/functions
Environment:
Expand All @@ -25,7 +25,7 @@ Resources:
Fn::Sub: ${Env}-${PutSponsorsFunctionName}
Role:
Fn::Sub: arn:aws:iam::${AccountId}:role/${Env}-heroes-lambda
Handler: src/functions/sponsors/index.put
Handler: handlers/sponsors/index.put
Runtime: python3.6
CodeUri: src/functions
Environment:
Expand Down
2 changes: 1 addition & 1 deletion test/functions/handlers/heroes/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
"""This is python3.6 program."""

from src.functions.handlers.heroes.utils import *
from core.utils import *


def test_epoc_by_second_precision():
Expand Down

0 comments on commit c4bd9b8

Please sign in to comment.