Skip to content

Commit

Permalink
🛠️ Replace layer and zips with docker images.
Browse files Browse the repository at this point in the history
  • Loading branch information
asaf-kali committed Oct 29, 2024
1 parent 5f66fc8 commit e1c5762
Show file tree
Hide file tree
Showing 10 changed files with 153 additions and 100 deletions.
39 changes: 19 additions & 20 deletions .github/workflows/pipeline.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,25 +94,25 @@ jobs:
# Deploy

build-deployment:
runs-on: ubuntu-latest
steps:
- name: Checkout repo
uses: actions/checkout@v4
- name: Set up python 3.11
uses: actions/setup-python@v4
with:
python-version: "3.11"
- name: Install dependencies
run: |
make install-ci
- name: Build Lambda Layer
run: make build-layer
- name: Upload deployment folder artifact
uses: actions/upload-artifact@v4
with:
name: deployment-${{ github.sha }}
path: ./.deployment
# build-deployment:
# runs-on: ubuntu-latest
# steps:
# - name: Checkout repo
# uses: actions/checkout@v4
# - name: Set up python 3.11
# uses: actions/setup-python@v4
# with:
# python-version: "3.11"
# - name: Install dependencies
# run: |
# make install-ci
# - name: Build Lambda Layer
# run: make build-layer
# - name: Upload deployment folder artifact
# uses: actions/upload-artifact@v4
# with:
# name: deployment-${{ github.sha }}
# path: ./.deployment

deploy-dev:
runs-on: ubuntu-latest
Expand All @@ -123,7 +123,6 @@ jobs:
- check-mypy
- check-poetry-lock
- unit-tests
- build-deployment
steps:
- name: Checkout repo
uses: actions/checkout@v4
Expand Down
11 changes: 5 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,9 +84,6 @@ kill:

# Deploy

build-layer:
./scripts/build_layer.sh

update:
cd tf; make update;

Expand All @@ -101,8 +98,10 @@ deploy:

# Quick and dirty

wip:
make format
wip: format
git add .
git commit -m "Auto commit" --no-verify
git push

amend: format
git add .
git commit --amend --no-edit --no-verify
10 changes: 0 additions & 10 deletions scripts/build_layer.sh

This file was deleted.

2 changes: 0 additions & 2 deletions tf/.gitignore

This file was deleted.

26 changes: 13 additions & 13 deletions tf/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

18 changes: 9 additions & 9 deletions tf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,25 @@ TERRAFORM_PLAN_FILE=deploy.tfplan
WORKSPACE=default
AWS_REGION=us-east-1

build-layer:
cd ../; make build-layer

update:
terraform get -update

upgrade:
terraform init -upgrade

refresh:
terraform workspace select $(WORKSPACE)
terraform refresh \
-var-file="$(WORKSPACE).tfvars" \

plan:
terraform workspace select $(WORKSPACE)
terraform plan \
-out $(TERRAFORM_PLAN_FILE) \
-var-file="$(WORKSPACE).tfvars" \
-var="aws_region=$(AWS_REGION)"

refresh:
terraform workspace select $(WORKSPACE)
terraform refresh \
-var-file="$(WORKSPACE).tfvars" \

apply:
terraform apply $(TERRAFORM_PLAN_FILE)

deploy: build-layer plan apply
deploy: plan apply
16 changes: 16 additions & 0 deletions tf/app.Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
ARG SRC_IMAGE
ARG SRC_TAG

FROM ${SRC_IMAGE}:${SRC_TAG}

WORKDIR /tmp/build
# Copy dependencies
COPY requirements.lock .
# Install dependencies
RUN pip install --no-deps --target ${LAMBDA_TASK_ROOT} -r requirements.lock

WORKDIR ${LAMBDA_TASK_ROOT}
# Copy source code
COPY src/ .
# Point to lambda handler
CMD [ "lambda_handler.handle"]
83 changes: 83 additions & 0 deletions tf/images.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# Vars and data

locals {
lock_file = "${local.project_root}/poetry.lock"
lock_file_sha = filebase64sha256(local.lock_file)
app_dockerfile = "${local.tf_root}/app.Dockerfile"
}

# ECR Repo

resource "aws_ecr_repository" "ecr_repo" {
name = "${local.service_name}-ecr"
}

