Skip to content

Commit

Permalink
commit message
Browse files Browse the repository at this point in the history
  • Loading branch information
lbernhard95 committed Oct 28, 2024
0 parents commit 5a8c9f4
Show file tree
Hide file tree
Showing 22 changed files with 1,231 additions and 0 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
name: Deploy

on:
push:
branches:
- main

jobs:
deploy:
name: deploy
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- uses: actions/checkout@v3
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: github_oidc_role
aws-region: eu-central-1
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build, tag, and push docker image to Amazon ECR
run: |
docker build -t 082113759242.dkr.ecr.eu-central-1.amazonaws.com/schafkopf-scheduler-lambda:latest .
docker push 082113759242.dkr.ecr.eu-central-1.amazonaws.com/schafkopf-scheduler-lambda:latest
167 changes: 167 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,167 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class
.env
# C extensions
*.so
.idea
# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
/infrastructure/.terraform/
/infrastructure/.terraform.lock.hcl
/infrastructure/terraform.exe
/infrastructure/terraform.tfstate
/infrastructure/terraform.tfstate.backup
23 changes: 23 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM --platform=linux/amd64 python:3.12-slim
WORKDIR /app

# https://docs.aws.amazon.com/lambda/latest/dg/python-image.html#python-image-clients
RUN pip install awslambdaric

ENV POETRY_VIRTUALENVS_CREATE=false \
POETRY_NO_INTERACTION=1 \
POETRY_HOME="/opt/poetry"
ENV PYTHONPATH="/app"

RUN pip install poetry==1.8.4

COPY pyproject.toml /app/
COPY poetry.lock /app/

RUN poetry config virtualenvs.create false && poetry install --no-root

COPY scheduler scheduler
COPY main.py main.py
COPY lambda_handler.py lambda_handler.py
ENTRYPOINT [ "/usr/local/bin/python", "-m", "awslambdaric" ]
CMD [ "lambda_handler.lambda_handler" ]
4 changes: 4 additions & 0 deletions infrastructure/cloudwatch.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
resource "aws_cloudwatch_log_group" "tasks_service" {
name = "/aws/lambda/schafkopf_scheduler"
retention_in_days = 30
}
11 changes: 11 additions & 0 deletions infrastructure/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
data "aws_caller_identity" "current" {}

data "aws_region" "current" {}

data "aws_secretsmanager_secret" "gmail_credentials" {
name = "gmail_access_credentials"
}

data "aws_secretsmanager_secret_version" "gmail_credentials" {
secret_id = data.aws_secretsmanager_secret.gmail_credentials.id
}
31 changes: 31 additions & 0 deletions infrastructure/ecr.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
resource "aws_ecr_repository" "schafkopf_scheduler" {
name = "schafkopf-scheduler-lambda"
}

resource "aws_ecr_lifecycle_policy" "schafkopf_scheduler" {
repository = aws_ecr_repository.schafkopf_scheduler.name

policy = jsonencode(
{
"rules" : [
{
"rulePriority" : 1,
"description" : "Keep last 1 untagged images",
"selection" : {
"tagStatus" : "untagged",
"countType" : "imageCountMoreThan",
"countNumber" : 1
},
"action" : {
"type" : "expire"
}
}
]
})
}

/*
data "aws_ecr_image" "schafkopf_scheduler" {
repository_name = aws_ecr_repository.schafkopf_scheduler.name
image_tag = "latest"
}*/
58 changes: 58 additions & 0 deletions infrastructure/github_oidc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
resource "aws_iam_openid_connect_provider" "this" {
url = "https://token.actions.githubusercontent.com"

client_id_list = [
"sts.amazonaws.com",
]

thumbprint_list = ["ffffffffffffffffffffffffffffffffffffffff"]
}

data "aws_iam_policy_document" "oidc" {
statement {
actions = ["sts:AssumeRoleWithWebIdentity"]

principals {
type = "Federated"
identifiers = [aws_iam_openid_connect_provider.this.arn]
}

condition {
test = "StringEquals"
values = ["sts.amazonaws.com"]
variable = "token.actions.githubusercontent.com:aud"
}

condition {
test = "StringLike"
values = ["repo:itsStRaNge/*"]
variable = "token.actions.githubusercontent.com:sub"
}
}
}

resource "aws_iam_role" "this" {
name = "github_oidc_role"
assume_role_policy = data.aws_iam_policy_document.oidc.json
}

data "aws_iam_policy_document" "deploy" {
statement {
effect = "Allow"
actions = [
"*",
]
resources = ["*"]
}
}

resource "aws_iam_policy" "deploy" {
name = "ci-deploy-policy"
description = "Policy used for deployments on CI"
policy = data.aws_iam_policy_document.deploy.json
}

resource "aws_iam_role_policy_attachment" "attach-deploy" {
role = aws_iam_role.this.name
policy_arn = aws_iam_policy.deploy.arn
}
35 changes: 35 additions & 0 deletions infrastructure/lambda.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*resource "aws_lambda_function" "schafkopf_scheduler" {
function_name = "schafkopf_scheduler"
package_type = "Image"
image_uri = "${aws_ecr_repository.schafkopf_scheduler.repository_url}:latest"
source_code_hash = replace(data.aws_ecr_image.schafkopf_scheduler.image_digest, "sha256:", "")
timeout = 60
memory_size = 512
role = aws_iam_role.schafkopf_scheduler.arn
environment {
variables = {
GMAIL_SENDER_ADDRESS = jsondecode(data.aws_secretsmanager_secret_version.gmail_credentials.secret_string)["GMAIL_SENDER_ADDRESS"]
GMAIL_SENDER_PASSWORD = jsondecode(data.aws_secretsmanager_secret_version.gmail_credentials.secret_string)["GMAIL_SENDER_PASSWORD"]
}
}
}*/

resource "aws_iam_role" "schafkopf_scheduler" {
name = "schafkopf_scheduler"
assume_role_policy = jsonencode({
"Version" : "2012-10-17",
"Statement" : [
{
"Action" : "sts:AssumeRole",
"Principal" : {
"Service" : [
"lambda.amazonaws.com"
]
},
"Effect" : "Allow",
}
]
})
}
4 changes: 4 additions & 0 deletions infrastructure/locals.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
locals {
account_id = data.aws_caller_identity.current.account_id
region = data.aws_region.current.name
}
23 changes: 23 additions & 0 deletions infrastructure/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
version = "5.73.0"
}
}
/*backend "s3" {
bucket = "schafkop-scheduler-tf-state-082113759242"
dynamodb_table = "schafkopf-tf-state-lock"
key = "schafkopf-scheduler.tfstate"
region = "eu-central-1"
}*/
}

provider "aws" {
region = "eu-central-1"
default_tags {
tags = {
application = "Schafkopf Scheduler"
}
}
}
Loading

0 comments on commit 5a8c9f4

Please sign in to comment.