-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from cm-wada-yusuke/iot-messaging=pattern-sample
Iot messaging=pattern sample
- Loading branch information
Showing
11 changed files
with
98 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
40 changes: 40 additions & 0 deletions
40
src/functions/handlers/dispatchers/iot_heroes_dispatcher.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters