使用 API 网关和 lambda 的 AWS 代码管道阶段批准不起作用

时间:2021-03-02 19:36:01

标签: amazon-web-services aws-lambda aws-api-gateway amazon-sns aws-codepipeline

我们有一个用于 Angular 应用程序构建和部署的 AWS 代码管道。

我添加了一个批准阶段,用户需要使用邮件中提供的链接进行批准。

以下是管道工作流程

我收到的邮件包含来自 AWS 的赛门铁克链接,https://clicktime.symantec.com/34RBC48WLEQRUG7Vc?u=https%3A%2F%xxxxx.execute-api.us-east-1.amazonaws.com%2Fv0%2Fpipeline-approval%3Faction%3DApproved%26pipeline%3Dpiplinexxx%3Drelease-approval%26pipelineexecutionid%3xxxxxx

  • 下面是lambda函数代码
import json
import logging
import re
import time
import boto3

logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
MAX_WAIT_FOR_RESPONSE = 10
WAIT_INCREMENT = 1

def handler(event, context):
  logger.info('REQUEST RECEIVED:\n %s', event)
  logger.info('REQUEST RECEIVED:\n %s', context)
  pipeline = event["queryStringParameters"]['pipeline']
  stage = event["queryStringParameters"]['stage']
  action = event["queryStringParameters"]['action']
  approval_action = 'transition'
  pipelineexecutionid = event["queryStringParameters"]['pipelineexecutionid']
  client = boto3.client('codepipeline')
  r = client.get_pipeline_state(name=pipeline)['stageStates']
  print(r)
  s = next((x for x in r if x['stageName'] == stage and x['latestExecution']['pipelineExecutionId'] == pipelineexecutionid ), None)
  print(s)
  s1 = s['actionStates']
  print(s1)
  s2 = next((y for y in s1 if y['actionName'] == approval_action ), None)
  print(s2)
  t = s2['latestExecution']['token']
  print(t)
  client.put_approval_result(
      pipelineName=pipeline,
      stageName=stage,
      actionName=approval_action,
      result={
          'summary': 'Automatically approved by Lambda.',
          'status': action
      },
      token=t
  )
  logger.info("Status message: %s", client.put_approval_result)
  if action == 'Approved':
    return {"statusCode": 200, "body": json.dumps('Thank you for approving the release!!')}
  elif action == 'Rejected':
    return {"statusCode": 200, "body": json.dumps('rejected.')}

问题在于,在到达批准阶段后,它会发送邮件,但不会等待用户输入,而是在 2 到 5 秒内自动获得批准或拒绝。

请帮助我这里出了什么问题,为什么 Lambda 不等待 API 的响应以及为什么它会自动批准或拒绝。

0 个答案:

没有答案