terraform { required_providers { aws = { source = "hashicorp/aws" version = "~> 5.0" } } } provider "aws" { region = var.region } data "aws_iam_policy_document" "assume_role" { statement { effect = "Allow" principals { type = "Service" identifiers = ["lambda.amazonaws.com"] } actions = ["sts:AssumeRole"] } } resource "null_resource" "pip_install" { triggers = { shell_hash = "${sha256(file("${path.module}/pyproject.toml"))}" } provisioner "local-exec" { command = "uv pip compile ${path.module}/pyproject.toml --python 3.11 --no-header -o /tmp/layer-requirements.txt && uv pip install -r /tmp/layer-requirements.txt --python 3.11 -t ${path.module}/layer/python" } } data "archive_file" "layer" { type = "zip" source_dir = "${path.module}/layer" output_path = "${path.module}/layer.zip" depends_on = [null_resource.pip_install] } resource "aws_lambda_layer_version" "layer" { layer_name = "openai-layer-${local.env}" filename = data.archive_file.layer.output_path source_code_hash = data.archive_file.layer.output_base64sha256 compatible_runtimes = ["python3.11"] } resource "aws_iam_role" "iam_for_lambda" { name = "glimpse2-${local.env}" assume_role_policy = data.aws_iam_policy_document.assume_role.json } data "archive_file" "lambda" { type = "zip" source_dir = "./code" output_path = "lambda_function_payload.zip" } resource "aws_lambda_function" "test_lambda" { # If the file is not in the current working directory you will need to include a # path.module in the filename. filename = "lambda_function_payload.zip" function_name = "glimpse2-${local.env}" role = aws_iam_role.iam_for_lambda.arn handler = "main.handler" memory_size = 512 timeout = 60 source_code_hash = data.archive_file.lambda.output_base64sha256 layers = [aws_lambda_layer_version.layer.arn] runtime = "python3.11" environment { variables = { environment = local.env } } }