# How to setup "Custom Access Logging" for api gateway using serverless

**URL:** <https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288>\
**Category:** Serverless Framework\
**Created:** [December 10, 2017, 8:39am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288 "2017-12-10T08:39:18Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [December 10, 2017, 8:39am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/1 "2017-12-10T08:39:18Z")

</div>

### Updates

This is a too new feature, just announced in last month on Nov 21, 2017. We have to wait CloudFormation to support for this new feature. Only if it is supported, we can use it in serverless.

> **[Amazon API Gateway Supports Access Logging - AWS](https://aws.amazon.com/about-aws/whats-new/2017/11/amazon-api-gateway-supports-access-logging/)**
>
> Discover more about what's new at AWS with Amazon API Gateway Supports Access Logging

I will follow up this feature, until we can manage it with Cloudformation and serverless.

### My original questions

I share the knowledges on how to enable cloud watch logs for API Gateway using Serverless

> [@Knowledge sharing - Enable cloud watch logs for API Gateway using Serverless](http://forum.serverless.com/t/knowledge-sharing-enable-cloud-watch-logs-for-api-gateway-using-serverless/3287):
>
> Serverless team are still working to add this feature in core service directly ([https://github.com/serverless/serverless/issues/4461](https://github.com/serverless/serverless/issues/4461)). If you need enable cloud watch logs for API Gateway using Serverless now, please follow this document. Get most help from this ticket [How to enable cloud watch logs for API Gateway using Serverless](http://forum.serverless.com/t/how-to-enable-cloud-watch-logs-for-api-gateway-using-serverless/2067/3) But I still can’t make it work, if you follow its codes. So I need to understand how it works, how to enable cloudwatch logs in API Gateway. Provide an iam role AR…

But I need to go a further step.

With this document ([Set up CloudWatch logging for REST APIs in API Gateway - Amazon API Gateway](http://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html)), API Gateway supports to enable “Custom Access Logging” for each stage in an API gateway.

For example, I can customize the access logs directly with below json format:

`{ "requestId":"$context.requestId", "ip": "$context.identity.sourceIp", "caller":"$context.identity.caller", "user":"$context.identity.user","requestTime":"$context.requestTime", "httpMethod":"$context.httpMethod","resourcePath":"$context.resourcePath", "status":"$context.status","protocol":"$context.protocol", "responseLength":"$context.responseLength" }`

I go through the cloudformation document, can’t find out which resource can be used to define above part. Then I don’t know how to set it in serverless.

 ![04 pm](https://canada1.discourse-cdn.com/flex036/uploads/serverless/original/2X/3/319b5a529b3a988871400ca84f063ecce91c72c7.png)

Any one can give me a hint for this feature?

---

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [December 31, 2017, 11:48am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/2 "2017-12-31T11:48:17Z")

</div>

I found its cloudformation setting for custom access logging, the related keyword is

```
accessLogSettings

```

which need be set in resource **[Amazon API Gateway Stage](https://docs.aws.amazon.com/apigateway/api-reference/resource/stage/#accessLogSettings)**

So I should be fine to manage it with below codes, let me test it.

```
resources:
  Resources:
     ApiGatewayStage:
      Type: AWS::ApiGateway::Stage
      Properties:
        accessLogSettings:
          format: "String"
          destinationArn: "String"
```

---

<div class="post-metadata">

**Author:** ![cassiesu](https://avatars.discourse-cdn.com/v4/letter/c/a5b964/32.png) [@cassiesu](https://forum.serverless.com/u/cassiesu)\
**Post date:** [April 3, 2018, 2:26pm UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/3 "2018-04-03T14:26:45Z")

</div>

Hi, are you able to setup the custom access logging in Cloudformation?

---

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [April 3, 2018, 8:22pm UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/4 "2018-04-03T20:22:26Z")

</div>

No. It is supported in API, but not cloudformation. I can’t find the keyword `accessLogSettings` in below aws document.

[Amazon API Gateway Stage MethodSetting](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-methodsetting.html)

---

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [April 17, 2018, 7:03am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/5 "2018-04-17T07:03:09Z")

</div>

@cassiesu

I successfully did it via aws cli as work around.

aws cli supports `accessLogSettings`: [https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-stage.html](https://docs.aws.amazon.com/cli/latest/reference/apigateway/update-stage.html)

Wrote simple script to enable this feature.

```
$ cat custom_access_log.sh

#!/usr/bin/env bash

if ["$#" -ne 3] || ! [-f "$3"]; then
  echo "Usage: $0 [rest_api_id] [stage_name] [config_file_name]" >&2
  exit 1
fi

rest_api_id=$1
stage_name=$2
cli_input_json=$3

aws apigateway update-stage \
--rest-api-id "${rest_api_id}" \
--stage-name "${stage_name}" \
--cli-input-json "file://${cli_input_json}"  

$ cat config-dev.json

{
  "patchOperations": [
    {
      "op": "replace",
      "path": "/accessLogSettings/format",
      "value": "{ \"requestId\": \"$context.requestId\", \"ip\": \"$context.identity.sourceIp\", \"caller\": \"$context.identity.caller\", \"requestTime\": \"$context.requestTimeEpoch\", \"httpMethod\": \"$context.httpMethod\", \"resourcePath\": \"$context.resourcePath\", \"status\": \"$context.status\", \"protocol\": \"$context.protocol\", \"responseLength\": \"$context.responseLength\" }"
    },
    {
      "op": "replace",
      "path": "/accessLogSettings/destinationArn",
      "value": "arn:aws:logs:ap-southeast-2:123456789012:log-group:/aws/apigateway/serverless-CustomAccessLogs"
    }
  ]
}

```

So I can run below command to enable custom access log on nominated APIG

```
aws apigateway update-stage --rest-api-id xxxdwgi337 --stage-name dev --cli-input-json file://config-dev.json

```

Reference: [https://forums.aws.amazon.com/message.jspa?messageID=715755](https://forums.aws.amazon.com/message.jspa?messageID=715755)

---

<div class="post-metadata">

**Author:** ![AdrieanKhisbe](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/adrieankhisbe/32/2168_2.png) [@AdrieanKhisbe](https://forum.serverless.com/u/AdrieanKhisbe)\
**Post date:** [February 4, 2019, 9:59am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/6 "2019-02-04T09:59:44Z")

</div>

It is now supported via cloudformation as you can see in the docs:

- [https://docs.aws.amazon.com/fr\_fr/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-stage.html#cfn-apigateway-stage-accesslogsetting](https://docs.aws.amazon.com/fr_fr/AWSCloudFormation/latest/UserGuide/aws-resource-apigateway-stage.html#cfn-apigateway-stage-accesslogsetting)
- [https://docs.aws.amazon.com/fr\_fr/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-accesslogsetting.html](https://docs.aws.amazon.com/fr_fr/AWSCloudFormation/latest/UserGuide/aws-properties-apigateway-stage-accesslogsetting.html)

Syntax is the following:

```auto
resources:
  Resources:
     ApiGatewayStage:
      Type: AWS::ApiGateway::Stage
      Properties:
        AccessLogSetting:
          Format: "String"
          DestinationArn: "String"

```

I don’t know since when, but now it’s possible 🙌

---

<div class="post-metadata">

**Author:** ![bill](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.serverless.com/bill/32/1072_2.png) [@bill](https://forum.serverless.com/u/bill)\
**Post date:** [March 1, 2019, 12:15am UTC](https://forum.serverless.com/t/how-to-setup-custom-access-logging-for-api-gateway-using-serverless/3288/7 "2019-03-01T00:15:12Z")

</div>

That’s great. Thanks for the updates.
