Skip to content

Commit 4bee0e0

Browse files
committed
bash Script
1 parent ef72b80 commit 4bee0e0

3 files changed

Lines changed: 72 additions & 3 deletions

File tree

redis_sync/package_lambda.sh

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
#!/bin/bash
2+
set -e
3+
4+
PROJECT_DIR="${1:-.}" # Default to current dir if not provided
5+
6+
echo "🚀 Packaging Lambda from $PROJECT_DIR..."
7+
8+
# Clean previous build
9+
rm -rf build lambda_package.zip
10+
mkdir -p build
11+
12+
# Export dependencies (using poetry) and install them
13+
poetry export -f requirements.txt --without-hashes -o requirements.txt
14+
pip install -r requirements.txt -t build/
15+
16+
# Copy only the needed source code and files
17+
cp -r "$PROJECT_DIR"/* build/
18+
cp "$PROJECT_DIR"/pyproject.toml "$PROJECT_DIR"/poetry.lock build/
19+
20+
# Create deployment zip
21+
cd build
22+
zip -r ../lambda_package.zip .
23+
cd ..
24+
25+
echo "✅ Lambda package created: lambda_package.zip"

terraform/package_lambda.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/bin/bash
2+
set -e
3+
4+
echo "🚀 Packaging Lambda..."
5+
6+
# Clean previous build
7+
rm -rf build lambda_package.zip
8+
mkdir -p build
9+
10+
# Export dependencies (using poetry) and install them
11+
poetry export -f requirements.txt --without-hashes -o requirements.txt
12+
pip install -r requirements.txt -t build/
13+
14+
# Copy only the needed source code and files
15+
cp -r src/* build/
16+
cp pyproject.toml poetry.lock build/
17+
18+
# Create deployment zip
19+
cd build
20+
zip -r ../lambda_package.zip .
21+
cd ..
22+
23+
echo "✅ Lambda package created: lambda_package.zip"

terraform/redis_sync_lambda.tf

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Define the directory containing source code and calculate its SHA-256 hash for triggering redeployments
22
locals {
3-
redis_sync_dir = abspath("${path.root}/../redis_sync/src")
3+
redis_sync_dir = abspath("${path.root}/../redis_sync")
44
redis_sync_files = fileset(local.redis_sync_dir, "**")
55
redis_sync_dir_sha = sha1(join("", [for f in local.redis_sync_files : filesha1("${local.redis_sync_dir}/${f}")]))
66
}
@@ -13,16 +13,37 @@ output "redis_sync_files" {
1313
value = "redis_sync_files: ${join(", ", local.redis_sync_files)}"
1414
}
1515

16+
# data "archive_file" "redis_sync_lambda_zip" {
17+
# type = "zip"
18+
# source_dir = local.redis_sync_dir
19+
# output_path = "${path.module}/build/redis_sync_lambda.zip"
20+
# excludes = ["test/*", "*.zip", "build/*", "venv/*"]
21+
# }
22+
23+
resource "null_resource" "package_lambda" {
24+
provisioner "local-exec" {
25+
command = "${path.module}/package_lambda.sh ${local.redis_sync_dir}"
26+
}
27+
28+
triggers = {
29+
src_hash = sha1(join("", fileset(local.redis_sync_dir, "**")))
30+
toml_hash = filesha1("${local.redis_sync_dir}/pyproject.toml")
31+
lock_hash = filesha1("${local.redis_sync_dir}/poetry.lock")
32+
}
33+
}
34+
1635
data "archive_file" "redis_sync_lambda_zip" {
1736
type = "zip"
18-
source_dir = local.redis_sync_dir
37+
source_dir = "${path.module}/build"
1938
output_path = "${path.module}/build/redis_sync_lambda.zip"
39+
40+
depends_on = [null_resource.package_lambda]
2041
}
2142

2243
resource "aws_lambda_function" "redis_sync_lambda" {
2344
function_name = "${local.short_prefix}-redis-sync-lambda"
2445
role = aws_iam_role.redis_sync_lambda_exec_role.arn
25-
handler = "redis_sync.sync_handler" # Update as appropriate
46+
handler = "src/redis_sync.sync_handler" # Update as appropriate
2647
runtime = "python3.11"
2748
filename = data.archive_file.redis_sync_lambda_zip.output_path
2849
source_code_hash = data.archive_file.redis_sync_lambda_zip.output_base64sha256

0 commit comments

Comments
 (0)