Deploying a Django project on AWS Lambda using Serverless (Part 2)
Vadym Khodak
Posted on May 10, 2021
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:
- manually via the AWS console
- automatically using Terraform
- automatically using AWS SDK
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
Step 3: Select your region. In my case, it is US East (N. Virginia)us-east-1
Step 4: Update Security Group
with rules for AWS RDS service (PostgreSQL)
- Type
EC2
in the search bar and click on theEC2
service in the search results
- Choose the
Security Groups
option in theNetwork & Security
section
- Click on your security group id
- Click on
Edit inbound rules
- Click on
Add rule
. Then, select thePostgreSQL
option in theType
column. Next, choose theAnywhere
option in theSource
column. Finally, click on theSave rules
button
- 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 theIAM
service in the search results
- Click on
Roles
in theIAM
side bar or in theIAM dashboard
- Click on the
Create Role
button
- Select
AWS service
,Lambda
, and click on theNext: Permissions
button
- Type
Lambda
in the search bar, select theAWSLambda_FullAccess
policy, click on theNext: Tags
button
- Click on the
Next: Review
button
- Type
Role name
,Role description
, and click on theCreate role
button
- Click on the created role name
- Click on the
Copy Role ARN
button
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
Step 6: Create S3 buckets
for static assets and deployment
- Type
S3
in the search bar and click on theS3
service in the search results
- Click on the
Create bucket
button
- Type
Bucket name
, select yourAWS region
, unselectBlock all public access
and click on theCreate bucket
button
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'
Step 7: Create a CloudFront distribution
- Type
CloudFront
in the search bar and click on theCloudFront
service in the search results
- Click on the
Create Distribution
button
- Click on the
Get started
button
- Select your
S3 bucket
- Select:
Yes
forRestrict Bucket Access
,Create a New Identity
forOrigin Access Identity
,Yes, Update Bucket Policy
forGrant Read Permissions on Bucket
,HTTP and HTTPS
forViewer Protocol Policy
- Type "some comment" (optional) and click on the
Create Distribution
button
- Go to the distributions list and copy the
Domain Name
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"
Step 8: Create RDS
- Type
RDS
in the search bar and click on theRDS
service in the search results
- Click on the
Create database
button
- Select
Standard create
,PostgreSQL
, andVersion
(in my example, it is PostgreSQL 12.5-R1)
- Select
Free Tier
as aTemplate
, fill inDB instance identifier
,Master username
,Master password
,Confirm password
- Select
Burstable classes (includes t classes)
anddb.t2.micro
forDB instance class
,General Purpose (SSD)
asStorage type
, and20
asAllocated storage
, unselectEnable storage autoscaling
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
asSubnet group
- Select
Yes
forPublic access
- Select
Choose existing
forVPC security group
, and selectdefault
forExisting VPC security groups
section - Select
Availability Zone
(in my exampleus-east-1a
)
- Select your default VPC as
- Select
Password authentication
or any other you want to use asDatabase authentication options
, unselect all check boxes inAdditional configuration
and typeInitial database name
(in my example, it isdjango_aws
)
- Click on the
Create database
button
- Go to the
RDS
dashboard and click onDatabases
in theRDS
side bar or onDB instances
- Click on the created database identifier
- Copy the database endpoint, the subnets, and the security groups
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. 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.
Posted on May 10, 2021
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.