简体   繁体   中英

How do I enable CloudWatch logs and metrics on API Gateway using aws-sdk?

Using the AWS web console this is just a matter of navigating to the API's stage in question and ticking the boxes under CloudWatch Settings. However I am using the aws-sdk to build my API and I seen no way to do this programatically. There is nothing under createStage, createMethod or anywhere else I have looked. Can I enable CloudWatch for my API using the aws-sdk?

Assuming you're using the Java SDK, here's how you could go about it:

  1. find the instance of the RestApi for which the stage is meant. For instance by cycling through the result of invoking:

    RestApi restApi = amazonApiGateway.getRestApis().getItem().get(0)

  2. get the stage you wish to configure:

    Stage stage = restApi.getStageByName("");

  3. Create a patch document:

    PatchDocument patchDocument = PatchUtils.createPatchDocument( PatchUtils.createReplaceOperation("/*/*/metrics/enabled", "true"), PatchUtils.createReplaceOperation("/*/*/logging/dataTrace", "true"), PatchUtils.createReplaceOperation("/*/*/logging/loglevel", "INFO") ); stage.updateStage(patchDocument);

In aws-java-sdk-api-gateway, version: '1.11.264'

I was able to start the CloudWatch Logging using following Code after a lot of time and help from Patrice.

If you are importing a yaml/json formatted swagger to create a API, after Importing is done you get apiID then you deploy it and then you update the Stage Settings as required. So the code below starts from deployment procedure - once deployed then you can change the stage setting

private void deployAPI(String name, String apiID) throws IOException, InterruptedException {
    CreateDeploymentRequest createDeploymentRequest = new CreateDeploymentRequest();
    createDeploymentRequest.setRestApiId(apiID);
    createDeploymentRequest.setStageName(getEnvironment());
    for (int i = 0; i < 5; i++) {
      try {
        client.createDeployment(createDeploymentRequest);
        break;
      } catch (TooManyRequestsException e) {
        log.error(e);
        sleepToAvoidAWSTooManyRequestException();
      }
    }

    log.info("API deployed  : " + apiID);
    System.out.println("API Stage Updating");
    client.updateStage(new UpdateStageRequest().withRestApiId(apiID).withStageName(getEnvironment())
        .withPatchOperations(getpatchOperations()));
    System.out.println("API Stage Updated");
  }

  private List<PatchOperation> getpatchOperations() {
    List<PatchOperation> list = new ArrayList<PatchOperation>();
    list.add(new PatchOperation().withOp(Op.Replace).withPath("/*/*/metrics/enabled").withValue("true"));
    list.add(new PatchOperation().withOp(Op.Replace).withPath("/*/*/logging/dataTrace").withValue("true"));
    list.add(new PatchOperation().withOp(Op.Replace).withPath("/*/*/logging/loglevel").withValue("ERROR"));
    return list;
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM