summaryrefslogtreecommitdiffstats
path: root/nixos/tests/amazon-cloudwatch-agent.nix
blob: ecb4e7c8c8d1c57c777fcfede7e813bafa8421ca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
{ pkgs, ... }:
let
  # See https://docs.aws.amazon.com/sdkref/latest/guide/file-format.html.
  iniFormat = pkgs.formats.ini { };

  region = "ap-northeast-1";
  sharedConfigurationDefaultProfile = "default";
  sharedConfigurationFile = iniFormat.generate "config" {
    "${sharedConfigurationDefaultProfile}" = {
      region = region;
    };
  };
  sharedCredentialsFile = iniFormat.generate "credentials" {
    "${sharedConfigurationDefaultProfile}" = {
      aws_access_key_id = "placeholder";
      aws_secret_access_key = "placeholder";
      aws_session_token = "placeholder";
    };
  };
  sharedConfigurationDirectory = pkgs.runCommand ".aws" { } ''
    mkdir $out

    cp ${sharedConfigurationFile} $out/config
    cp ${sharedCredentialsFile} $out/credentials
  '';
in
{
  name = "amazon-cloudwatch-agent";

  nodes.machine =
    { config, pkgs, ... }:
    {
      services.amazon-cloudwatch-agent = {
        enable = true;
        commonConfiguration = {
          credentials = {
            shared_credential_profile = sharedConfigurationDefaultProfile;
            shared_credential_file = "${sharedConfigurationDirectory}/credentials";
          };
        };
        configuration = {
          agent = {
            # Required despite documentation saying the agent ignores it in "onPremise" mode.
            region = region;

            # Show debug logs and write to a file for interactive debugging.
            debug = true;
            logfile = "/var/log/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log";
          };
          logs = {
            logs_collected = {
              files = {
                collect_list = [
                  {
                    file_path = "/var/log/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log";
                    log_group_name = "/var/log/amazon-cloudwatch-agent/amazon-cloudwatch-agent.log";
                    log_stream_name = "{local_hostname}";
                  }
                ];
              };
            };
          };
          traces = {
            local_mode = true;
            traces_collected = {
              xray = { };
            };
          };
        };
        mode = "onPremise";
      };

      # Keep the runtime directory for interactive debugging.
      systemd.services.amazon-cloudwatch-agent.serviceConfig.RuntimeDirectoryPreserve = true;
    };

  testScript = ''
    start_all()

    machine.wait_for_unit("amazon-cloudwatch-agent.service")

    machine.wait_for_file("/run/amazon-cloudwatch-agent/amazon-cloudwatch-agent.pid")
    machine.wait_for_file("/run/amazon-cloudwatch-agent/amazon-cloudwatch-agent.toml")
    # "config-translator" omits this file if no trace configurations are specified.
    #
    # See https://github.com/aws/amazon-cloudwatch-agent/issues/1320.
    machine.wait_for_file("/run/amazon-cloudwatch-agent/amazon-cloudwatch-agent.yaml")
    machine.wait_for_file("/run/amazon-cloudwatch-agent/env-config.json")
  '';
}