Skip to content

Commit

Permalink
Add opentofu infrastructure provisioning templates
Browse files Browse the repository at this point in the history
  • Loading branch information
shri committed Aug 8, 2024
1 parent afd78e3 commit 640072c
Show file tree
Hide file tree
Showing 39 changed files with 1,580 additions and 0 deletions.
86 changes: 86 additions & 0 deletions deploy/aws/Dockerfile.app
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
ARG PYTHON_BUILDER_IMAGE=3.12-slim-bookworm

## ---------------------------------------------------------------------------------- ##
## ------------------------- Python base -------------------------------------------- ##
## ---------------------------------------------------------------------------------- ##
FROM python:${PYTHON_BUILDER_IMAGE} as python-base
ENV PIP_DEFAULT_TIMEOUT=100 \
PIP_DISABLE_PIP_VERSION_CHECK=1 \
PIP_NO_CACHE_DIR=1 \
PIP_ROOT_USER_ACTION=ignore \
PYTHONDONTWRITEBYTECODE=1 \
PYTHONUNBUFFERED=1 \
PYTHONFAULTHANDLER=1 \
PYTHONHASHSEED=random \
LANG=C.UTF-8 \
LC_ALL=C.UTF-8
RUN apt-get update \
&& apt-get upgrade -y \
&& apt-get install -y --no-install-recommends git tini curl \
&& apt-get install -y dnsutils \
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /root/.cache \
&& rm -rf /var/apt/lists/* \
&& rm -rf /var/cache/apt/* \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false\
&& mkdir -p /workspace/app \
&& pip install --quiet -U pip wheel setuptools virtualenv

## ---------------------------------------------------------------------------------- ##
## ------------------------- Python build base -------------------------------------- ##
## ---------------------------------------------------------------------------------- ##
FROM python-base AS build-base
ARG PDM_INSTALL_ARGS=""
ENV PDM_INSTALL_ARGS="${PDM_INSTALL_ARGS}" \
GRPC_PYTHON_BUILD_WITH_CYTHON=1 \
PATH="/workspace/app/.venv/bin:/usr/local/bin:$PATH"
## -------------------------- add build packages ----------------------------------- ##
RUN apt-get install -y --no-install-recommends build-essential curl \
&& apt-get autoremove -y \
&& apt-get clean -y \
&& rm -rf /root/.cache \
&& rm -rf /var/apt/lists/* \
&& rm -rf /var/cache/apt/* \
&& apt-get purge -y --auto-remove -o APT::AutoRemove::RecommendsImportant=false

## -------------------------- install application ----------------------------------- ##
WORKDIR /workspace/app
COPY pyproject.toml pdm.lock README.md .pre-commit-config.yaml .pylintrc LICENSE.md Makefile \
./
COPY scripts ./scripts/
RUN python -m venv --copies /workspace/app/.venv \
&& /workspace/app/.venv/bin/pip install --quiet pdm nodeenv cython \
&& pdm install ${PDM_INSTALL_ARGS} --no-self \
&& pdm export ${PDM_INSTALL_ARGS} --without-hashes --prod --output=requirements.txt
COPY src ./src/
RUN pdm build


## ---------------------------------------------------------------------------------- ##
## -------------------------------- runtime build ----------------------------------- ##
## ---------------------------------------------------------------------------------- ##
## ------------------------- use base image ---------------------------------------- ##

FROM python-base as run-image
ARG ENV_SECRETS="runtime-secrets"
ARG LITESTAR_APP="app.asgi:app"
ENV ENV_SECRETS="${ENV_SECRETS}" \
LITESTAR_APP="${LITESTAR_APP}"

RUN addgroup --system --gid 65532 nonroot \
&& adduser --no-create-home --system --uid 65532 nonroot \
&& chown -R nonroot:nonroot /workspace
## -------------------------- install application ----------------------------------- ##
COPY --from=build-base --chown=65532:65532 /workspace/app/requirements.txt /tmp/requirements.txt
COPY --from=build-base --chown=65532:65532 /workspace/app/dist /tmp/
WORKDIR /workspace/app
RUN pip install --quiet --disable-pip-version-check --no-deps --requirement=/tmp/requirements.txt
RUN pip install --quiet --disable-pip-version-check --no-deps /tmp/*.whl

USER nonroot
STOPSIGNAL SIGINT
EXPOSE 8000
ENTRYPOINT [ "tini", "--" ]
CMD [ "litestar", "run", "--host", "0.0.0.0" ]
VOLUME /workspace/app
Empty file added deploy/aws/README.md
Empty file.
277 changes: 277 additions & 0 deletions deploy/aws/ecs-task-definition-prod.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
{
"family": "chapter-app-family-prod",
"containerDefinitions": [
{
"name": "app",
"image": "050206582437.dkr.ecr.eu-central-1.amazonaws.com/chapter-app-prod:latest",
"cpu": 256,
"memory": 512,
"essential": true,
"command": ["litestar", "run", "--host", "0.0.0.0"],
"dependsOn": [
{
"condition": "SUCCESS",
"containerName": "db-migrator"
},
{
"condition": "SUCCESS",
"containerName": "worker"
}
],
"portMappings": [
{
"containerPort": 8000,
"hostPort": 8000,
"protocol": "tcp"
}
],
"environment": [
{
"name": "LITESTAR_APP",
"value": "app.asgi:create_app"
},
{
"name": "APP_ENVIRONMENT",
"value": "docker"
},
{
"name": "OPENAPI_CONTACT_EMAIL",
"value": "[email protected]"
},
{
"name": "OPENAPI_CONTACT_NAME",
"value": "Devs"
},
{
"name": "OPENAPI_TITLE",
"value": "Chapter API"
},
{
"name": "DB_MIGRATION_DDL_VERSION_TABLE",
"value": "ddl_version"
},
{ "name": "SAQ_USE_SERVER_LIFESPAN", "value": "false" }
],
"secrets": [
{
"name": "APP_SECRET_KEY",
"valueFrom": "arn:aws:ssm:eu-central-1:050206582437:parameter/prod/chapter/app/app-secret-key"
},
{
"name": "SLACK_ALERTS_URL",
"valueFrom": "arn:aws:ssm:eu-central-1:050206582437:parameter/prod/chapter/app/slack-alerts-url"
},
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-Jig3q1:password::"
},
{
"name": "DB_HOST",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:host::"
},
{
"name": "DB_PORT",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:port::"
},
{
"name": "DB_USER",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:username::"
},
{
"name": "DB_NAME",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:dbname::"
}
],
"healthCheck": {
"command": [
"CMD-SHELL",
"curl -f http://localhost:8000/health || exit 1"
],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 60
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/chapter-app-app-prod",
"awslogs-region": "eu-central-1",
"awslogs-stream-prefix": "ecs"
}
}
},
{
"name": "db-migrator",
"image": "050206582437.dkr.ecr.eu-central-1.amazonaws.com/nectar:latest",
"cpu": 256,
"memory": 512,
"essential": false,
"command": ["litestar", "database", "upgrade", "--no-prompt"],
"environment": [
{
"name": "LITESTAR_APP",
"value": "app.asgi:create_app"
},
{
"name": "APP_ENVIRONMENT",
"value": "docker"
},
{
"name": "OPENAPI_CONTACT_EMAIL",
"value": "[email protected]"
},
{
"name": "OPENAPI_CONTACT_NAME",
"value": "Devs"
},
{
"name": "OPENAPI_TITLE",
"value": "Nectar API"
},
{
"name": "DB_MIGRATION_DDL_VERSION_TABLE",
"value": "ddl_version"
},
{ "name": "SAQ_USE_SERVER_LIFESPAN", "value": "false" }
],
"secrets": [
{
"name": "APP_SECRET_KEY",
"valueFrom": "arn:aws:ssm:eu-central-1:050206582437:parameter/prod/chapter/app/app-secret-key"
},
{
"name": "SLACK_ALERTS_URL",
"valueFrom": "arn:aws:ssm:eu-central-1:050206582437:parameter/prod/chapter/app/slack-alerts-url"
},
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-Jig3q1:password::"
},
{
"name": "DB_HOST",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:host::"
},
{
"name": "DB_PORT",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:port::"
},
{
"name": "DB_USER",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:username::"
},
{
"name": "DB_NAME",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:dbname::"
}
],
"healthCheck": {
"command": ["CMD-SHELL", "exit 0"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 60
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/chapter-app-db-migrator-prod",
"awslogs-region": "eu-central-1",
"awslogs-stream-prefix": "ecs"
}
}
},
{
"name": "worker",
"image": "050206582437.dkr.ecr.eu-central-1.amazonaws.com/chapter:latest",
"cpu": 256,
"memory": 512,
"essential": false,
"command": ["litestar", "workers", "run"],
"environment": [
{
"name": "LITESTAR_APP",
"value": "app.asgi:create_app"
},
{
"name": "APP_ENVIRONMENT",
"value": "docker"
},
{
"name": "OPENAPI_CONTACT_EMAIL",
"value": "[email protected]"
},
{
"name": "OPENAPI_CONTACT_NAME",
"value": "Devs"
},
{
"name": "OPENAPI_TITLE",
"value": "Nectar API"
},
{
"name": "DB_MIGRATION_DDL_VERSION_TABLE",
"value": "ddl_version"
},
{ "name": "SAQ_USE_SERVER_LIFESPAN", "value": "false" }
],
"secrets": [
{
"name": "APP_SECRET_KEY",
"valueFrom": "arn:aws:ssm:eu-central-1:050206582437:parameter/prod/chapter/app/app-secret-key"
},
{
"name": "SLACK_ALERTS_URL",
"valueFrom": "arn:aws:ssm:eu-central-1:050206582437:parameter/prod/chapter/app/slack-alerts-url"
},
{
"name": "DB_PASSWORD",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-Jig3q1:password::"
},
{
"name": "DB_HOST",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:host::"
},
{
"name": "DB_PORT",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:port::"
},
{
"name": "DB_USER",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:username::"
},
{
"name": "DB_NAME",
"valueFrom": "arn:aws:secretsmanager:eu-central-1:050206582437:secret:prod/nectar/rds-YzLzXz:dbname::"
}
],
"healthCheck": {
"command": ["CMD-SHELL", "exit 0"],
"interval": 30,
"timeout": 5,
"retries": 3,
"startPeriod": 60
},
"logConfiguration": {
"logDriver": "awslogs",
"options": {
"awslogs-create-group": "true",
"awslogs-group": "/ecs/chapter-app-worker-prod",
"awslogs-region": "eu-central-1",
"awslogs-stream-prefix": "ecs"
}
}
}
],
"runtimePlatform": {
"operatingSystemFamily": "LINUX"
},
"networkMode": "awsvpc",
"requiresCompatibilities": ["FARGATE"],
"cpu": 1024,
"memory": 2048,
"executionRoleArn": "arn:aws:iam::050206582437:role/chapter-app-ecs-task-execution-role-prod",
"taskRoleArn": "arn:aws:iam::050206582437:role/chapter-app-ecs-task-role-prod"
}
Loading

0 comments on commit 640072c

Please sign in to comment.