Skip to content
Advertisement

Lambda to call incoming webhooks

I have been using a bash script to call a webhook that trigger azure devops pipeline, now want to use lambda function to do the same thing but am having issue with identation.

   def lambda_handler(event, context):
        import subprocess
        result = subprocess.call("curl -X POST 'https://dev.azure.com/organisation/_apis/public/distributedtask/webhooks/stepfunction?api-version=6.0-preview' -d '{ "tags2": {} }' -H "Content-Type: application/json"", shell=True)
        return result

The error is below and am not sure why is not working. Any idea why?

Function Logs
START RequestId: c750f682-5f80-4ab1-9ef1-7cac25878b11 Version: $LATEST
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 3)

Trying this now and comes up with :

import json
from urllib import request, parse
def lambda_handler(event, context):
    some_url = "https://dev.azure.com/organization/_apis/public/distributedtask/webhooks/stepfunction?api-version=6.0-preview"
    some_data = {"tags":[]}
    result = do_post(some_url,some_data)
    return {"statusCode":200,body:"sent callback"}

But the error comes up :

[ERROR] NameError: name 'do_post' is not defined

Advertisement

Answer

why are you mixing linux curl with python? … just use python

def do_post(url,data):
    from urllib import request, parse
    data_str = json.dumps(data).encode()
    headers = {"Content-Type":"application/json"}
    req =  request.Request(url, headers=headers,data=data_str) # this will make the method "POST"
    resp = request.urlopen(req)
    return resp

resp = do_post('https://dev.azure.com/organisation/_apis/public/distributedtask/webhooks/stepfunction?api-version=6.0-preview',{"tags":[]})

something like that

of coarse in a lambda fn you would need to set it up appropriately…

def lambda_handler(event, context):
    some_url = "https://..."
    some_data = {"key":"value"}
    result = do_post(some_url,some_data)
    return {"statusCode":200,body:"sent callback"}
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement