Twitter CRC Challenge not working on AWS Lambda/API Gateway

Viewed 89

I'm trying to get a twitter webhook CRC challenge working on AWS Lambda & API Gateway

I've been following this tutorial and so far the webhook works locally but not on AWS.

Here's the code I have in AWS Lambda:

import json
import base64
import hashlib
import hmac
import json
from flask import Flask, request, Response, jsonify

key = '#####################'

def lambda_handler(event, context):
    
    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

def webhook_challenge():
    consumer_secret_bytes = bytes(key, 'utf-8')
    message = bytes(request.args.get('crc_token'),'utf-8')

    sha256_hash_digest = hmac.new(consumer_secret_bytes, message , digestmod=hashlib.sha256).digest()
    response={
        'response_token':'sha256='+base64.b64encode(sha256_hash_digest).decode('utf-8')
    }

    return json.dumps(response)
webhook_challenge() 

The response I get when I send a get request to the API Gateway link

https://#######.execute-api.us-east-2.amazonaws.com/dev/twitter?crc_token=foo

I get an error:

{"message":"Missing Authentication Token"}

When I run a test on API gateway I get this towards the end of the logs:

Sat Dec 11 19:09:54 UTC 2021 : Endpoint response headers: {Date=Sat, 11 Dec 2021 19:09:54 GMT, Content-Type=application/json, Content-Length=114, Connection=keep-alive, x-amzn-RequestId=5c1ac93d-9f24-43b1-a8dd-42ef0210a4e3, X-Amz-Function-Error=Unhandled, x-amzn-Remapped-Content-Length=0, X-Amz-Executed-Version=$LATEST, X-Amzn-Trace-Id=root=1-61b4f77e-f17e25f619a9c5287a90ca8a;sampled=0} Sat Dec 11 19:09:54 UTC 2021 : Endpoint response body before transformations: {"errorMessage":"2021-12-11T19:09:54.379Z 5c1ac93d-9f24-43b1-a8dd-42ef0210a4e3 Task timed out after 3.02 seconds"} Sat Dec 11 19:09:54 UTC 2021 : Lambda execution failed with status 200 due to customer function error: 2021-12-11T19:09:54.379Z 5c1ac93d-9f24-43b1-a8dd-42ef0210a4e3 Task timed out after 3.02 seconds. Lambda request id: 5c1ac93d-9f24-43b1-a8dd-42ef0210a4e3 Sat Dec 11 19:09:54 UTC 2021 : Method completed with status: 502

I'm sure it's something simple but it's my first time working with webhooks and AWS so I can't quite put a finger on it yet, any help will be much appreciated

1 Answers

From the error message:

{"errorMessage":"2021-12-11T19:09:54.379Z 5c1ac93d-9f24-43b1-a8dd-42ef0210a4e3 Task timed out after 3.02 seconds"}

Lambdas by default are set to run for 3 seconds, but this is not enough time four your use case. You can increase the allowed running time for the function in the General Configurations for the Lambda:

enter image description here

For the API Gateway error:

{"message":"Missing Authentication Token"}

First of all, you should check if the API Gateway methods are deployed correctly and you are calling the correct method using the correct url. For any changes on your API you have to redeploy it, otherwise it wont be taken in consideration:

enter image description here

If you have an API Key set up, you should pass it with x-api-key header.

Related