【发布时间】:2019-03-28 19:45:06
【问题描述】:
我有一个简单的 AWS CodePipeline,其标准“源”->“构建”->“部署”管道阶段运行良好,我正在尝试添加我自己的自定义最终管道阶段,它是单个 AWS Lambda 函数。问题是我的最后一个自定义 Lambda 函数多次运行,经过很长时间后,出现以下消息的错误:
整个管道请看附件截图:
当管道到达最后一步时,它会以“Blue (In-Progress)”状态旋转很长时间,然后显示错误,如下所示:
这是我的 Lambda 函数代码:
from __future__ import print_function
import hashlib
import time
import os
import boto3
import json
from botocore.exceptions import ClientError
def lambda_handler(event, context):
# Test
AWS_ACCESS_KEY = ASDF1234
AWS_SECRET_KEY = ASDF1234
SQS_TESTING_OUTPUT_STATUS_QUEUE_NAME = 'TestingOutputQueue'
# Get the code pipeline
code_pipeline = boto3.client('codepipeline')
# Get the job_id
for key, value in event.items():
print(key,value)
job_id = event['CodePipeline.job']['id']
DATA = json.dumps(event)
# Create a connection the SQS Notification service
sqs_resource_connection = boto3.resource(
'sqs',
aws_access_key_id = AWS_ACCESS_KEY,
aws_secret_access_key = AWS_SECRET_KEY,
region_name = 'us-west-2'
)
# Get the queue handle
print("Waiting for notification from AWS ...")
queue = sqs_resource_connection.get_queue_by_name(QueueName = SQS_TESTING_OUTPUT_STATUS_QUEUE_NAME)
messageContent = ""
cnt = 1
# Replace sender@example.com with your "From" address.
# This address must be verified with Amazon SES.
SENDER = ME
# Replace recipient@example.com with a "To" address. If your account
# is still in the sandbox, this address must be verified.
RECIPIENTS = [YOU]
# If necessary, replace us-west-2 with the AWS Region you're using for Amazon SES.
AWS_REGION = "us-east-1"
# The subject line for the email.
SUBJECT = "Test Case Results"
# The email body for recipients with non-HTML email clients.
BODY_TEXT = ("Test Case Results Were ...")
# The HTML body of the email.
BODY_HTML = """<html>
<head></head>
<body>
<h1>Amazon SES Test (SDK for Python)</h1>
<p>%s</p>
</body>
</html>
"""%(DATA)
# The character encoding for the email.
CHARSET = "UTF-8"
# Create a new SES resource and specify a region.
client = boto3.client('ses', region_name=AWS_REGION)
# Try to send the email.
try:
# Provide the contents of the email.
response = client.send_email(
Destination={
'ToAddresses': RECIPIENTS,
},
Message={
'Body': {
'Html': {
'Charset': CHARSET,
'Data': BODY_HTML,
},
'Text': {
'Charset': CHARSET,
'Data': BODY_TEXT,
},
},
'Subject': {
'Charset': CHARSET,
'Data': SUBJECT,
},
},
Source=SENDER,
# If you are not using a configuration set, comment or delete the
# following line
#ConfigurationSetName=CONFIGURATION_SET,
)
# Display an error if something goes wrong.
except ClientError as e:
code_pipeline.put_third_party_job_failure_result(jobId=job_id, failureDetails={'message': message, 'type': 'JobFailed'})
code_pipeline.put_job_failure_result(jobId=job_id, failureDetails={'message': message, 'type': 'JobFailed'})
print(e.response['Error']['Message'])
else:
code_pipeline.put_third_party_job_success_result(jobId=job_id)
code_pipeline.put_job_success_result(jobId=job_id)
print("Email sent! Message ID:"),
print(response['MessageId'])
print('Function complete.')
return "Complete."
如何让 Lambda 触发一次并返回,以便管道可以正确完成。谢谢!
【问题讨论】:
-
由于某种原因,我第一次阅读您的 Q 时没有看到您的代码调用
put_job_success_result。无论如何,您不需要调用put_third_party_job_success_result。尝试不使用它并添加一些调试打印以检查它是否挂起以及挂起的位置。 -
嗨@jweyrich - 感谢您的回答和评论。我试过没有“third_party_job”调用,但它仍然失败。我一定错过了其他东西,但我还无法弄清楚。我尝试了“put_third_party_job_success_result”、“put_job_success_result”的各种组合,甚至有一个“回报”。还有其他想法吗?谢谢。
-
当它无限期地运行时,它会发送多封电子邮件——所以它的行为就像 lambda 函数被调用了几次一样。这是线索吗?
-
put_job_success_result是否可以引发异常?将其移到try块内进行验证。
标签: amazon-web-services aws-lambda boto3 aws-codepipeline aws-codestar