|
| 1 | +provider "aws" { |
| 2 | + region = "us-east-1" |
| 3 | +} |
| 4 | + |
| 5 | +variable "prefix" { |
| 6 | + description = "Prefix to associate with the resources" |
| 7 | + type = string |
| 8 | +} |
| 9 | + |
| 10 | +resource "random_string" "suffix" { |
| 11 | + length = 8 |
| 12 | + special = false |
| 13 | + upper = false |
| 14 | +} |
| 15 | + |
| 16 | +# Create Lambda layer for Pillow from provided zip file |
| 17 | +resource "aws_lambda_layer_version" "pillow_layer" { |
| 18 | + filename = "pillow.zip" # Make sure this zip file exists in your terraform directory |
| 19 | + layer_name = "pillow_layer" |
| 20 | + compatible_runtimes = ["python3.11"] |
| 21 | + description = "Pillow library layer for image processing" |
| 22 | +} |
| 23 | + |
| 24 | +# IAM Policy for invoking Bedrock model |
| 25 | +resource "aws_iam_policy" "invoke_model_policy" { |
| 26 | + name = "${lower(var.prefix)}-InvokeModelPolicy-${random_string.suffix.result}" |
| 27 | + path = "/" |
| 28 | + description = "Policy to invoke Bedrock model" |
| 29 | + |
| 30 | + policy = jsonencode({ |
| 31 | + Version = "2012-10-17" |
| 32 | + Statement = [ |
| 33 | + { |
| 34 | + Action = [ |
| 35 | + "bedrock:InvokeModel", |
| 36 | + ] |
| 37 | + Effect = "Allow" |
| 38 | + Resource = [ |
| 39 | + "arn:aws:bedrock:${data.aws_region.current.name}::foundation-model/amazon.nova-canvas-v1:0" |
| 40 | + ] |
| 41 | + }, |
| 42 | + ] |
| 43 | + }) |
| 44 | +} |
| 45 | + |
| 46 | +# S3 bucket for storing images |
| 47 | +resource "aws_s3_bucket" "image_bucket" { |
| 48 | + bucket = "${lower(var.prefix)}-image-bucket-${random_string.suffix.result}" |
| 49 | + force_destroy = true |
| 50 | +} |
| 51 | + |
| 52 | +# Create CloudWatch Log Group for Lambda |
| 53 | +resource "aws_cloudwatch_log_group" "lambda_log_group" { |
| 54 | + name = "/aws/lambda/${lower(var.prefix)}-invoke-bedrock" |
| 55 | + retention_in_days = 14 |
| 56 | + |
| 57 | + lifecycle { |
| 58 | + prevent_destroy = false |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +# Lambda function |
| 63 | +resource "aws_lambda_function" "invoke_bedrock_function" { |
| 64 | + filename = "index.zip" # Replace with your Lambda code zip file |
| 65 | + function_name = "${lower(var.prefix)}-invoke-bedrock" |
| 66 | + role = aws_iam_role.lambda_role.arn |
| 67 | + handler = "index.handler" |
| 68 | + runtime = "python3.11" |
| 69 | + timeout = 30 |
| 70 | + |
| 71 | + layers = [ |
| 72 | + aws_lambda_layer_version.pillow_layer.arn |
| 73 | + ] |
| 74 | + |
| 75 | + environment { |
| 76 | + variables = { |
| 77 | + BUCKET = aws_s3_bucket.image_bucket.id |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + depends_on = [aws_cloudwatch_log_group.lambda_log_group] |
| 82 | +} |
| 83 | + |
| 84 | +# IAM role for Lambda function |
| 85 | +resource "aws_iam_role" "lambda_role" { |
| 86 | + name = "${lower(var.prefix)}-lambda_role-${random_string.suffix.result}" |
| 87 | + |
| 88 | + assume_role_policy = jsonencode({ |
| 89 | + Version = "2012-10-17" |
| 90 | + Statement = [ |
| 91 | + { |
| 92 | + Action = "sts:AssumeRole" |
| 93 | + Effect = "Allow" |
| 94 | + Principal = { |
| 95 | + Service = "lambda.amazonaws.com" |
| 96 | + } |
| 97 | + } |
| 98 | + ] |
| 99 | + }) |
| 100 | +} |
| 101 | + |
| 102 | +# Attach policies to Lambda role |
| 103 | +resource "aws_iam_role_policy_attachment" "lambda_invoke_model_policy" { |
| 104 | + policy_arn = aws_iam_policy.invoke_model_policy.arn |
| 105 | + role = aws_iam_role.lambda_role.name |
| 106 | +} |
| 107 | + |
| 108 | +resource "aws_iam_role_policy_attachment" "lambda_basic_execution" { |
| 109 | + policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole" |
| 110 | + role = aws_iam_role.lambda_role.name |
| 111 | +} |
| 112 | + |
| 113 | +resource "aws_iam_role_policy_attachment" "lambda_s3_access" { |
| 114 | + policy_arn = "arn:aws:iam::aws:policy/AmazonS3FullAccess" |
| 115 | + role = aws_iam_role.lambda_role.name |
| 116 | +} |
| 117 | + |
| 118 | +# API Gateway |
| 119 | +resource "aws_api_gateway_rest_api" "bedrock_api" { |
| 120 | + name = "${lower(var.prefix)}-BedrockImageAPI-${random_string.suffix.result}" |
| 121 | +} |
| 122 | + |
| 123 | +resource "aws_api_gateway_resource" "image_gen" { |
| 124 | + rest_api_id = aws_api_gateway_rest_api.bedrock_api.id |
| 125 | + parent_id = aws_api_gateway_rest_api.bedrock_api.root_resource_id |
| 126 | + path_part = "image_gen" |
| 127 | +} |
| 128 | + |
| 129 | +resource "aws_api_gateway_method" "image_gen_post" { |
| 130 | + rest_api_id = aws_api_gateway_rest_api.bedrock_api.id |
| 131 | + resource_id = aws_api_gateway_resource.image_gen.id |
| 132 | + http_method = "POST" |
| 133 | + authorization = "NONE" |
| 134 | +} |
| 135 | + |
| 136 | +resource "aws_api_gateway_integration" "lambda_integration" { |
| 137 | + rest_api_id = aws_api_gateway_rest_api.bedrock_api.id |
| 138 | + resource_id = aws_api_gateway_resource.image_gen.id |
| 139 | + http_method = aws_api_gateway_method.image_gen_post.http_method |
| 140 | + |
| 141 | + integration_http_method = "POST" |
| 142 | + type = "AWS_PROXY" |
| 143 | + uri = aws_lambda_function.invoke_bedrock_function.invoke_arn |
| 144 | +} |
| 145 | + |
| 146 | +# API Gateway Deployment |
| 147 | +resource "aws_api_gateway_deployment" "api_deployment" { |
| 148 | + rest_api_id = aws_api_gateway_rest_api.bedrock_api.id |
| 149 | + |
| 150 | + depends_on = [ |
| 151 | + aws_api_gateway_integration.lambda_integration |
| 152 | + ] |
| 153 | +} |
| 154 | + |
| 155 | +# API Gateway Stage |
| 156 | +resource "aws_api_gateway_stage" "api_stage" { |
| 157 | + deployment_id = aws_api_gateway_deployment.api_deployment.id |
| 158 | + rest_api_id = aws_api_gateway_rest_api.bedrock_api.id |
| 159 | + stage_name = "dev" |
| 160 | +} |
| 161 | + |
| 162 | +# Lambda permission for API Gateway |
| 163 | +resource "aws_lambda_permission" "api_gateway_lambda" { |
| 164 | + statement_id = "AllowAPIGatewayInvoke" |
| 165 | + action = "lambda:InvokeFunction" |
| 166 | + function_name = aws_lambda_function.invoke_bedrock_function.function_name |
| 167 | + principal = "apigateway.amazonaws.com" |
| 168 | + source_arn = "${aws_api_gateway_rest_api.bedrock_api.execution_arn}/*/*" |
| 169 | +} |
| 170 | + |
| 171 | +# Outputs |
| 172 | +output "lambda_function" { |
| 173 | + description = "The ARN of the Lambda function" |
| 174 | + value = aws_lambda_function.invoke_bedrock_function.arn |
| 175 | +} |
| 176 | + |
| 177 | +output "api_endpoint" { |
| 178 | + description = "The API Gateway endpoint URL " |
| 179 | + value = "${aws_api_gateway_stage.api_stage.invoke_url}/image_gen" |
| 180 | +} |
| 181 | + |
| 182 | +output "s3_image_bucket" { |
| 183 | + description = "The Output S3 bucket is " |
| 184 | + value = aws_s3_bucket.image_bucket.id |
| 185 | +} |
| 186 | + |
| 187 | +# Data source for current region |
| 188 | +data "aws_region" "current" {} |
0 commit comments