resource "aws_ecr_lifecycle_policy" "ecr_lifecycle_policy" {
repository = aws_ecr_repository.ecr_repo.name
policy = <<EOF
{
"rules": [
{
"rulePriority": 1,
"description": "Keep last 3 images",
"selection": {
"tagStatus": "any",
"countType": "imageCountMoreThan",
"countNumber": 3
},
"action": {
"type": "expire"
}
}
]
}
EOF
}

# Prepare

resource "null_resource" "lock_export" {
triggers = {
lock_file = local.lock_file_sha
}

provisioner "local-exec" {
command = <<EOF
cd ${local.project_root}; make lock-export || exit 1
EOF
}
}

module "app_archive" {
source = "github.com/asaf-kali/resources//tf/filtered_archive"
name = "service"
source_dir = local.lambda_src_root
exclude_patterns = [
".coverage",
"**/__pycache__/**",
"**/.pytest_cache/**",
]
}

# Image

module "app_image" {
name = "app"
source = "github.com/asaf-kali/resources//tf/ecr_builder"
aws_account_id = local.aws_account_id
aws_region = var.aws_region
build_dir = local.project_root
docker_file = local.app_dockerfile
ecr_name = aws_ecr_repository.ecr_repo.name
ecr_url = aws_ecr_repository.ecr_repo.repository_url
src_image = "public.ecr.aws/lambda/python"
src_tag = local.python_version
triggers = {
docker_file = filebase64sha256(local.app_dockerfile)
lock_file = local.lock_file_sha
source_dir = module.app_archive.output_sha
}
depends_on = [
null_resource.lock_export
]
}
4 changes: 2 additions & 2 deletions tf/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,11 @@ locals {
# Base
project_name = "the-spymaster-bot"
service_name = "${local.project_name}-${local.env}"
aws_account_id = data.aws_caller_identity.current.account_id
aws_account_id = data.aws_caller_identity.current.account_id
python_version = "3.11"
# Paths
tf_root = abspath(path.module)
project_root = abspath("${path.module}/../")
layer_src_root = "${local.project_root}/.deployment/layer-dependencies"
lambda_src_root = "${local.project_root}/src/"
# Domain
base_app_domain = "303707.xyz"
Expand Down
44 changes: 6 additions & 38 deletions tf/service.tf
Original file line number Diff line number Diff line change
@@ -1,47 +1,15 @@
# Layer

module "layer_archive" {
source = "github.com/asaf-kali/resources//tf/filtered_archive"
source_dir = local.layer_src_root
name = "layer"
}

output "layer_archive_hash" {
value = filebase64sha256(module.layer_archive.output_path)
}

resource "aws_lambda_layer_version" "dependencies_layer" {
layer_name = "${local.service_name}-layer"
filename = module.layer_archive.output_path
source_code_hash = filebase64sha256(module.layer_archive.output_path)
skip_destroy = true
}

# Lambda

module "lambda_archive" {
source = "github.com/asaf-kali/resources//tf/filtered_archive"
source_dir = local.lambda_src_root
name = "service"
exclude_patterns = [
".coverage",
"**/__pycache__/**",
"**/.pytest_cache/**",
]
}

resource "aws_lambda_function" "service_lambda" {
function_name = "${local.service_name}-lambda"
role = aws_iam_role.lambda_exec_role.arn
handler = "lambda_handler.handle"
runtime = "python3.11"
filename = module.lambda_archive.output_path
source_code_hash = filebase64sha256(module.lambda_archive.output_path)
timeout = 30
image_uri = "${aws_ecr_repository.ecr_repo.repository_url}@${module.app_image.id}"
package_type = "Image"
memory_size = 200
reserved_concurrent_executions = 2
layers = [
aws_lambda_layer_version.dependencies_layer.arn
depends_on = [
module.app_image
]
environment {
variables = {
Expand All @@ -57,7 +25,7 @@ data "aws_iam_policy_document" "lambda_assume_policy_doc" {
actions = ["sts:AssumeRole"]

principals {
type = "Service"
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
Expand All @@ -67,7 +35,7 @@ resource "aws_iam_role" "lambda_exec_role" {
name = "${local.service_name}-lambda-exec-role"
assume_role_policy = data.aws_iam_policy_document.lambda_assume_policy_doc.json
inline_policy {
name = "${local.service_name}-lambda-exec-role-policy"
name = "${local.service_name}-lambda-exec-role-policy"
policy = jsonencode(
{
"Version" : "2012-10-17",
Expand Down

0 comments on commit e1c5762

Please sign in to comment.