terraform 파일 구조
C:\USERS\USER\DESKTOP\TERRAFORM-DEVELOPMENTS\CLOUDWATCH-ALARM
└─infrastructure
└─terraform
├─.terraform
│ ├─modules
│ └─providers
│ └─registry.terraform.io
│ └─hashicorp
│ └─aws
│ └─5.54.1
│ └─windows_amd64
├─development
└─modules
├─apigateway
├─CloudWatch
├─dynamodb
├─iam
├─lambda
├─s3
└─SimpleNotificationService
7회차에 추가한 내용에서 추가
CloudWatch
\terraform\modules\CloudWatch\main.tf
resource "aws_cloudwatch_log_metric_filter" "lambda_error_filter" {
name = "LambdaErrorFilter"
log_group_name = var.log_group_name
pattern = "Error processing survey"
metric_transformation {
name = var.error_metric_name
namespace = var.error_metric_namespace
value = var.error_metric_value
default_value = var.error_default_value
unit = var.error_metric_unit
}
}
resource "aws_cloudwatch_log_metric_filter" "lambda_success_filter" {
name = "LambdaSuccessFilter"
log_group_name = var.log_group_name
pattern = "Survey data saved successfully"
metric_transformation {
name = var.success_metric_name
namespace = var.success_metric_namespace
value = var.success_metric_value
default_value = var.success_default_value
unit = var.success_metric_unit
}
}
resource "aws_cloudwatch_metric_alarm" "lambda_error_alarm" {
alarm_name = "LambdaErrorAlarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = var.error_metric_name
namespace = var.error_metric_namespace
period = "300" # 5 minutes
statistic = "Sum"
threshold = "1"
alarm_actions = [var.lambda_error_topic_arn]
}
resource "aws_cloudwatch_metric_alarm" "lambda_success_alarm" {
alarm_name = "LambdaSuccessAlarm"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "1"
metric_name = var.success_metric_name
namespace = var.success_metric_namespace
period = "300" # 5 minutes
statistic = "Sum"
threshold = "1"
alarm_actions = [var.lambda_success_topic_arn]
}
\terraform\modules\CloudWatch\variables.tf
variable "log_group_name" {
description = "The name of the log group to apply the metric filter to"
type = string
}
variable "error_metric_name" {
description = "The name of the CloudWatch error metric"
type = string
}
variable "error_metric_namespace" {
description = "The namespace of the CloudWatch error metric"
type = string
}
variable "error_metric_value" {
description = "The value of the CloudWatch error metric"
type = string
}
variable "error_default_value" {
description = "The default value of the CloudWatch error metric"
type = number
}
variable "error_metric_unit" {
description = "The unit of the CloudWatch error metric"
type = string
}
variable "success_metric_name" {
description = "The name of the CloudWatch success metric"
type = string
}
variable "success_metric_namespace" {
description = "The namespace of the CloudWatch success metric"
type = string
}
variable "success_metric_value" {
description = "The value of the CloudWatch success metric"
type = string
}
variable "success_default_value" {
description = "The default value of the CloudWatch success metric"
type = number
}
variable "success_metric_unit" {
description = "The unit of the CloudWatch success metric"
type = string
}
variable "lambda_error_topic_arn" {
description = "The ARN of the SNS topic for Lambda errors"
type = string
}
variable "lambda_success_topic_arn" {
description = "The ARN of the SNS topic for Lambda successes"
type = string
}
\terraform\modules\SNS\main.tf
resource "aws_sns_topic" "lambda_error_notifications" {
name = "LambdaErrorNotifications"
}
resource "aws_sns_topic_subscription" "lambda_error_subscription" {
topic_arn = aws_sns_topic.lambda_error_notifications.arn
protocol = "email"
endpoint = "p8489009@gmail.com"
}
resource "aws_sns_topic" "lambda_success_notifications" {
name = "LambdaSuccessNotifications"
}
resource "aws_sns_topic_subscription" "lambda_success_subscription" {
topic_arn = aws_sns_topic.lambda_success_notifications.arn
protocol = "email"
endpoint = "p8489009@gmail.com"
}
\terraform\main.tf
provider "aws" {
region = var.region
}
module "dynamodb" {
source = "./modules/dynamodb"
name = "dynamodb-html"
environment = "Production"
}
module "iam" {
source = "./modules/iam"
lambda_role_name = "lambda-dynamodb-role"
api_gateway_role_name = "apigateway-cloudwatch-role"
}
module "lambda" {
source = "./modules/lambda"
s3_bucket = var.s3_bucket_name
s3_key = var.s3_object_key
function_name = "lambda-html"
role_arn = module.iam.lambda_dynamodb_role_arn
handler = "lambda_function.lambda_handler"
runtime = "python3.12"
dynamodb_table = module.dynamodb.table_name
source_arn = "arn:aws:execute-api:${var.region}:${data.aws_caller_identity.current.account_id}:*"
}
module "apigateway" {
source = "./modules/apigateway"
name = "REST-html"
description = "REST API for handling survey submissions"
lambda_invoke_arn = module.lambda.lambda_invoke_arn
stage_name = "test"
depends_on = [module.lambda]
}
module "cloudwatch_metric_filters" {
source = "./modules/CloudWatch"
log_group_name = "/aws/lambda/lambda-html"
error_metric_name = "ErrorCount"
error_metric_namespace = "MyApp/LambdaErrors"
error_metric_value = "1"
error_default_value = 0
error_metric_unit = "Count"
success_metric_name = "SuccessCount"
success_metric_namespace = "MyApp/LambdaSuccesses"
success_metric_value = "1"
success_default_value = 0
success_metric_unit = "Count"
lambda_error_topic_arn = module.sns_notifications.lambda_error_topic_arn
lambda_success_topic_arn = module.sns_notifications.lambda_success_topic_arn
}
module "sns_notifications" {
source = "./modules/SNS"
}
output "api_endpoint" {
value = module.apigateway.api_endpoint
}
LambdaSuccessAlarm을 통해 경보 발생

경보 확인
