简体   繁体   English

使用 Terraform 为 AWS API 网关启用 CloudWatch 日志

[英]Enable CloudWatch logs for AWS API Gateway using Terraform

I am using OpenAPI 3.0 spec to deploy an AWS API Gateway.我正在使用 OpenAPI 3.0 规范部署 AWS API 网关。 I am not able to figure out how to enable cloud watch logs for the deployment.我无法弄清楚如何为部署启用云监视日志。

Here is the terraform code:这是 terraform 代码:

data "template_file" "test_api_swagger" {
  template = file(var.api_spec_path)

  vars = {
    //ommitted  
  }
}

resource "aws_api_gateway_rest_api" "test_api_gateway" {
  name        = "test_backend_api_gateway"
  description = "API Gateway for some x"
  body        = data.template_file.test_api_swagger.rendered

  endpoint_configuration {
    types = ["REGIONAL"]
  }
}

resource "aws_api_gateway_deployment" "test_lambda_gateway" {
  rest_api_id = aws_api_gateway_rest_api.test_api_gateway.id
  stage_name  = var.env
}

I checked Amazon OpenAPI extensions and none seem to have this option.我检查了 Amazon OpenAPI 扩展,似乎没有一个有这个选项。 Only way I see is using api_gateway_method_settings which I cannot use in this case.我看到的唯一方法是使用在这种情况下我无法使用的 api_gateway_method_settings 。

I think that it is not supported in terraform.我认为 terraform 不支持它。 I'm currently using terraform provisioner to run aws cli command after the deployment is created, like in the example below:我目前正在使用terraform 配置程序在创建部署后运行 aws cli 命令,如下例所示:

The example that I'm providing is to enable XRay tracing.我提供的示例是启用 XRay 跟踪。 You'll need to research the correct path and value to be used for CloudWatch logs.您需要研究用于 CloudWatch 日志的正确路径和值。 You can find more information in the docs .您可以在文档中找到更多信息。

resource "aws_api_gateway_deployment" "test_lambda_gateway" {
  rest_api_id = aws_api_gateway_rest_api.test_api_gateway.id
  stage_name  = var.env

  provisioner "local-exec" {
    command = "aws apigateway update-stage --region ${data.aws_region.current.name} --rest-api-id ${aws_api_gateway_rest_api.test_api_gateway.id} --stage-name ${var.env} --patch-operations op=replace,path=/tracingEnabled,value=true"
  }

}

You just need to make a reference to the aws data provider in your terraform template:您只需在 terraform 模板中引用 aws 数据提供程序:

data "aws_region" "current" {}

Even though you're creating the gateway with OpenAPI import, you can still use api_gateway_method_settings to reference the stage, assuming you're using a stage as recommended.即使您使用 OpenAPI 导入创建网关,您仍然可以使用 api_gateway_method_settings 来引用阶段,假设您按照推荐使用阶段。 See AWS documentation .请参阅AWS 文档 You would just indicate "*/*" on the method_path as per the example.根据示例,您只需在 method_path 上指示“*/*”。

resource "aws_api_gateway_stage" "example" {
  deployment_id = aws_api_gateway_deployment.test_lambda_gateway.id
  rest_api_id   = aws_api_gateway_rest_api.test_api_gateway.id
  stage_name    = "example"
}

resource "aws_api_gateway_method_settings" "all" {
  rest_api_id = aws_api_gateway_rest_api.test_api_gateway.id
  stage_name  = aws_api_gateway_stage.example.stage_name
  method_path = "*/*"

  settings {
    logging_level   = "INFO"
  }
}

This should set up the logging on the gateway for all requests with INFO level logging as if you had done it in the console on the stage.这应该在网关上为所有具有 INFO 级别日志记录的请求设置日志记录,就像您在舞台上的控制台中完成的一样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AWS API Gateway通过Boto3启用Cloudwatch日志 - AWS API Gateway Enable Cloudwatch Logs via Boto3 如何使用aws-sdk在API Gateway上启用CloudWatch日志和指标? - How do I enable CloudWatch logs and metrics on API Gateway using aws-sdk? 如何使用 Cloudformation 为 API 网关启用 Cloudwatch 日志? - How to enable Cloudwatch Logs for API Gateway using Cloudformation? AWS 网关和 CloudWatch 日志 - AWS Gateway and CloudWatch logs 从 Terraform 为 Beanstalk 环境启用 CloudWatch 日志 - Enable CloudWatch logs for Beanstalk env from Terraform 如何使用 Terraform 在 API Gateway 中创建一个具有启用的 cloudwatch 指标的阶段? - How to create a stage in API Gateway with enabled cloudwatch metrics using terraform? 通过Terraform将docker日志发送到AWS CloudWatch - Sending docker logs to AWS CloudWatch via Terraform 如何通过 Terraform 在 API Gateway v2 API 上启用 CloudWatch 日志记录? - How can I enable CloudWatch logging on an API Gateway v2 API via Terraform? 使用 terraform 在 aws api 网关中启用 url 查询参数的缓存 - Enable caching for url query parameters in aws api gateway with terraform 在 Web 应用程序中显示 AWS API Gateway Cloudwatch 日志(无需登录 AWS 控制台) - Show AWS API Gateway Cloudwatch Logs in Web Application ( without login to AWS console)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM