1

With the simple function I am able to get data stored in response variable. With a print statement I can get the data showing correctly.

ec2_client = boto3.client("ec2")
def instance_list(instance_name):
    response = ec2_client.describe_instances(
        Filters=[
            {
                'Name': 'tag:Name',
                'Values': [ instance_name ]
            }
        ]
    )['Reservations']
    return response
    #print(response)

if __name__ == '__main__':
    my_instance_list = instance_list("example-*")

However while trying to import the value of response from the above function to another function, getting error as NameError: name 'response' is not defined

def my_list():
    list = instance_list(response)
    print(list)

Looks like something unidentified.

7
  • You are using a parameter in a function! Response is never defined! You should pass the name of the instance then it returns a response Commented Aug 7, 2019 at 15:21
  • You didn't call the returned result response, you called it my_instance_list, and you did so in a place that deliberately doesn't get executed when you import the file. Also why would you want to pass that to the function that returns it? Your code doesn't make any sense. Commented Aug 7, 2019 at 15:22
  • @jonrsharpe the basic idea is to use return value of one function to another function Commented Aug 7, 2019 at 15:23
  • Also, is multilining the list filters allowed? Commented Aug 7, 2019 at 15:24
  • And are both functions called instance_list? Commented Aug 7, 2019 at 15:24

2 Answers 2

2

you need to pass the varable to the next function, for example

my_list(instance_list())
Sign up to request clarification or add additional context in comments.

1 Comment

@Rio glad to hear it ;)
0

the basic idea is to use return value of one function to another function

Your function should be this:

def my_list():
    # List is equal to the response value of instance list.
    list = instance_list("example-*")

    print(list)

Easy example

def add_five_to_number(number):
   number += 5
   return number

def print_number(number):
   higher_number = add_five_to_number(number)   
   print(higher_number)

test_number = 3
print_number(test_number)

# Returns 8

3 Comments

its there under if __name__ == '__main__' to pass inputs. However I am trying to fetch output of response from the same function.
What? :p I don't get what you're trying to say. Fetch output of response from the same function?
actually the function instance_list is taking input from main and generating list of dynamic data. Now i am storing those data within response variable. My further goal is to use the data of the variable to another function with any procedure.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.