Skip to content
Advertisement

Is there any way to get the output in aws sns to print separate lines for every item in the list

Ok, so I have this lambda function that appends return_values[whatever] list and that is sent to aws sns with the formatting below, the problem is it separates the list item with commas and not with a desired new line. Is there a method to get this formatting correct? I’ve tried the print(*results,sep=’n’) but that just comes back as None as print only returns to the console.

Here’s the output:

Here is a list of EC2 tags that need review.
Please correct the tags using the approved values list located below
<insert site here>.

------------------------------------------------------------------------------------
Summary of incorrect tags keys and/or values:
------------------------------------------------------------------------------------
Incorrect Tag Keys    :   []
Incorrect Tag Values  :   ['i-abc has a incorrect builder tag value.']

------------------------------------------------------------------------------------
Summary of missing tags keys and/or values:
------------------------------------------------------------------------------------
Missing Tag Keys     :   []
Missing Tag Values   :   []

------------------------------------------------------------------------------------
Summary of unknown values:
------------------------------------------------------------------------------------
Unknown Tag Values   :   ["i-abc has unknown for it's builder tag value.", "i-def has unknown for it's builder tag value.", "i-ghi has unknown for it's builder tag value."]

------------------------------------------------------------------------------------

Unknown us listed on the same line three times and the goal is to separate the results per line. The general idea of the code is below, thanks for the assistance.

def lambda_handler(event, context): 
    reservations = ec.describe_instances().get('Reservations', [])
    return_value = {} #creates empty dictionary#
    return_value['missingtagkeys'] = [] #within return values dictionary, create a missing tag key list#
    return_value['missingtagvalues'] = [] #within return values dictionary, creates a missing tag values key list#
    return_value['incorrecttagkeys'] = [] #within return values dictionary, create a incorrect tag key list#
    return_value['incorrecttagvalues'] = [] #within return values dictionary, create a incorrect tag value list#
    return_value['unknowntagvalues'] = [] #within return values dictionary, create a unknown tag value list# 



    incorrect_keys = return_value['incorrecttagkeys']
    incorrect_values = return_value['incorrecttagvalues']
    missing_keys = return_value['missingtagkeys']
    missing_values = return_value['missingtagvalues']
    unknowng_value = return_value['unknowntagvalues']
    
    email_body = """

    Here is a list of EC2 tags that need review.  
    Please correct the tags using the approved values list located below  
    <insert site here>.
    
    ------------------------------------------------------------------------------------
    Summary of incorrect tags keys and/or values:
    ------------------------------------------------------------------------------------
    Incorrect Tag Keys    :   {incorrectkeys}
    Incorrect Tag Values  :   {incorrectvalues}

    ------------------------------------------------------------------------------------
    Summary of missing tags keys and/or values:
    ------------------------------------------------------------------------------------
    Missing Tag Keys     :   {missingkeys}
    Missing Tag Values   :   {missingvalues}

    ------------------------------------------------------------------------------------
    Summary of unknown values:
    ------------------------------------------------------------------------------------
    Unknown Tag Values   :   {unknowngvalues}

    ------------------------------------------------------------------------------------

        """.format(incorrectkeys = incorrect_keys, 
                   incorrectvalues = incorrect_values, 
                   missingkeys = missing_keys, 
                   missingvalues = missing_values, 
                   unknownvalues = unknown_value)  
            
           
    sns_client.publish(
        TopicArn = 'arn:aws:sns',
        Subject = 'Incorrect Tags Detected',
        Message = str(email_body) + str(return_value),
        )
    return return_value

Advertisement

Answer

This last example is how I got it to work.

list_abc = ['aaa', 'bbb', 'ccc']

string = ''.join(list_abc)
print(string)
>>> aaabbbccc

string = ','.join(list_abc)
print(string)
>>> aaa,bbb,ccc

string = '-'.join(list_abc)
print(string)
>>> aaa-bbb-ccc

string = 'n'.join(list_abc)
print(string)
>>> aaa
>>> bbb
>>> ccc
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement