Skip to content

Commit

Permalink
add cloudwatch role
Browse files Browse the repository at this point in the history
  • Loading branch information
tinhtq committed Dec 20, 2024
1 parent 66b3590 commit b317cb3
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 5 deletions.
60 changes: 58 additions & 2 deletions apigw/apigw-hello-world-lambda/apigw.tf
Original file line number Diff line number Diff line change
@@ -1,10 +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" "proxy" {

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 = "{proxy+}"
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
]
}
10 changes: 9 additions & 1 deletion apigw/apigw-hello-world-lambda/lambda.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ resource "aws_iam_role_policy_attachment" "lambda_s3_policy_attachment" {


# Step 4: Create the Lambda function (assuming the Lambda ZIP package is uploaded)
resource "aws_lambda_function" "hello-world" {
resource "aws_lambda_function" "hello_world" {
function_name = "hello-world"
filename = "lambda.zip"
source_code_hash = data.archive_file.lambda.output_base64sha256
Expand All @@ -33,3 +33,11 @@ resource "aws_lambda_function" "hello-world" {
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: 9 additions & 2 deletions apigw/apigw-hello-world-lambda/lambda/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
import json
def lambda_handler(event, context):
return {
'message' : "Hello World"
message = {
'message': 'Hello World'
}

return {
'statusCode': 200,
'headers': {'Content-Type': 'application/json'},
'body': json.dumps(message)
}

0 comments on commit b317cb3

Please sign in to comment.