Deploying a Django project on AWS Lambda using Serverless (Part 2)
Tags: aws, django, infrastructure
Published: 2021-05-10 11:40:59

---
title: Deploying a Django project on AWS Lambda using Serverless (Part 2)
published: true
description: How to prepare AWS infrastructure for deploying a Django project.
tags: aws, django, infrastructure
canonical_url: https://vadymkhodak.com/blog/deploy-django-app-on-aws-lambda-using-serverless-part-2/
series: Django with AWS Lambda.
---

After my previous blog post [Deploy Django App on AWS Lambda using Serverless (Part 1)](https://blog.vadymkhodak.com/deploy-django-app-on-aws-lambda-using-serverless-part-1), people started asking me about AWS infrastructure for Django projects, so I decided to share my experience with this. 

It is not obvious (for people who don't have enough experience with AWS resources) how to create and configure all the necessary AWS resources to deploy a Django project on AWS Lambda using Serverless. 

Here are a list of ways of how to do this:
- manually via the [AWS console](https://console.aws.amazon.com/console/home)
- automatically using [Terraform](https://www.terraform.io/)
- automatically using [AWS SDK](https://aws.amazon.com/sdk-for-python/)

In this blog post, I will show you how to do it manually via [AWS console](https://console.aws.amazon.com/console/home).

## Update configuration for existing AWS resource and create new ones

**Step 1**: Create your own AWS account (if you don't have one).
Here is [a link to a manual for creating and activating an AWS account](https://aws.amazon.com/premiumsupport/knowledge-center/create-and-activate-aws-account/).


**Step 2**: Go to the [AWS Management Console](https://console.aws.amazon.com/console/home)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3wqrrnq2vy6uw5us63y2.png)
 

**Step 3**: Select your region. In my case, it is `US East (N. Virginia)us-east-1`

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fu6pmiiywlz3653nxi5z.png)
  

**Step 4**: Update `Security Group` with rules for AWS RDS service (PostgreSQL)

- *Type `EC2` in the search bar and click on the `EC2` service in the search results*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m59us8el27f25wqdak7k.png) 

- *Choose the `Security Groups` option in the `Network & Security` section*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/b4ifmpp3uukgm88drsl5.png)
 
- *Click on your security group id*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dwmsas8oxrwd7k3tmko9.png)

- *Click on `Edit inbound rules`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eqapv9ia94bhl9297u79.png)
 
- *Click on `Add rule`. Then, select the `PostgreSQL` option in the `Type` column. Next, choose the `Anywhere` option in the `Source` column. Finally, click on the `Save rules` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1n8zu4g3y7i23v5u87kr.png)
 
- *Go to the `Outbound rules` tab and add the same rules that were described in the previous section*

**Step 5**: Create an `IAM role`

- *Type `IAM` in the search bar and click on the `IAM` service in the search results*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qsrk0qqqvicd15qllmu.png) 
 
- *Click on `Roles` in the `IAM` side bar or in the `IAM dashboard`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l7cmyaj6lmqfh8o2x1oa.png)
 

- *Click on the `Create Role` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4w39xffqhiqeq2l12arx.png)
 
- *Select `AWS service`, `Lambda`, and click on the `Next: Permissions` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ybj55xal891k3qpbl6x4.png)
 
- *Type `Lambda` in the search bar, select the `AWSLambda_FullAccess` policy, click on the `Next: Tags` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hrvu6snyyince40ed8fp.png)
 
- *Click on the `Next: Review` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t03i43if302cx560l6bp.png)
 
- *Type `Role name`, `Role description`, and click on the `Create role` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yuhbw3q5fe17i2lepbao.png)
 
- *Click on the created role name*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6g3c98e9npmbsgwsv9be.png)
 
- *Click on the `Copy Role ARN` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bef89qld3wzj2q0dwxd8.png) 

Next, you should add the role ARN to the Serverless configuration directly or using environment variables (for example `.env` file)

```env
ROLE=arn:aws:iam::<your-aws-account-id>:role/exec_lambda
```

**Step 6**: Create `S3 buckets` for static assets and deployment

- *Type `S3` in the search bar and click on the `S3` service in the search results*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/k9ptr0u2mqpc47jql6hh.png)
 
- *Click on the `Create bucket` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qb3w1ema3zu2f08atjse.png)
 
- *Type `Bucket name`, select your `AWS region`, unselect `Block all public access` and click on the `Create bucket` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/q7qqd1rup6evfa8ys7wq.png)
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wbmn6n98ongrakwyre18.png)
 
Then, you should repeat all the steps mentioned above to create an S3 bucket for deployment.

Next, you should add your bucket names to your Django and Serverless configurations directly or using environment variables (for example `.env` file)

