|
| 1 | +#------------------------------------------------------------------------------ |
| 2 | +# written by: Lawrence McDaniel |
| 3 | +# https://lawrencemcdaniel.com/ |
| 4 | +# |
| 5 | +# date: dec-2023 |
| 6 | +# |
| 7 | +# usage: - implement a Python Lambda function to create a dump of the |
| 8 | +# configuration settings for the facial recognition system. |
| 9 | +#------------------------------------------------------------------------------ |
| 10 | +locals { |
| 11 | + info_function_name = "lambda_${var.shared_resource_identifier}_info" |
| 12 | + |
| 13 | + info_build_path = "${path.module}/build/distribution_package" |
| 14 | + info_source_directory = "${path.module}/python/openai_api" |
| 15 | + info_packaging_script = "${local.info_source_directory}/create_pkg.sh" |
| 16 | + info_dist_package_name = "${local.info_function_name}_dist_pkg.zip" |
| 17 | + |
| 18 | +} |
| 19 | + |
| 20 | + |
| 21 | +############################################################################### |
| 22 | +# Cloudwatch logging |
| 23 | +############################################################################### |
| 24 | +resource "aws_cloudwatch_log_group" "info" { |
| 25 | + name = "/aws/lambda/${local.info_function_name}" |
| 26 | + retention_in_days = var.log_retention_days |
| 27 | + tags = var.tags |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +############################################################################### |
| 32 | +# Python package |
| 33 | +# https://alek-cora-glez.medium.com/deploying-aws-lambda-function-with-terraform-custom-dependencies-7874407cd4fc |
| 34 | +############################################################################### |
| 35 | +resource "null_resource" "package_lambda_info" { |
| 36 | + triggers = { |
| 37 | + always_redeploy = timestamp() |
| 38 | + } |
| 39 | + |
| 40 | + provisioner "local-exec" { |
| 41 | + interpreter = ["/bin/bash"] |
| 42 | + command = local.info_packaging_script |
| 43 | + |
| 44 | + environment = { |
| 45 | + TERRAFORM_ROOT = path.module |
| 46 | + SOURCE_CODE_PATH = local.info_source_directory |
| 47 | + BUILD_PATH = local.info_build_path |
| 48 | + PACKAGE_FOLDER = local.info_function_name |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +# see https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file |
| 54 | +data "archive_file" "lambda_info" { |
| 55 | + source_dir = local.info_build_path |
| 56 | + output_path = "${path.module}/build/${local.info_dist_package_name}" |
| 57 | + type = "zip" |
| 58 | + depends_on = [null_resource.package_lambda_info] |
| 59 | + |
| 60 | +} |
| 61 | + |
| 62 | +resource "aws_lambda_function" "info" { |
| 63 | + |
| 64 | + # see https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html |
| 65 | + function_name = local.info_function_name |
| 66 | + description = "OpenAI API configuration settings. invoked by API Gateway." |
| 67 | + role = aws_iam_role.lambda.arn |
| 68 | + publish = true |
| 69 | + runtime = var.lambda_python_runtime |
| 70 | + memory_size = var.lambda_memory_size |
| 71 | + timeout = var.lambda_timeout |
| 72 | + handler = "openai_api.lambda_info.lambda_handler.handler" |
| 73 | + filename = data.archive_file.lambda_info.output_path |
| 74 | + source_code_hash = data.archive_file.lambda_info.output_base64sha256 |
| 75 | + layers = [aws_lambda_layer_version.genai.arn] |
| 76 | + tags = var.tags |
| 77 | + |
| 78 | + environment { |
| 79 | + variables = { |
| 80 | + DEBUG_MODE = var.debug_mode |
| 81 | + AWS_DEPLOYED = true |
| 82 | + } |
| 83 | + } |
| 84 | +} |
0 commit comments