Get Started
Deploy Django with LaunchFlow
Deploy a Django application to AWS Fargate with LaunchFlow.
View the source code for this guide in our examples repo.
0. Set up your Django Project
Initialize Django
Run the standard Django commands to create a new Django project and app.
1mkdir launchflow-django
2cd launchflow-django
3django-admin startproject lfdjango .
4python manage.py startapp app
Add a basic view to app/views.py
:
1from django.http import HttpResponse
2import launchflow as lf
3
4def index(request):
5 return HttpResponse(f"Hello from {lf.project}/{lf.environment}")
Add your view to lfdjango/urls.py
:
1from django.urls import path
2from app import views
3
4urlpatterns = [
5 path('', views.index, name='index'),
6]
Update lfdjango/settings.py
to allow hosts:
1ALLOWED_HOSTS = ["*"]
You will want to change these in production later, but now lets just get things working.
Create a Dockerfile
in your project root directory:
1FROM public.ecr.aws/docker/library/python:3.11-slim
2
3WORKDIR /code
4
5RUN apt-get update && apt-get install -y --no-install-recommends gcc libpq-dev && apt-get clean && rm -rf /var/lib/apt/lists/*
6RUN pip install --no-cache-dir -U pip setuptools wheel && pip install --no-cache-dir launchflow[gcp] django
7
8COPY . /code/
9
10ENV PORT=80
11EXPOSE $PORT
12
13CMD python manage.py runserver 0.0.0.0:$PORT
1. Initialize Launch Flow
Install the LaunchFlow Python SDK and CLI using pip
.
1pip install launchflow[aws]
Initialize LaunchFlow in your project
1lf init --backend=local
- Name your project
- Select
Yes
for creating an exampleinfra.py
- Select
AWS
for your cloud provider - Select
ECS Fargate
for your service
Once finished you will get an infra.py
that looks like:
1import launchflow as lf
2
3# ECSFargateService Docs: https://docs.launchflow.com/reference/aws-services/ecs-fargate
4api = lf.aws.ECSFargateService(
5 "my-ecs-api",
6 dockerfile="Dockerfile", # Path to your Dockerfile
7)
ECSFargateService will build your Dockerfile and deploy to ECS Fargate. You can provide additional fields to ECSFargateService
to configure things like machine type, num instances, or even a custom domain.
2. Deploy your Service
Make sure you have local AWS credentials set up before deploying.
1lf deploy
- Name your environment (
dev
is a good first name) - Select your cloud provider
AWS
- Confirm the resources to be created
- Select the service to deploy
Once complete you will see a link to your deployed service on ECS Fargate.
3. Cleanup your Resources
Optionally you can delete all your resources, service, and environments with:
1lf destroy
2lf environments delete
4. Visualize, Share, and Automate
LaunchFlow Cloud usage is optional and free for individuals.
Using the local backend like we did above works fine for starting a project, but doesn't offer a way to share state between multiple users. LaunchFlow Cloud is a web-based service for managing, sharing, and automating your infrastructure. It's free small teams and provides a simple, secure way to collaborate with your team and automate your release pipelines.
Sign up for LaunchFlow Cloud and connect your local environment by running:
1lf init --backend=lf
This will create a project in your LaunchFlow Cloud account and migrate your local state to the LaunchFlow Cloud backend.
What's next?
- Add resources to your application
- Promote your application to a production enviroment
- Learn more about Environments, Services, and Resources
- Join theLaunchFlow Slack community
- View your application in theLaunchFlow console