DEV Community

Vadym Khodak
Vadym Khodak

Posted on • Updated on • Originally published at vadymkhodak.com

Deploying a Django project on AWS Lambda using Serverless (Part 2)

After my previous blog post 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:

In this blog post, I will show you how to do it manually via AWS console.

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.

Step 2: Go to the AWS Management Console

image

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

image

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

  • Choose the Security Groups option in the Network & Security section

image

  • Click on your security group id

image

  • Click on Edit inbound rules

image

  • 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

  • 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

  • Click on Roles in the IAM side bar or in the IAM dashboard

image

  • Click on the Create Role button

image

  • Select AWS service, Lambda, and click on the Next: Permissions button

image

  • Type Lambda in the search bar, select the AWSLambda_FullAccess policy, click on the Next: Tags button

image

  • Click on the Next: Review button

image

  • Type Role name, Role description, and click on the Create role button

image

  • Click on the created role name

image

  • Click on the Copy Role ARN button

image

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



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


Enter fullscreen mode Exit fullscreen mode

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

  • Click on the Create bucket button

image

  • Type Bucket name, select your AWS region, unselect Block all public access and click on the Create bucket button

image

image

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)



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


Enter fullscreen mode Exit fullscreen mode

Step 7: Create a CloudFront distribution

  • Type CloudFront in the search bar and click on the CloudFront service in the search results

image

  • Click on the Create Distribution button

image

  • Click on the Get started button

image

  • Select your S3 bucket

image

  • 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

  • Type "some comment" (optional) and click on the Create Distribution button

image

  • Go to the distributions list and copy the Domain Name

image

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



AWS_S3_CDN_DOMAIN="<domain-id>.cloudfront.net"


Enter fullscreen mode Exit fullscreen mode

Step 8: Create RDS

  • Type RDS in the search bar and click on the RDS service in the search results

image

  • Click on the Create database button

image

  • Select Standard create, PostgreSQL, and Version (in my example, it is PostgreSQL 12.5-R1)

image

  • Select Free Tier as a Template, fill in DB instance identifier, Master username, Master password, Confirm password

image

  • 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

  • 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

  • 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

image

  • Click on the Create database button

image

  • Go to the RDS dashboard and click on Databases in the RDS side bar or on DB instances

image

  • Click on the created database identifier

image

  • Copy the database endpoint, the subnets, and the security groups

image

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>


Enter fullscreen mode Exit fullscreen mode

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. There are some ways to automate this process. I'll show how to manage AWS resources using Terraform (infrastructure as code) in my next blog post. Follow me on Twitter @vadim_khodak or on LinkedIn so you do not miss the next posts.

Top comments (1)

Collapse
 
aroy01986 profile image
A

mate , i really appreciate the time and effort you have put in . very detailed and helpful