```env
AWS_STORAGE_BUCKET_NAME='django-react-static-assets'
DEPLOYMENT_BUCKET='django-react-deployments'
```

**Step 7**: Create a `CloudFront distribution`

- *Type `CloudFront` in the search bar and click on the `CloudFront` service in the search results*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7du2dc50zyg2ixr1iv50.png)
 
- *Click on the `Create Distribution` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ttuhrn7efjcj5zwcek7k.png)
 
- *Click on the `Get started` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w0kuunqwe2m9c9wcuezo.png)
 
- *Select your `S3 bucket`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/japfsufsxfi9hbwhayxf.png)
 
- *Select: `Yes` for `Restrict Bucket Access`, `Create a New Identity` for `Origin Access Identity`, `Yes, Update Bucket Policy` for `Grant Read Permissions on Bucket`, `HTTP and HTTPS` for `Viewer Protocol Policy`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jwr4cbsrvcwbhldbzrv3.png)
 
- *Type "some comment" (optional) and click on the `Create Distribution` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/824es8l4gvcrojlfmbqn.png)
 
- *Go to the distributions list and copy the `Domain Name`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3jrh6mus8rpjltl74124.png) 

Then, you should add CloudFront distribution `Domain Name` to your Django and Serverless configurations directly or using environment variables (for example `.env` file)

```.env
AWS_S3_CDN_DOMAIN="<domain-id>.cloudfront.net"
```
**Step 8**: Create `RDS`

- *Type `RDS` in the search bar and click on the `RDS` service in the search results*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/frsbgpa845qlmehmrqoa.png)
 
- *Click on the `Create database` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jw14jucu945330m9l8hr.png)
 
- *Select `Standard create`, `PostgreSQL`, and `Version` (in my example, it is PostgreSQL 12.5-R1)*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/0ek0ukwn3i85ecupbm77.png)
 
- *Select `Free Tier` as a `Template`, fill in `DB instance identifier`,  `Master username`, `Master password`, `Confirm password`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mizrmdyexkyfvbmi7qn.png) 

- *Select `Burstable classes (includes t classes)` and `db.t2.micro` for `DB instance class`, `General Purpose (SSD)` as `Storage type`, and `20` as `Allocated storage`, unselect `Enable storage autoscaling`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1eiavy5q05af3n79b06b.png)
 
- *Skip the `Availability & durability` section*

- *Configure `Connectivity`*:
    - Select your default VPC as `Virtual private cloud (VPC)`
    > After a database is created, you can't change the VPC selection.
    - Select `default` as `Subnet group`
    - Select `Yes` for `Public access`
    - Select `Choose existing` for `VPC security group`, and select `default` for `Existing VPC security groups` section
    - Select  `Availability Zone` (in my example `us-east-1a`)

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t3v687vgjl1z9od5umro.png) 

- *Select `Password authentication` or any other you want to use as `Database authentication options`, unselect all check boxes in `Additional configuration` and type `Initial database name` (in my example, it is `django_aws`)*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/becethsz6x6mdnwpury7.png)
 
![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bnytql84kfpwa6xilfkt.png) 

- *Click on the `Create database` button*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/90ieoqtef1qfea8k6xv5.png) 

- *Go to the `RDS` dashboard and click on `Databases` in the `RDS` side bar or on `DB instances`*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6dm7xyeauu4ap7qzr4c.png)
 
- *Click on the created database identifier*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/z5tmyjvtut6hhz750vd7.png) 

- *Copy the database endpoint, the subnets, and the security groups*

![image](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zsw7spwglji3a3jo7er2.png)
 
Then, you should add this info to your Django and Serverless configurations directly or using environment variables (for example `.env` file)

```
DB_HOST='django-aws.<db-domain-id>.us-east-1.rds.amazonaws.com'
DB_USER='<your-master-db-user>'
DB_PASSWORD='<password-for-your-master-db-user>'
DB_NAME='<your-db-name>'
SECURITY_GROUPS=sg-<security-group-id>
SUBNETS=subnet-<subnet-id>,subnet-<subnet-id>,subnet-<subnet-id>,subnet-<subnet-id>,subnet-<subnet-id>,subnet-<subnet-id>
```

> **NOTE**: This is just an example of AWS configuration I use in my example. You may use your own configuration.


## Automate managing your AWS infrastructure 

I showed you how to configure all the necessary AWS resources for a Django project manually using the [AWS Management Console](https://console.aws.amazon.com/console/home). There are some ways to automate this process. I'll show how to manage AWS resources using [Terraform](https://www.terraform.io/) (infrastructure as code) in my next blog post. Follow me on Twitter [@vadim_khodak](https://twitter.com/vadim_khodak) or on [LinkedIn](https://www.linkedin.com/in/vadym-khodak-0b1a05149/) so you do not miss the next posts.