Created
June 19, 2019 12:32
-
-
Save tac-yacht/a7ae286c0a81b39ccd67e5ba39633af2 to your computer and use it in GitHub Desktop.
IoT 1-Click to CodePipeline's Approval
This file contains hidden or 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
import json | |
import boto3 | |
import os | |
import logging | |
#https://docs.aws.amazon.com/ja_jp/lambda/latest/dg/python-logging.html | |
logger = logging.getLogger() | |
logger.setLevel(logging.INFO) | |
client = boto3.client('codepipeline') | |
def lambda_handler(event, context): | |
logger.info(event) | |
attr=event['placementInfo']['attributes'] | |
pipelineName=attr['pipelineName'] | |
stageName=attr['stageName'] | |
actionName=attr['actionName'] | |
ansStatus='Approved' if event['deviceEvent']['buttonClicked']['clickType']=='DOUBLE' else 'Rejected' | |
logger.info(ansStatus) | |
pipeline_res = client.get_pipeline_state( | |
name=pipelineName | |
) | |
targetStage = list(filter(lambda n:n['stageName']==stageName, pipeline_res['stageStates'])) | |
if len(targetStage)==0: | |
return { | |
'statusCode': 404, | |
'body': json.dumps('targetStage is not found') | |
} | |
else: | |
t = targetStage[0] | |
targetStage = t | |
logger.info(targetStage) | |
logger.info(targetStage['actionStates']) | |
targetAction = list(filter(lambda n:n['actionName']==actionName, targetStage['actionStates'])) | |
if len(targetAction)==0: | |
return { | |
'statusCode': 404, | |
'body': json.dumps('targetAction is not found') | |
} | |
else: | |
t = targetAction[0] | |
targetAction = t | |
logger.info(targetAction) | |
if targetAction['latestExecution']['status']!='InProgress' : | |
return { | |
'statusCode': 406, | |
'body': json.dumps('targetAction is not InProgress') | |
} | |
token = targetAction['latestExecution']['token'] | |
putApprovalRes = client.put_approval_result( | |
pipelineName=pipelineName, | |
stageName=stageName, | |
actionName=actionName, | |
result={ | |
'summary': 'approval answer by lambda', | |
'status': ansStatus | |
}, | |
token=token | |
) | |
logger.info(putApprovalRes) | |
return { | |
'statusCode': 200 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment