Skip to content

Commit 5c97832

Browse files
committed
add terraform lambda layers sample
1 parent 7b9f021 commit 5c97832

File tree

5 files changed

+167
-0
lines changed

5 files changed

+167
-0
lines changed

lambda-layers/terraform/.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
.terraform
2+
.venv
3+
build
4+
.terraform.lock.hcl
5+
output.json
6+
terraform.*

lambda-layers/terraform/Makefile

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
export AWS_ACCESS_KEY_ID ?= test
2+
export AWS_SECRET_ACCESS_KEY ?= test
3+
export AWS_DEFAULT_REGION = us-east-1
4+
SHELL := /bin/bash
5+
PYTHON_BIN ?= $(shell which python3 || which python)
6+
7+
usage: ## Show this help
8+
@fgrep -h "##" $(MAKEFILE_LIST) | fgrep -v fgrep | sed -e 's/\\$$//' | sed -e 's/##//'
9+
10+
start:
11+
localstack start -d
12+
13+
install: ## Install dependencies
14+
@which localstack || pip install localstack
15+
@which awslocal || pip install awscli-local
16+
@which terraform || (\
17+
echo 'Terraform was not found, installing locally' && \
18+
wget https://releases.hashicorp.com/terraform/1.0.8/terraform_1.0.8_linux_amd64.zip && \
19+
unzip terraform_*.zip && \
20+
rm terraform_*.zip)
21+
@test -e .venv || ($(PYTHON_BIN) -m venv .venv; source .venv/bin/activate; pip3 install terraform-local;)
22+
23+
package:
24+
source .venv/bin/activate; pip install \
25+
--platform manylinux2014_x86_64 \
26+
--target=package \
27+
--implementation cp \
28+
--only-binary=:all: \
29+
--upgrade \
30+
--requirement requirements.txt \
31+
--target build/my-lambda-layer/python
32+
33+
init:
34+
tflocal init
35+
36+
deploy:
37+
tflocal apply --auto-approve
38+
39+
invoke:
40+
awslocal lambda invoke \
41+
--function-name my-lambda-function \
42+
output.json
43+
cat output.json
44+
45+
run: start install init deploy invoke
46+
47+
clean:
48+
rm -rf build
49+
rm -rf .terraform
50+
rf -f output.json
51+
52+
.PHONY: start install init deploy invoke clean

lambda-layers/terraform/main.tf

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.0"
6+
}
7+
}
8+
}
9+
10+
provider "aws" {
11+
region = "us-east-1" # Replace with your preferred region
12+
}
13+
14+
# Lambda Layer
15+
data "archive_file" "lambda_layer_zip" {
16+
type = "zip"
17+
source_dir = "${path.module}/build/my-lambda-layer" # Path to your Python dependencies directory
18+
output_path = "${path.module}/build/lambda_layer.zip"
19+
}
20+
21+
# Layer bucket
22+
resource "aws_s3_bucket" "lambda_layer_bucket" {
23+
bucket = "my-lambda-layer-bucket"
24+
}
25+
26+
# Layer ZIP upload
27+
resource "aws_s3_object" "lambda_layer" {
28+
bucket = aws_s3_bucket.lambda_layer_bucket.id
29+
key = "lambda_layer.zip"
30+
source = data.archive_file.lambda_layer_zip.output_path
31+
depends_on = [data.archive_file.lambda_layer_zip] # Triggered only if the zip file is created
32+
}
33+
34+
# Lambda Layer from S3
35+
resource "aws_lambda_layer_version" "dependencies" {
36+
s3_bucket = aws_s3_bucket.lambda_layer_bucket.id
37+
s3_key = aws_s3_object.lambda_layer.key
38+
layer_name = "my-lambda-layer"
39+
compatible_runtimes = ["python3.12"]
40+
depends_on = [aws_s3_object.lambda_layer] # Triggered only if the zip file is uploaded to the bucket
41+
}
42+
43+
# Lambda Function
44+
data "archive_file" "lambda_function" {
45+
type = "zip"
46+
source_file = "${path.module}/src/lambda_function.py"
47+
output_path = "${path.module}/build/lambda_function.zip"
48+
}
49+
50+
resource "aws_lambda_function" "my_lambda" {
51+
filename = data.archive_file.lambda_function.output_path
52+
function_name = "my-lambda-function"
53+
role = aws_iam_role.lambda_role.arn # See IAM Role below
54+
handler = "lambda_function.handler"
55+
runtime = "python3.12"
56+
57+
layers = [aws_lambda_layer_version.dependencies.arn]
58+
}
59+
60+
# IAM Role for Lambda
61+
resource "aws_iam_role" "lambda_role" {
62+
name = "lambda_basic_execution"
63+
64+
assume_role_policy = <<EOF
65+
{
66+
"Version": "2012-10-17",
67+
"Statement": [
68+
{
69+
"Action": "sts:AssumeRole",
70+
"Principal": {
71+
"Service": "lambda.amazonaws.com"
72+
},
73+
"Effect": "Allow"
74+
}
75+
]
76+
}
77+
EOF
78+
}
79+
80+
resource "aws_iam_role_policy" "lambda_logs_policy" {
81+
name = "lambda_logs_policy"
82+
role = aws_iam_role.lambda_role.id
83+
84+
policy = <<EOF
85+
{
86+
"Version": "2012-10-17",
87+
"Statement": [
88+
{
89+
"Action": [
90+
"logs:CreateLogGroup",
91+
"logs:CreateLogStream",
92+
"logs:PutLogEvents"
93+
],
94+
"Resource": "arn:aws:logs:*:*:*",
95+
"Effect": "Allow"
96+
}
97+
]
98+
}
99+
EOF
100+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PyYAML==6.0.1
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import yaml
2+
3+
def handler(event, context):
4+
status_yaml = """
5+
status: success
6+
"""
7+
status = yaml.safe_load(status_yaml)
8+
return status

0 commit comments

Comments
 (0)