簡體   English   中英

蟒蛇 | Boto3 - AWS SNS 電子郵件格式問題

[英]Python | Boto3 - AWS SNS Email formatting issue

我想將我的輸出格式化為 SNS 電子郵件正文,但 Elasticbeanstalk 的動態特性對我構成了可怕的挑戰。

代碼:

def lambda_handler(event, context):
    #Some code to fetch Elasticbeanstalk environement details
    _ _ _ 
    _ _ _ 

    msg = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'\
    '\n\nHello Team, \n\n Elastic Beanstalk status: \n\n'\
    'Total no.of Elastic beanstalk Environments: '+str(len(envname))+'\n'\
    'EnvironmentName    |  Health   | HealthStatus      | CNAME           \n\n'\
    +str(prin_envname[0])+' | '+str(prin_Ehealth[0])+'       | '+str(prin_Ehealthstatus[0])+'      | '+str(prin_Cname[0])+' \n'\
    +str(prin_envname[1])+' | '+str(prin_Ehealth[1])+'       | '+str(prin_Ehealthstatus[1])+'      | '+str(prin_Cname[1])+' \n'\
    +str(prin_envname[2])+' | '+str(prin_Ehealth[2])+'       | '+str(prin_Ehealthstatus[2])+'      | '+str(prin_Cname[2])+' \n'\
    +str(prin_envname[3])+' | '+str(prin_Ehealth[3])+'       | '+str(prin_Ehealthstatus[3])+'      | '+str(prin_Cname[3])+' \n'\
    '\n'\
    'Total number of EC2 instances associated: '+str(len(envinst))+'\n'\
    '\n'\
    '\n'\
    '\n'\
    '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
    send_sns(msg)

def send_sns(msg):
    snsClient = boto3.client('sns',region)
    if msg:
        subject_str = 'Elastic Beanstalkstatus - Environment status' 
        response = snsClient.publish(TopicArn=os.environ['SNS_ARN'],Message=msg,Subject=subject_str)
        print('Publishing Messsage to SNS Topic')
    else:
        print('No content, exiting without sending SNS email..')


挑戰

由於我在 aws 帳戶中的 Elastic Beanstalk 環境不斷從 1 變為 N,因此我無法預測 Elastic Beanstalk 中的環境數量。

如果您在 SNS 的變量 msg 中看到我的打印 stmts,它是針對 4 種環境進行硬編碼的。

請建議我如何在 SNS 電子郵件正文中動態打印環境。

警告

我無法訪問 Pandas/Numpy 等第三方庫,我可以使用 for 循環按我希望的順序打印,但問題是 SNS 只接受電子郵件正文的 msg 變量。

非常感謝任何幫助/提示/參考。

經過數小時的反復試驗,找到了解決我的問題的有效方法。

注意:盡管我對我的解決方案很滿意,但始終歡迎任何更好的解決方案:)

這是我修改后的代碼:

代碼:

def lambda_handler(event, context):
    #Some code to fetch Elasticbeanstalk environement details
    _ _ _ 
    _ _ _ 

    # Transposing the list values into rows to columns & columns to rows with below code
    # and creating 2 parameters for designer function
    final_list = []
    pre_final_list = []
    pre_final_list = [envname] + [Ehealth] + [Ehealthstatus] + [Cname]
    final_list = [list(i) for i in zip(*pre_final_list)]

    # Calling designer function with 2 arguments envlen and final_list
    designer_op = designer(envlen,final_list)

    msg = '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'\
    '\n\nHello Team, \n\n Elastic Beanstalk status: \n'\
    'Total no.of Elastic beanstalk Environments: '+str(len(envname))+'\n'\
    'EnvironmentName    |  Health   | HealthStatus      | CNAME           \n'\
    '\n'\
    ''+str(designer_op)+\  # assigning designer function's output
    '\n'\
    'Total number of EC2 instances currently associated: ' +str(len(envinst))+ '\n'\
    '\n'\
    '^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^'
    send_sns(msg)

def send_sns(msg):
    snsClient = boto3.client('sns',region)
    if msg:
        subject_str = 'Elastic Beanstalkstatus - Environment status' 
        response = snsClient.publish(TopicArn=os.environ['SNS_ARN'],Message=msg,Subject=subject_str)
        print('Publishing Messsage to SNS Topic')
    else:
        print('No content, exiting without sending SNS email..')

# Designer function to return rows equal to the number of environments
def designer(envlen, final_list):

    if envlen == 10:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'+str(final_list[3])+'\n'\
        ''+str(final_list[4])+'\n'+str(final_list[5])+'\n'+str(final_list[6])+'\n'+str(final_list[7])+'\n'\
        ''+str(final_list[8])+'\n'+str(final_list[9])+'\n'
        return str(op)

    elif envlen == 9:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'+str(final_list[3])+'\n'\
        ''+str(final_list[4])+'\n'+str(final_list[5])+'\n'+str(final_list[6])+'\n'+str(final_list[7])+'\n'\
        ''+str(final_list[8])+'\n'
        return str(op)

    elif envlen == 8:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'+str(final_list[3])+'\n'\
        ''+str(final_list[4])+'\n'+str(final_list[5])+'\n'+str(final_list[6])+'\n'+str(final_list[7])+'\n'
        return str(op)

    elif envlen == 7:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'+str(final_list[3])+'\n'\
        ''+str(final_list[4])+'\n'+str(final_list[5])+'\n'+str(final_list[6])+'\n'
        return str(op)

    elif envlen == 6:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'+str(final_list[3])+'\n'\
        ''+str(final_list[4])+'\n'+str(final_list[5])+'\n'
        print('op type ',type(op))
        return str(op)

    elif envlen == 5:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'+str(final_list[3])+'\n'\
        ''+str(final_list[4])+'\n'
        return op

    elif envlen == 4:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'+str(final_list[3])+'\n'
        return op

    elif envlen == 3:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'+str(final_list[2])+'\n'
        return op

    elif envlen == 2:
        op = ''+str(final_list[0])+'\n'+str(final_list[1])+'\n'
        return op

    elif envlen == 1:
        op = ''+str(final_list[0])+'\n'
        return op


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM