diff --git a/apigw/apigw-hello-world-lambda/apigw.tf b/apigw/get-s3-files/apigw.tf similarity index 100% rename from apigw/apigw-hello-world-lambda/apigw.tf rename to apigw/get-s3-files/apigw.tf diff --git a/apigw/apigw-hello-world-lambda/data.tf b/apigw/get-s3-files/data.tf similarity index 100% rename from apigw/apigw-hello-world-lambda/data.tf rename to apigw/get-s3-files/data.tf diff --git a/apigw/apigw-hello-world-lambda/lambda.tf b/apigw/get-s3-files/lambda.tf similarity index 100% rename from apigw/apigw-hello-world-lambda/lambda.tf rename to apigw/get-s3-files/lambda.tf diff --git a/apigw/apigw-hello-world-lambda/lambda/main.py b/apigw/get-s3-files/lambda/main.py similarity index 100% rename from apigw/apigw-hello-world-lambda/lambda/main.py rename to apigw/get-s3-files/lambda/main.py diff --git a/apigw/apigw-hello-world-lambda/main.tf b/apigw/get-s3-files/main.tf similarity index 100% rename from apigw/apigw-hello-world-lambda/main.tf rename to apigw/get-s3-files/main.tf diff --git a/apigw/apigw-hello-world-lambda/provider.tf b/apigw/get-s3-files/provider.tf similarity index 100% rename from apigw/apigw-hello-world-lambda/provider.tf rename to apigw/get-s3-files/provider.tf diff --git a/apigw/apigw-hello-world-lambda/variables.tf b/apigw/get-s3-files/variables.tf similarity index 100% rename from apigw/apigw-hello-world-lambda/variables.tf rename to apigw/get-s3-files/variables.tf diff --git a/apigw/hello-world/README.md b/apigw/hello-world/README.md new file mode 100644 index 0000000..74c0f88 --- /dev/null +++ b/apigw/hello-world/README.md @@ -0,0 +1 @@ +# API Gateway to trigger Hello World Lambda Function diff --git a/apigw/hello-world/apigw.tf b/apigw/hello-world/apigw.tf new file mode 100644 index 0000000..a9a9102 --- /dev/null +++ b/apigw/hello-world/apigw.tf @@ -0,0 +1,66 @@ +resource "aws_api_gateway_rest_api" "hello_world" { + name = "basic-invoke-lambda-function" + description = "API Gateway for Hello World Lambda" + +} + + +resource "aws_api_gateway_resource" "hello_world" { + rest_api_id = aws_api_gateway_rest_api.hello_world.id + parent_id = aws_api_gateway_rest_api.hello_world.root_resource_id + path_part = "hello-world" + +} + + +resource "aws_api_gateway_method" "hello_world_method" { + rest_api_id = aws_api_gateway_rest_api.hello_world.id + resource_id = aws_api_gateway_resource.hello_world.id + http_method = "GET" + authorization = "NONE" + +} + + +resource "aws_api_gateway_method_settings" "default" { + rest_api_id = aws_api_gateway_rest_api.hello_world.id + stage_name = aws_api_gateway_stage.production.stage_name + method_path = "*/*" + + settings { + metrics_enabled = true + logging_level = "INFO" + + } +} + + +resource "aws_api_gateway_integration" "lambda_integration" { + rest_api_id = aws_api_gateway_rest_api.hello_world.id + resource_id = aws_api_gateway_resource.hello_world.id + http_method = aws_api_gateway_method.hello_world_method.http_method + + integration_http_method = "POST" + type = "AWS_PROXY" + uri = aws_lambda_function.hello_world.invoke_arn +} + +resource "aws_api_gateway_stage" "production" { + deployment_id = aws_api_gateway_deployment.hello_world_deployment.id + rest_api_id = aws_api_gateway_rest_api.hello_world.id + stage_name = "production" +} + +resource "aws_api_gateway_account" "api_gw_account" { + cloudwatch_role_arn = aws_iam_role.api_gateway_cloudwatch_role.arn +} + + +resource "aws_api_gateway_deployment" "hello_world_deployment" { + rest_api_id = aws_api_gateway_rest_api.hello_world.id + + depends_on = [ + aws_api_gateway_method.hello_world_method, + aws_api_gateway_integration.lambda_integration + ] +} diff --git a/apigw/hello-world/data.tf b/apigw/hello-world/data.tf new file mode 100644 index 0000000..91a9b28 --- /dev/null +++ b/apigw/hello-world/data.tf @@ -0,0 +1,5 @@ +data "archive_file" "lambda" { + type = "zip" + source_dir = "${path.module}/lambda/" + output_path = "${path.module}/lambda.zip" +} \ No newline at end of file diff --git a/apigw/hello-world/lambda.tf b/apigw/hello-world/lambda.tf new file mode 100644 index 0000000..20ce3ab --- /dev/null +++ b/apigw/hello-world/lambda.tf @@ -0,0 +1,43 @@ +resource "aws_iam_policy" "lambda_s3_access_policy" { + name = "lambda_s3_access_policy" + + policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Action = [ + "logs:CreateLogGroup", + "logs:CreateLogStream", + "logs:PutLogEvents" + ] + Effect = "Allow" + Resource = "arn:aws:logs:*:*:*" + } + ] + }) +} + +resource "aws_iam_role_policy_attachment" "lambda_s3_policy_attachment" { + role = aws_iam_role.lambda_execution_role.name + policy_arn = aws_iam_policy.lambda_s3_access_policy.arn +} + + +# Step 4: Create the Lambda function (assuming the Lambda ZIP package is uploaded) +resource "aws_lambda_function" "hello_world" { + function_name = "hello-world" + filename = "lambda.zip" + source_code_hash = data.archive_file.lambda.output_base64sha256 + handler = "main.lambda_handler" + runtime = "python3.10" # Adjust as per your runtime + role = aws_iam_role.lambda_execution_role.arn + timeout = 30 +} +resource "aws_lambda_permission" "api_gw_permission" { + statement_id = "AllowExecutionFromApiGateway" + action = "lambda:InvokeFunction" + function_name = aws_lambda_function.hello_world.function_name + principal = "apigateway.amazonaws.com" + + source_arn = "${aws_api_gateway_rest_api.hello_world.execution_arn}/*/*" +} \ No newline at end of file diff --git a/apigw/hello-world/lambda/main.py b/apigw/hello-world/lambda/main.py new file mode 100644 index 0000000..c494460 --- /dev/null +++ b/apigw/hello-world/lambda/main.py @@ -0,0 +1,11 @@ +import json +def lambda_handler(event, context): + message = { + 'message': 'Hello World' + } + + return { + 'statusCode': 200, + 'headers': {'Content-Type': 'application/json'}, + 'body': json.dumps(message) + } \ No newline at end of file diff --git a/apigw/hello-world/main.tf b/apigw/hello-world/main.tf new file mode 100644 index 0000000..eef9946 --- /dev/null +++ b/apigw/hello-world/main.tf @@ -0,0 +1,16 @@ +resource "aws_iam_role" "lambda_execution_role" { + name = "lambda_execution_role" + + assume_role_policy = jsonencode({ + Version = "2012-10-17" + Statement = [ + { + Action = "sts:AssumeRole" + Effect = "Allow" + Principal = { + Service = "lambda.amazonaws.com" + } + }, + ] + }) +} diff --git a/apigw/hello-world/provider.tf b/apigw/hello-world/provider.tf new file mode 100644 index 0000000..e9f14dc --- /dev/null +++ b/apigw/hello-world/provider.tf @@ -0,0 +1,5 @@ +provider "aws" { + region = "us-east-1" + access_key = var.access_key + secret_key = var.secret_key +} \ No newline at end of file diff --git a/apigw/hello-world/variables.tf b/apigw/hello-world/variables.tf new file mode 100644 index 0000000..a520a6e --- /dev/null +++ b/apigw/hello-world/variables.tf @@ -0,0 +1,7 @@ +variable "access_key" { + +} + +variable "secret_key" { + +} \ No newline at end of file