1

I am trying to create a variable that I can retrieve outside of the function counts every time a chalice webhook alert is triggered. I cannot figure out how to get the variable out of the function. Here is my code. Thank you!

from chalice import Chalice

app = Chalice(app_name='tv_alert')
counter = None

@app.route('/')
def index():
    return {'hello': 'world'}

@app.route('/tv', methods=['POST'])
def tv():
    request = app.current_request
    webhook_message = request.json_body
    
    if webhook_message != 0:
        counter += 1

return {
    'message': 'I received the webhook notification!',
    'webhook_message': webhook_message
        }

print(counter)
  • Could you fix the indentation in the `if webhook_message != 0:` line and after it? – Sergey Shubin Apr 06 '21 at 13:14
  • Thank you Sergey. That was a paste error into stack overflow I have now corrected. The code now accurately reflects my script. I am still unclear how to get the value of the "counter" variable outside of the function. – registerrug Apr 06 '21 at 15:29
  • Sounds like this is just a generic question about how to access variables in global scope in Python. An SO search should show you the answer to that. There is nothing specific to Chalice.You will need to keep in mind that any AWS Lambda function state (globals) will only persist for that specific function instance, and only while AWS Lambda reuses that function invocation. – dmulter Apr 28 '21 at 22:06

0 Answers0