Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Amazon API Gateway Access Logging Support #2406

Closed
bflad opened this issue Nov 22, 2017 · 11 comments · Fixed by #4369
Closed

Amazon API Gateway Access Logging Support #2406

bflad opened this issue Nov 22, 2017 · 11 comments · Fixed by #4369
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/apigateway Issues and PRs that pertain to the apigateway service.
Milestone

Comments

@bflad
Copy link
Contributor

bflad commented Nov 22, 2017

AWS has announced an enhancement to API Gateway to write access logs in CloudWatch: https://aws.amazon.com/about-aws/whats-new/2017/11/amazon-api-gateway-supports-access-logging/

Prerequisite: #2403

Terraform Version

terraform 0.10+
terraform-provider-aws 1.3.1

Affected Resource(s)

  • aws_api_gateway_stage

Expected Behavior

resource "aws_api_gateway_stage" "example" {
  # ... other configuration ...  new enhancement:
  access_log {
    destination_arn = "..."
    format          = "..." 
  }
}

References

Still waiting on the aws-sdk-go API documentation to update, but appears to be new AccessLogSettings struct inside Stage.

@Ninir Ninir added the enhancement Requests to existing resources that expand the functionality or scope. label Nov 22, 2017
@roberthutto
Copy link

roberthutto commented Nov 28, 2017

The aws api should already support this with a patch operation on http://docs.aws.amazon.com/cli/latest/reference/apigateway/update-stage.html

In the interim Im using a null resource to accomplish this.

resource "null_resource" "access-logging" {

  depends_on = ["aws_cloudwatch_log_group.cloudwatch_access_log_group"]
  count = "${var.access_logs_enabled == "true" ? 1 : 0}"
  triggers {
    log_format = "${file("log_format.json")}"
    log_group = "${local.cloudwatch_access_log_group_arn}"
  }
  provisioner "local-exec" {
    command = "aws apigateway update-stage --rest-api-id ${aws_api_gateway_deployment.deployment.rest_api_id} --stage-name ${aws_api_gateway_deployment.deployment.stage_name} --patch-operations op=replace,path=/accessLogSettings/destinationArn,value='${local.cloudwatch_access_log_group_arn}'"
  }
  provisioner "local-exec" {
    command = "aws apigateway update-stage --rest-api-id ${aws_api_gateway_deployment.deployment.rest_api_id} --stage-name ${aws_api_gateway_deployment.deployment.stage_name} --patch-operations 'op=replace,path=/accessLogSettings/format,value=${jsonencode(replace(file("log_format.json"), "\n", ""))}'"
  }

  provisioner "local-exec" {
    when = "destroy"
    command = "aws apigateway update-stage --rest-api-id ${aws_api_gateway_deployment.deployment.rest_api_id} --stage-name ${aws_api_gateway_deployment.deployment.stage_name} --patch-operations op=remove,path=/accessLogSettings,value="
  }
}

I have not looked at the code but I assume the https://www.terraform.io/docs/providers/aws/r/api_gateway_method_settings.html uses the same patch operation on metrics and logging level

@volkodava
Copy link

volkodava commented Dec 3, 2017

Hello @roberthutto

Thanks for the code.

UPD: Information about log_format.json available at http://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html

@radeksimko radeksimko added the service/apigateway Issues and PRs that pertain to the apigateway service. label Jan 28, 2018
@smrtslckr
Copy link

Is there an ETA on when this functionality will be ready?

@Oupsla
Copy link

Oupsla commented Apr 25, 2018

Any news ? 😃

@bflad
Copy link
Contributor Author

bflad commented May 2, 2018

This has been released in version 1.17.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading.

@phuonghuynh
Copy link

How could I set my custom cloud-watch-log-arn in "aws_api_gateway_deployment"?

The "aws_api_gateway_method_settings" has no way to set it.

The "aws_api_gateway_stage" create new stage, no update to the existing one which is already deployed using "aws_api_gateway_deployment".

I would greatly appreciate it if someone kindly give me some advise on this

@tdmalone
Copy link
Contributor

@phuonghuynh The CloudWatch ARN setting applies to your whole account rather than a specific API or endpoint. You’ll find it at https://www.terraform.io/docs/providers/aws/r/api_gateway_account.html

@phuonghuynh
Copy link

@tdmalone Thanks for getting back so fast. I really appreciate it.

Do you mean the attribute cloudwatch_role_arn in "aws_api_gateway_account" ?

I am finding a way to set CloudWatchLogGroupARN to GatewayAPI settings, like api_gateway_stage does. But it create new stage rather than update the existing one which was created by using "aws_api_gateway_deployment".

@tdmalone
Copy link
Contributor

Ah, sorry - I misinterpreted what you were asking. Yeah, this is a pain, but the workaround I've come across so far is to leave the stage name blank - see #2918 (comment)

@cmosetick
Copy link

For anyone coming here wondering how to enable logging at the global level, but override the logging for a specific endpoint, I found that this was the only way that worked for me is the way below. Note that in my example I tried using authenticate/* but found that it did not work, only using authenticate/POST or GET, etc, etc. seems to work.

# enable logging at the global level
resource "aws_api_gateway_method_settings" "init" {
  rest_api_id = "${local.api_id}"
  stage_name  = "${aws_api_gateway_stage.init.stage_name}"
  method_path = "*/*"

  settings {
    metrics_enabled    = "${local.cloudwatch_metrics_enabled[terraform.workspace]}"
    logging_level      = "${local.cloudwatch_logging_level[terraform.workspace]}"
    data_trace_enabled = "${local.cloudwatch_request_response_logging_enabled[terraform.workspace]}"
  }
}

# disable logging for a specific end point
resource "aws_api_gateway_method_settings" "authenticate" {
  rest_api_id = "${local.api_id}"
  stage_name  = "${aws_api_gateway_stage.init.stage_name}"
  method_path = "authenticate/POST"

  settings {
    metrics_enabled    = false
    logging_level      = "OFF"
    data_trace_enabled = false
  }
}

@ghost
Copy link

ghost commented Apr 4, 2020

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.

If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks!

@ghost ghost locked and limited conversation to collaborators Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/apigateway Issues and PRs that pertain to the apigateway service.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

10 participants