-
-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
428 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
############################################################################### | ||
# REST API resources - Search | ||
# This is an HTTP Get request, to retrieve the configuration settings for the | ||
# facial recognition system. | ||
# | ||
# workflow is: 1. method request (from Postman, curl, your application, etc.) | ||
# 2. integration request (we're integrating to an AWS Lambda function) | ||
# 3. integration response (the results from the Lambda function) | ||
# 4. method response (hopefully, an http 200 response) | ||
# | ||
############################################################################### | ||
resource "aws_api_gateway_resource" "info" { | ||
path_part = "info" | ||
parent_id = aws_api_gateway_rest_api.openai.root_resource_id | ||
rest_api_id = aws_api_gateway_rest_api.openai.id | ||
} | ||
resource "aws_api_gateway_method" "info" { | ||
rest_api_id = aws_api_gateway_rest_api.openai.id | ||
resource_id = aws_api_gateway_resource.info.id | ||
http_method = "ANY" | ||
authorization = "NONE" | ||
api_key_required = "true" | ||
} | ||
|
||
resource "aws_api_gateway_integration" "info" { | ||
rest_api_id = aws_api_gateway_rest_api.openai.id | ||
resource_id = aws_api_gateway_resource.info.id | ||
http_method = aws_api_gateway_method.info.http_method | ||
integration_http_method = "POST" | ||
type = "AWS_PROXY" | ||
uri = aws_lambda_function.info.invoke_arn | ||
} | ||
|
||
resource "aws_lambda_permission" "info" { | ||
statement_id = "AllowExecutionFromAPIGateway" | ||
action = "lambda:InvokeFunction" | ||
function_name = aws_lambda_function.info.function_name | ||
principal = "apigateway.amazonaws.com" | ||
|
||
# More: http://docs.aws.amazon.com/apigateway/latest/developerguide/api-gateway-control-access-using-iam-policies-to-invoke-api.html | ||
source_arn = "arn:aws:execute-api:${var.aws_region}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.openai.id}/*/${aws_api_gateway_method.info.http_method}${aws_api_gateway_resource.info.path}" | ||
|
||
} | ||
|
||
resource "aws_api_gateway_method_response" "info_response_200" { | ||
rest_api_id = aws_api_gateway_rest_api.openai.id | ||
resource_id = aws_api_gateway_resource.info.id | ||
http_method = aws_api_gateway_method.info.http_method | ||
status_code = "200" | ||
response_models = { | ||
"application/json" = "Empty" | ||
} | ||
response_parameters = {} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
#------------------------------------------------------------------------------ | ||
# written by: Lawrence McDaniel | ||
# https://lawrencemcdaniel.com/ | ||
# | ||
# date: dec-2023 | ||
# | ||
# usage: - implement a Python Lambda function to create a dump of the | ||
# configuration settings for the facial recognition system. | ||
#------------------------------------------------------------------------------ | ||
locals { | ||
info_function_name = "lambda_${var.shared_resource_identifier}_info" | ||
|
||
info_build_path = "${path.module}/build/distribution_package" | ||
info_source_directory = "${path.module}/python/openai_api" | ||
info_packaging_script = "${local.info_source_directory}/create_pkg.sh" | ||
info_dist_package_name = "${local.info_function_name}_dist_pkg.zip" | ||
|
||
} | ||
|
||
|
||
############################################################################### | ||
# Cloudwatch logging | ||
############################################################################### | ||
resource "aws_cloudwatch_log_group" "info" { | ||
name = "/aws/lambda/${local.info_function_name}" | ||
retention_in_days = var.log_retention_days | ||
tags = var.tags | ||
} | ||
|
||
|
||
############################################################################### | ||
# Python package | ||
# https://alek-cora-glez.medium.com/deploying-aws-lambda-function-with-terraform-custom-dependencies-7874407cd4fc | ||
############################################################################### | ||
resource "null_resource" "package_lambda_info" { | ||
triggers = { | ||
always_redeploy = timestamp() | ||
} | ||
|
||
provisioner "local-exec" { | ||
interpreter = ["/bin/bash"] | ||
command = local.info_packaging_script | ||
|
||
environment = { | ||
TERRAFORM_ROOT = path.module | ||
SOURCE_CODE_PATH = local.info_source_directory | ||
BUILD_PATH = local.info_build_path | ||
PACKAGE_FOLDER = local.info_function_name | ||
} | ||
} | ||
} | ||
|
||
# see https://registry.terraform.io/providers/hashicorp/archive/latest/docs/data-sources/file | ||
data "archive_file" "lambda_info" { | ||
source_dir = local.info_build_path | ||
output_path = "${path.module}/build/${local.info_dist_package_name}" | ||
type = "zip" | ||
depends_on = [null_resource.package_lambda_info] | ||
|
||
} | ||
|
||
resource "aws_lambda_function" "info" { | ||
|
||
# see https://docs.aws.amazon.com/lambda/latest/dg/lambda-runtimes.html | ||
function_name = local.info_function_name | ||
description = "OpenAI API configuration settings. invoked by API Gateway." | ||
role = aws_iam_role.lambda.arn | ||
publish = true | ||
runtime = var.lambda_python_runtime | ||
memory_size = var.lambda_memory_size | ||
timeout = var.lambda_timeout | ||
handler = "openai_api.lambda_info.lambda_handler.handler" | ||
filename = data.archive_file.lambda_info.output_path | ||
source_code_hash = data.archive_file.lambda_info.output_base64sha256 | ||
layers = [aws_lambda_layer_version.genai.arn] | ||
tags = var.tags | ||
|
||
environment { | ||
variables = { | ||
DEBUG_MODE = var.debug_mode | ||
AWS_DEPLOYED = true | ||
} | ||
} | ||
} |
Oops, something went wrong.