Skip to content

Instantly share code, notes, and snippets.

View marianobrc's full-sized avatar

Mariano Martinez Grasso marianobrc

  • worldwide
View GitHub Profile
@marianobrc
marianobrc / django.local.Dockerfile
Last active February 3, 2022 17:33
A Dockerfile sample for a Django development environment
# Pull base image
FROM python:3.10
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Set working directory
WORKDIR /code
#!/bin/bash
# When starting the django app container, we need to wait until the postgress DB is ready to receive connections
# docker-compose "depends_on: - db" checks the container started, but is not enough to check that the database is ready to take connections
# This script also accepts a command to be executed after the DB is ready (i.e. migrate, runserver or a script..)
function postgres_ready(){
python << END
import sys
import psycopg2
try:
print("Trying to connect to database '$DB_NAME' on host '$DB_HOST'..")
@marianobrc
marianobrc / django.local.docker-compose.yml
Last active April 6, 2022 12:27
A docker-compose.yml sample for a local django-postgres development environment
version: '3.5'
services:
db:
image: postgres:10
restart: always
# Optional: Map the container port to a host port to be able to connect with a local db client
ports:
- ${DB_PORT}:${DB_PORT}
environment:
@marianobrc
marianobrc / django.docker.local.start-dev-server.sh
Created February 3, 2022 18:07
A script to start the django local dev server
#!/bin/sh
echo "Running migrations.."
python manage.py migrate
echo "Starting server.."
python manage.py runserver 0.0.0.0:8000
@marianobrc
marianobrc / django.docker.postgres.db.sql
Created February 7, 2022 15:38
A script to initialize a PostreSQL database
\set DB_NAME `echo "$DB_NAME"`
\set DB_USER `echo "$DB_USER"`
\set DB_PASSWORD `echo "$DB_PASSWORD"`
CREATE USER :DB_USER WITH PASSWORD :'DB_PASSWORD';
CREATE DATABASE :DB_NAME;
GRANT ALL PRIVILEGES ON DATABASE :DB_NAME TO :DB_USER;
ALTER ROLE :DB_USER CREATEDB;
@marianobrc
marianobrc / django.docker.multistage.Dockerfile
Last active March 2, 2022 18:57
A multistage Dockerfile to build a Django app for development and production
# Base off the official python image
# Define a common stage for dev and prod images called base
FROM python:3.10 as base
# Set environment variables
ENV PYTHONDONTWRITEBYTECODE 1
ENV PYTHONUNBUFFERED 1
# Create a user to avoid running containers as root in production
RUN addgroup --system web \
&& adduser --system --ingroup web web
# Install os-level dependencies (as root)
@marianobrc
marianobrc / django.docker.prod.start-prod-server.sh
Created February 10, 2022 21:25
A script to start gunicorn to serve a django app in production
#!/bin/sh
python manage.py migrate
python manage.py collectstatic --noinput
gunicorn app.wsgi --bind 0.0.0.0:8000 --timeout 60 --access-logfile - --error-logfile -
@marianobrc
marianobrc / cloudformation_infrastructure.yml
Last active March 11, 2022 14:02
A CloudFormation Template for a LoadBalanced Fargate Service
AWSTemplateFormatVersion: '2010-09-09'
Description: Deploy a service on AWS Fargate, hosted in a private subnet, but accessible via a public load balancer.
Parameters:
StackName:
Type: String
Default: ecs-python-sample-network
Description: The name of the parent Fargate networking stack that you created. Necessary
to locate and reference resources created by that stack.
ServiceName:
Type: String
@marianobrc
marianobrc / cdk_load_balanced_fargate_service.py
Last active March 22, 2022 21:11
A CDK stack sample for load balanced ECS/Fargate service
from aws_cdk import (
Stack,
aws_ec2 as ec2,
aws_sqs as sqs,
aws_ecs as ecs,
aws_ecs_patterns as ecs_patterns,
aws_certificatemanager as acm,
aws_elasticloadbalancingv2 as elbv2,
aws_ssm as ssm
)
@marianobrc
marianobrc / cdk.network_stack.py
Last active March 30, 2022 12:13
A CDK Stack for creating the basic network resources
from aws_cdk import (
Stack,
aws_ec2 as ec2,
aws_ssm as ssm,
aws_ecs as ecs,
)
from constructs import Construct
class NetworkStack(Stack):