Skip to content

Commit

Permalink
feat: add /info end point
Browse files Browse the repository at this point in the history
  • Loading branch information
lpm0073 committed Dec 30, 2023
1 parent de1651d commit bc7cdcd
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 9 deletions.
3 changes: 1 addition & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
## [0.8.2](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.8.1...v0.8.2) (2023-12-30)


### Bug Fixes

* refactor request templates for openai api v1 object_type ([c857082](https://github.com/FullStackWithLawrence/aws-openai/commit/c8570826c75a9f99f465ffa3ebd470795ffb70d3))
- refactor request templates for openai api v1 object_type ([c857082](https://github.com/FullStackWithLawrence/aws-openai/commit/c8570826c75a9f99f465ffa3ebd470795ffb70d3))

## [0.8.1](https://github.com/FullStackWithLawrence/aws-openai/compare/v0.8.0...v0.8.1) (2023-12-30)

Expand Down
54 changes: 54 additions & 0 deletions api/terraform/api_gateway_info.tf
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 = {}
}
8 changes: 5 additions & 3 deletions api/terraform/json/iam_policy_lambda.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,16 @@
"Effect": "Allow",
"Action": [
"apigateway:GET",
"ec2:DescribeRegions",
"iam:ListPolicies",
"iam:ListRoles",
"iam:GetPolicy",
"iam:GetPolicyVersion",
"iam:ListAttachedRolePolicies",
"s3:ListAllMyBuckets",
"ec2:DescribeRegions",
"lambda:ListFunctions",
"route53:ListHostedZones",
"route53:ListResourceRecordSets"
"route53:ListResourceRecordSets",
"s3:ListAllMyBuckets"
],
"Resource": "*"
}
Expand Down
84 changes: 84 additions & 0 deletions api/terraform/lambda_info.tf
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
}
}
}
Loading

0 comments on commit bc7cdcd

Please sign in to comment.