如何通过Terraform设置aws cloudwatch保留

时间:2018-05-18 18:57:28

标签: aws-api-gateway terraform amazon-cloudwatch

使用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
  }
}

1 个答案:

答案 0 :(得分:0)

是的,可以在创建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
          }
        }