Search code examples
djangotwitteraws-lambdaoauthzappa

Serverless Django app (AWS Lambda via Zappa) times out when trying to OAuth to Twitter


I've got a Django app setup to use django-allauth to connect to Twitter. The flow is all working locally and I've followed the same setup steps on Lambda to add my tokens, site, etc.

When I try to access the login url (/accounts/twitter/login/) the request eventually times out with this message from AWS Lambda:

{"message": "Endpoint request timed out"}

The last message from zappa tail before the timeout event is:

[1619019159940] [DEBUG] 2021-04-21T15:32:39.939Z 7f66a0e3-58de-4612-82c0-54590d69676f Starting new HTTPS connection (1): api.twitter.com:443

I've seen that the gateways have a 30 second timeout but I don't think it should be taking this long anyway. Locally, it's taking a couple of seconds.

Does anyone with knowledge of these platforms have an idea where the bottleneck might be and where the issue could be? Or have any pointed questions to help debug?

Things I've already checked and (tentatively) ruled out:

  • The database backend is AWS Aurora Serverless, and I did worry that the double-serverless setup might be causing the slow speeds. However, a simple call of the Django management command (zappa manage dev migrate returns takes less than a second so I've ruled that out for now. Plus the Admin dashboard loads fine which is also accessing the DB.
  • I've got both the dev and live URLs added into Twitter's dashboard as valid OAuth callback URLs.

Solution

  • Leaving this answer to help future searches, although it's not the route I'll take.

    Thanks to @Jens in the comments for pointing towards the VPC issue. You need to add a NAT Gateway service to the Lambda to add public internet access to a private VPC.

    "To grant internet access to your function, its associated VPC must have a NAT gateway (or NAT instance) in a public subnet."

    Source: https://aws.amazon.com/premiumsupport/knowledge-center/internet-access-lambda-function/

    This is a per-hour billed extra so might defeat the point of using serverless (if you're using it for small fees and simplicity rather than scale like I was).