Home > Enterprise >  Lambda SNS Not Printing Message
Lambda SNS Not Printing Message

Time:06-07

I'm very new to Lambda and AWS but what I'm trying to do is make a function that checks the builder tag for certain conditions and returns a statement of action. The code is successful in the console but and it emails the sns message, however, it is not the output I was expecting, it's just "None". The output would be multiple lines with instances and the logic ran on them

import boto3  

import collections    

import datetime    

import time    

import sys

import json

import logging

from botocore.exceptions import ClientError

sns = boto3.client('sns', 'us-east-1')

ec = boto3.client('ec2', 'us-east-1')    

ec2 = boto3.resource('ec2', 'us-east-1')    

SNS_TOPIC_ARN = 'arn'

AWS_REGION = 'us-east-1'

 

def lambda_handler(event, context):          

    instance_ids = []

    reservations = ec.describe_instances().get('Reservations', [])
    
    buildertag = [{

        "Key": "builder",

        "Value": "unknown"

    }]

   

    for reservation in reservations:

        for instance in reservation['Instances']:

            tags = {}

            for tag in instance['Tags']:

                tags[tag['Key']] = tag['Value']

   

            # Checks if builder tag exist and adds if it doesn't #

            if not 'builder' in tags:

                print(instance['InstanceId']   " does not have builder tag. Adding 'builder' tag with value of 'unknown'.")

                ec.create_tags(Resources=[instance["InstanceId"]], Tags=buildertag)

     

                missingtag = print(instance['InstanceId']   " does not have builder tag. Adding 'builder' tag with value of 'unknown'.")

                               
                sns = boto3.client('sns')

                sns.publish(

                    TopicArn = 'arn',

                    Subject = 'Incorrect Tags Dectected',

                    Message = str(missingtag),
                
                )

            # Checks for unknown tag value and print output #                                        

            elif tags['builder'] in ['Unknown', 'unknown']:

                print(instance['InstanceId']   " has unknown for it's builder tag value.")  

                 # Checks for null tag value and print output #                                        

            elif tags['builder'] in ['']:

                print(instance['InstanceId']   " has no value for builder tag.")  

                ec.create_tags(Resources=[instance["InstanceId"]], Tags=buildertag)

 
            # Checks for builder tags equal to approved values and print output #                                        

            elif tags['builder'] in ['x', 'y', 'z', 'a', 'b']:

                print(instance['InstanceId']   " has an approved builder tag value.")  

                
 

                

            # Checks for builder tags equal to unapproved values and print output #                                        

            elif tags['builder'] not in ['x', 'y', 'z', 'a', 'b']:

                print(instance['InstanceId']   " has a incorrect builder tag value.")

CodePudding user response:

You are not returning anything from your lambda_handler, thus the output is just None. You have to add return statement(s) to your code with actual return values that you want to output from the lambda.

  • Related