Skip to content

Commit

Permalink
rename folder
Browse files Browse the repository at this point in the history
  • Loading branch information
tinhtq committed Dec 20, 2024
1 parent b317cb3 commit 01ecc71
Show file tree
Hide file tree
Showing 15 changed files with 154 additions and 0 deletions.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions apigw/hello-world/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# API Gateway to trigger Hello World Lambda Function
66 changes: 66 additions & 0 deletions apigw/hello-world/apigw.tf
Original file line number Diff line number Diff line change
@@ -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
]
}
5 changes: 5 additions & 0 deletions apigw/hello-world/data.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
data "archive_file" "lambda" {
type = "zip"
source_dir = "${path.module}/lambda/"
output_path = "${path.module}/lambda.zip"
}
43 changes: 43 additions & 0 deletions apigw/hello-world/lambda.tf
Original file line number Diff line number Diff line change
@@ -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}/*/*"
}
11 changes: 11 additions & 0 deletions apigw/hello-world/lambda/main.py
Original file line number Diff line number Diff line change
@@ -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)
}
16 changes: 16 additions & 0 deletions apigw/hello-world/main.tf
Original file line number Diff line number Diff line change
@@ -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"
}
},
]
})
}
5 changes: 5 additions & 0 deletions apigw/hello-world/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
provider "aws" {
region = "us-east-1"
access_key = var.access_key
secret_key = var.secret_key
}
7 changes: 7 additions & 0 deletions apigw/hello-world/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
variable "access_key" {

}

variable "secret_key" {

}

0 comments on commit 01ecc71

Please sign in to comment.