gpt4 book ai didi

aws-api-gateway - 如何通过 Terraform 设置 aws cloudwatch 保留

转载 作者:行者123 更新时间:2023-12-04 21:31:19 24 4
gpt4 key购买 nike

使用 Terraform 部署 API Gateway/Lambda 并且在 Cloudwatch 中已经有了相应的日志。但是,我似乎无法找到一种使用我当前部署的资源(如下)通过 Terraform 设置日志保留的方法。看起来日志组资源是我要做的地方,但不确定如何将来自 api 网关的日志流指向新的日志组。我一定遗漏了一些明显的东西......非常感谢任何建议!

resource "aws_api_gateway_account" "name" {
cloudwatch_role_arn = "${aws_iam_role.cloudwatch.arn}"
}

resource "aws_iam_role" "cloudwatch" {
name = "#{name}_APIGatewayCloudWatchLogs"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "Allow",
"Principal": {
"Service": "apigateway.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}
EOF
}

resource "aws_iam_policy_attachment" "api_gateway_logs" {
name = "#{name}_api_gateway_logs_policy_attach"
roles = ["${aws_iam_role.cloudwatch.id}"]
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
}

resource "aws_api_gateway_method_settings" "name" {
rest_api_id = "${aws_api_gateway_rest_api.name.id}"
stage_name = "${aws_api_gateway_stage.name.stage_name}"
method_path = "${aws_api_gateway_resource.name.path_part}/${aws_api_gateway_method.name.http_method}"

settings {
metrics_enabled = true
logging_level = "INFO"
data_trace_enabled = true
}
}

最佳答案

是的,您可以在创建 Lambda 函数之前使用 Lambda 日志名称创建日志资源。或者您可以导入现有的日志组。

resource "aws_cloudwatch_log_group" "lambda" {
name = "/aws/lambda/${var.env}-${join("", split("_",title(var.lambda_name)))}-Lambda"

retention_in_days = 7

lifecycle {
create_before_destroy = true
prevent_destroy = false
}
}

关于aws-api-gateway - 如何通过 Terraform 设置 aws cloudwatch 保留,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50417706/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com