Skip to content

Commit

Permalink
Merge pull request #171 from tungbq/issue-162
Browse files Browse the repository at this point in the history
[Issue-162] Link a Serverless Function to a Web App
  • Loading branch information
tungbq authored Jul 1, 2023
2 parents f97dad4 + bd9b6ab commit efd7040
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
61 changes: 61 additions & 0 deletions tutorial/serverless-basic-application/api-gateway.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# Flow:
# New API -> Resource -> Method -> Interation -> Deployment

resource "aws_api_gateway_rest_api" "lambda" {
name = "serverless_lambda_gw"
}

# Resource
resource "aws_api_gateway_resource" "resource" {
path_part = "resource"
parent_id = aws_api_gateway_rest_api.lambda.root_resource_id
rest_api_id = aws_api_gateway_rest_api.lambda.id
}

# Method
resource "aws_api_gateway_method" "method" {
rest_api_id = aws_api_gateway_rest_api.lambda.id
resource_id = aws_api_gateway_resource.resource.id
http_method = "POST"
authorization = "NONE"
}

# Integration
resource "aws_api_gateway_integration" "integration" {
rest_api_id = aws_api_gateway_rest_api.lambda.id
resource_id = aws_api_gateway_resource.resource.id
http_method = aws_api_gateway_method.method.http_method
integration_http_method = "POST"
type = "AWS_PROXY"
uri = aws_lambda_function.hello_world.invoke_arn
}

# Deployment
resource "aws_api_gateway_deployment" "example" {
rest_api_id = aws_api_gateway_rest_api.lambda.id

triggers = {
redeployment = sha1(jsonencode([
aws_api_gateway_resource.resource.id,
aws_api_gateway_method.method.id,
aws_api_gateway_integration.integration.id,
]))
}

lifecycle {
create_before_destroy = true
}
}

# Stage
resource "aws_api_gateway_stage" "lambda" {
rest_api_id = aws_api_gateway_rest_api.lambda.id
deployment_id = aws_api_gateway_deployment.example.id
stage_name = "serverless_lambda_stage"

# Temporarity disable logging. TODO: work on it later
# access_log_settings { ... }
}

# Logging
# Temporarity disable logging. TODO: work on it later
6 changes: 6 additions & 0 deletions tutorial/serverless-basic-application/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,9 @@ output "function_name" {

value = aws_lambda_function.hello_world.function_name
}

output "base_url" {
description = "Base URL for API Gateway stage."

value = aws_api_gateway_stage.lambda.invoke_url
}

0 comments on commit efd7040

Please sign in to comment.