Skip to content

Commit cedca9d

Browse files
Tested and fixed tfstate store
1 parent d0093f5 commit cedca9d

25 files changed

+165
-388
lines changed

terraform/apigw.tf

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
resource "aws_apigatewayv2_api" "http_api" {
2+
name = "${var.env}-health-check-api"
3+
protocol_type = "HTTP"
4+
}
5+
6+
resource "aws_apigatewayv2_integration" "lambda_integration" {
7+
api_id = aws_apigatewayv2_api.http_api.id
8+
integration_type = "AWS_PROXY"
9+
integration_uri = aws_lambda_function.health_check.arn
10+
payload_format_version = "2.0"
11+
}
12+
13+
resource "aws_apigatewayv2_route" "health_route" {
14+
api_id = aws_apigatewayv2_api.http_api.id
15+
route_key = "GET /health"
16+
target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}"
17+
}
18+
19+
resource "aws_apigatewayv2_route" "health_route_post" {
20+
api_id = aws_apigatewayv2_api.http_api.id
21+
route_key = "POST /health"
22+
target = "integrations/${aws_apigatewayv2_integration.lambda_integration.id}"
23+
}
24+
25+
resource "aws_apigatewayv2_stage" "default_stage" {
26+
api_id = aws_apigatewayv2_api.http_api.id
27+
name = "$default"
28+
auto_deploy = true
29+
}
30+
31+
resource "aws_lambda_permission" "allow_apigw" {
32+
statement_id = "${var.env}-allow-apigw"
33+
action = "lambda:InvokeFunction"
34+
function_name = aws_lambda_function.health_check.function_name
35+
principal = "apigateway.amazonaws.com"
36+
source_arn = "${aws_apigatewayv2_api.http_api.execution_arn}/*/*"
37+
}

terraform/backend-prod.tfvars

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bucket = "serverlesshealthcheckapi"
2+
key = "serverless-health-check-api/prod/tfstate"
3+
region = "us-east-1"
4+
encrypt = true

terraform/backend-staging.tfvars

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
bucket = "serverlesshealthcheckapi"
2+
key = "serverless-health-check-api/staging/tfstate"
3+
region = "us-east-1"
4+
encrypt = true

terraform/backend.tfvars

Lines changed: 0 additions & 5 deletions
This file was deleted.

terraform/iam.tf

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
data "aws_iam_policy_document" "lambda_assume_role" {
2+
statement {
3+
effect = "Allow"
4+
principals {
5+
type = "Service"
6+
identifiers = ["lambda.amazonaws.com"]
7+
}
8+
actions = ["sts:AssumeRole"]
9+
}
10+
}
11+
12+
resource "aws_iam_role" "lambda_role" {
13+
name = "${var.env}-health-check-lambda-role"
14+
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role.json
15+
tags = {
16+
Environment = var.env
17+
}
18+
}
19+
20+
resource "aws_iam_role_policy" "lambda_policy" {
21+
name = "${var.env}-health-check-lambda-policy"
22+
role = aws_iam_role.lambda_role.id
23+
24+
policy = jsonencode({
25+
Version = "2012-10-17"
26+
Statement = [
27+
{
28+
Effect = "Allow"
29+
Action = [
30+
"dynamodb:PutItem"
31+
]
32+
Resource = aws_dynamodb_table.requests.arn
33+
},
34+
{
35+
Effect = "Allow"
36+
Action = [
37+
"logs:CreateLogGroup",
38+
"logs:CreateLogStream",
39+
"logs:PutLogEvents"
40+
]
41+
Resource = "arn:aws:logs:*:*:*"
42+
}
43+
]
44+
})
45+
}

terraform/lambda.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
data "archive_file" "lambda_zip" {
2+
type = "zip"
3+
output_path = "${path.module}/lambda_package/${var.env}-lambda.zip"
4+
5+
source {
6+
content = file("${path.module}/../lambda/lambda_function.py")
7+
filename = "lambda_function.py"
8+
}
9+
}
10+
11+
resource "aws_lambda_function" "health_check" {
12+
function_name = "${var.env}-health-check-function"
13+
filename = data.archive_file.lambda_zip.output_path
14+
handler = var.lambda_handler
15+
runtime = var.lambda_runtime
16+
role = aws_iam_role.lambda_role.arn
17+
source_code_hash = data.archive_file.lambda_zip.output_base64sha256
18+
19+
environment {
20+
variables = {
21+
REQUESTS_TABLE = aws_dynamodb_table.requests.name
22+
}
23+
}
24+
25+
tags = {
26+
Environment = var.env
27+
}
28+
}
745 Bytes
Binary file not shown.

terraform/main.tf

Lines changed: 11 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,15 @@
1-
terraform {
2-
required_version = ">= 1.0"
3-
4-
required_providers {
5-
aws = {
6-
source = "hashicorp/aws"
7-
version = "~> 5.0"
8-
}
9-
}
10-
}
11-
12-
provider "aws" {
13-
region = var.aws_region
14-
15-
default_tags {
16-
tags = local.common_tags
1+
resource "aws_dynamodb_table" "requests" {
2+
name = "${var.env}-requests-db"
3+
billing_mode = "PAY_PER_REQUEST"
4+
hash_key = "id"
5+
6+
attribute {
7+
name = "id"
8+
type = "S"
179
}
18-
}
1910

20-
locals {
21-
common_tags = {
22-
Environment = var.environment
23-
Project = var.project_name
24-
ManagedBy = "Terraform"
25-
CreatedAt = timestamp()
11+
tags = {
12+
Environment = var.env
13+
Name = "${var.env}-requests-db"
2614
}
2715
}
28-
29-
# DynamoDB Module
30-
module "dynamodb" {
31-
source = "./modules/dynamodb"
32-
environment = var.environment
33-
common_tags = local.common_tags
34-
}
35-
36-
# IAM Module
37-
module "iam" {
38-
source = "./modules/iam"
39-
40-
environment = var.environment
41-
dynamodb_table_arn = module.dynamodb.table_arn
42-
common_tags = local.common_tags
43-
}
44-
45-
# API Gateway Module
46-
module "api_gateway" {
47-
source = "./modules/api-gateway"
48-
environment = var.environment
49-
lambda_invoke_arn = module.lambda.function_invoke_arn
50-
common_tags = local.common_tags
51-
}
52-
53-
# Lambda Module
54-
module "lambda" {
55-
source = "./modules/lambda"
56-
environment = var.environment
57-
lambda_role_arn = module.iam.lambda_role_arn
58-
dynamodb_table_name = module.dynamodb.table_name
59-
api_gateway_execution_arn = module.api_gateway.execution_arn
60-
lambda_funtion_dir = var.lambda_funtion_dir
61-
common_tags = local.common_tags
62-
# depends_on = [module.api_gateway]
63-
}
64-

terraform/modules/api-gateway/main.tf

Lines changed: 0 additions & 62 deletions
This file was deleted.

terraform/modules/api-gateway/outputs.tf

Lines changed: 0 additions & 14 deletions
This file was deleted.

0 commit comments

Comments
 (0)