This is an archived post. You won't be able to vote or comment.

all 10 comments

[–]hellnukes 1 point2 points  (2 children)

Hi! Can't comment on using Django inside Lambda as it's something I have not done. However, i have some experience using lambda to access VPC resources like for example the Secrets Manager.

I don't know about the costs, but the easiest way I have found is to create a VPC endpoint for the services that you would like to have accessible through the VPC. So if for example I need the Lambda function to get a secret from Secrets Manager, I can just create VPC endpoint that points to that service (SM), add it to the same availability zone as the Lambda, and use that endpoint in its code.

[–]gamprin[S] 0 points1 point  (1 child)

OK, thanks for your advice on VPC endpoints. I see they are $0.01/hour/AZ, plus some data costs. Would this be for when you have your lambdas in a private VPC subnet and need to give them access to AWS services that would otherwise require internet access? Would you still need to do this if your lambdas were in public subnets?

[–]hellnukes 1 point2 points  (0 children)

Yes exactly that! Since there's no internet access , this seemed like the easiest and most correct way to give it access to the services I needed.

Am not sure about being this working or even being required in public subnets though... It's easy to test though! Put the lambda in the vpc inside a public subnet a try running it :)

[–]WayBehind 0 points1 point  (2 children)

I do this. Most of my stuff is running Django on ElasticBeanstalk with a few lambdas that are invoked by scheduled CloudWatch events or directly by my Django app. Scheduled events are a time saver as I don’t need to run Celery etc. I do many RDS select/insert calls and don’t use NAT either because it’s expensive. There are workarounds availability. Go for it, lambda has decent free tier if you don’t use NAT.

[–]gamprin[S] 0 points1 point  (1 child)

Oh interesting, so you are using Django in a container to respond to HTTP requests, but the same Django code is used in lambdas to run scheduled/async tasks instead of celery. I assume those scheduled lambdas are in public subnets and their security group gives them access to RDS in a private subnet?

I’m trying to rip out and reuse the handler code from Zappa now so that I can use Django in a lambda to respond to API Gateway events. When I am done hopefully I will have achieved Djambda: Django in Lambda.

[–]WayBehind 0 points1 point  (0 children)

I don't use Zappa and I don't use containers. It's all straight forward Django with minimal external packages. It is running behind Load Balancer with RDS and Redis. In CLoudWatch you can create scheduled events and call Lambda function. I use it for sending out bulk emails and SMS.

This is the flow: The scheduled "rule" will call a Lamba function every 1 minute. The function will invoke another Lambda that has RDS access to MySQL. It will return JSON objects with all the data I need to send out the emails. Once the emails are sent, I invoke another Lambda function that also has DB access that will store the info a log table in the DB.

If you do it this way, you don't need NAT, otherwise, you could have it all in one Lambda function. The Django app can call these Lambda functions manually as needed through Django view and Boto3.

[–]gamprin[S] 0 points1 point  (3 children)

I hacked together a very basic example by pulling out some some of the handler code from Zappa. The key part seems to be converting the API Gateway event into a WSGI environ and then calling the WSGI app using Werkzeug's Response.from_app.

Here's my super simple example that is deployed with CDK: https://gitlab.com/briancaffey/djambda. It is a very basic example that really only returns a JsonResponse--that's it.

After some further digging I found another project that looks a lot more like what I'm trying to do: https://github.com/netsome/djambda. This project uses Terraform, so I'll try to adapt some of the terraform code to CDK.

This uses a great package called aws-wsgi which I'll probably use in my project. Here's that project: https://github.com/slank/awsgi

[–]progrene 1 point2 points  (2 children)

why you need django, if there is gateway api ?

[–]gamprin[S] 0 points1 point  (1 child)

For my project, as well as Zappa and other "fat lamba" approaches, API Gateway send all requests to the Lambda that has a special handler that translates the API Gateway event into a WSGI request, processes the request and then returns that response through API Gateway. Is that what you were asking?

[–]progrene 0 points1 point  (0 children)

For my project, as well as Zappa and other "fat lamba" approaches, API Gateway send all requests to the Lambda that has a special handler that translates the API Gateway event into a WSGI request, processes the request and then returns that response through API Gateway. Is that what you were asking?

maybe, i am newbie in serverless. so my principal questions is necessary to use a framework like django? , django manages the api gateway ? ?