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.
JavaScript
x
5
1
def lambda_handler(event, context):
2
import subprocess
3
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)
4
return result
5
The error is below and am not sure why is not working. Any idea why?
JavaScript
1
4
1
Function Logs
2
START RequestId: c750f682-5f80-4ab1-9ef1-7cac25878b11 Version: $LATEST
3
[ERROR] Runtime.UserCodeSyntaxError: Syntax error in module 'lambda_function': invalid syntax (lambda_function.py, line 3)
4
Trying this now and comes up with :
JavaScript
1
8
1
import json
2
from urllib import request, parse
3
def lambda_handler(event, context):
4
some_url = "https://dev.azure.com/organization/_apis/public/distributedtask/webhooks/stepfunction?api-version=6.0-preview"
5
some_data = {"tags":[]}
6
result = do_post(some_url,some_data)
7
return {"statusCode":200,body:"sent callback"}
8
But the error comes up :
JavaScript
1
2
1
[ERROR] NameError: name 'do_post' is not defined
2
Advertisement
Answer
why are you mixing linux curl with python? … just use python
JavaScript
1
10
10
1
def do_post(url,data):
2
from urllib import request, parse
3
data_str = json.dumps(data).encode()
4
headers = {"Content-Type":"application/json"}
5
req = request.Request(url, headers=headers,data=data_str) # this will make the method "POST"
6
resp = request.urlopen(req)
7
return resp
8
9
resp = do_post('https://dev.azure.com/organisation/_apis/public/distributedtask/webhooks/stepfunction?api-version=6.0-preview',{"tags":[]})
10
something like that
of coarse in a lambda fn you would need to set it up appropriately…
JavaScript
1
6
1
def lambda_handler(event, context):
2
some_url = "https://..."
3
some_data = {"key":"value"}
4
result = do_post(some_url,some_data)
5
return {"statusCode":200,body:"sent callback"}
6