从Glue作业发送电子邮件

0

【以下的问题经过翻译处理】 我有一个AWS Glue作业,可以从S3中读取数据并将其注入RDS MySQL中。 我正在尝试在作业结束时(从pySpark Glue作业里)使用以下链接中的示例发送通知电子邮件https://docs.aws.amazon.com/ses/latest/dg/send-an-email-using-sdk-programmatically.html

我还向附加到我的Glue作业的角色添加了以下权限:

 "ses:SendEmail",  
 "ses:SendRawEmail"

运行我的作业需要很长时间,并且没有任何输出(无失败,无成功,其仍在运行并且没有任何结果),所以我需要每次停止它。

以下是我使用的示例代码来从Glue脚本发送电子邮件:

import boto3
from botocore.exceptions import ClientError

# 将sender@example.com替换为您的“发件人”地址。该地址必须通过Amazon SES进行验证。
SENDER = "test@test.com"

# 将recipient@example.com替换为“收件人”地址。如果您的帐户仍处于沙箱状态,则必须验证此地址。
RECIPIENT = "test.test@test.com"

# 如有必要,请将us-west-2替换为您用于Amazon SES的AWS区域。
AWS_REGION = "us-east-1"

# 电子邮件的主题行。
SUBJECT = "Amazon SES Test (SDK for Python)"

# 针对不支持HTML电子邮件客户端的收件人的电子邮件正文(纯文本)。
BODY_TEXT = ("Amazon SES Test (Python)\r\n"
             "This email was sent with Amazon SES using the "
             "AWS SDK for Python (Boto)."
            )

# 电子邮件的HTML正文。
BODY_HTML = """<html>
<head></head>
<body>
  <h1>Amazon SES Test (SDK for Python)</h1>
  <p>This email was sent with
    <a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
    <a href='https://aws.amazon.com/sdk-for-python/'>
      AWS SDK for Python (Boto)</a>.</p>
</body>
</html>
 """   

# 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': [
                RECIPIENT,
            ],
        },
        Message={
            'Body': {
                'Html': {
                    'Charset': CHARSET,
                    'Data': BODY_HTML,
                },
                'Text': {
                    'Charset': CHARSET,
                    'Data': BODY_TEXT,
                },
            },
            'Subject': {
                'Charset': CHARSET,
                'Data': SUBJECT,
            },
        },
        Source=SENDER,
    )
# Display an error if something goes wrong.	
except ClientError as e:
    print(e.response['Error']['Message'])
else:
    print("Email sent! Message ID:"),
    print(response['MessageId'])

哪里有配置错误使得glue作业卡住的地方吗?

1 Antwort
0

【以下的回答经过翻译处理】 你好,根据你的故障排除,问题在于Glue需要连接到默认通过互联网访问的SES终端点。 为避免打开出站规则或查找SES IP范围以限制规则的需要,您可以考虑创建一个SES VPC端点

拆分工作的建议仍然有效,如果存储过程失败,则可能希望使用错误代码退出作业,然后在后续作业中发送通知,这种情况下,您不需要为SES设置出站规则或VPC端点,因为第二个(python shell)作业不与VPC关联,可以直接访问SES。

希望这可以帮助您。

profile picture
EXPERTE
beantwortet vor 5 Monaten

Du bist nicht angemeldet. Anmelden um eine Antwort zu veröffentlichen.

Eine gute Antwort beantwortet die Frage klar, gibt konstruktives Feedback und fördert die berufliche Weiterentwicklung des Fragenstellers.

Richtlinien für die Beantwortung von Fragen