Socket
Socket
Sign inDemoInstall

aws-logs-sink

Package Overview
Dependencies
161
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    aws-logs-sink

Stream logs to Amazon CloudWatch logs


Version published
Maintainers
1
Created

Readme

Source

aws-logs-sink

Stream logs to AWS CloudWatch Logs.

Either pipe your log lines to stdin from the CLI, or use this module programmatically in NodeJS––it exposes a NodeJS writable stream.

Can't I just use the AWS CLI/SDK?

Yes, but streaming logs to CloudWatch Logs is slightly less trivial than you might think, because each successive putLogEvents call must include the log stream's nextSequenceToken from the previous putLogEvents call. This module is a small wrapper around the JavaScript AWS SDK (V3) to make streaming logs easy.

CLI usage

Install globally:

npm install -g aws-logs-sink

Provide the log group name and stream name as arguments, and pipe to stdin:

echo "This is a test" | aws-logs-sink <log-group-name> <log-stream-name>

Of course a long running stream works too:

while true; do date; sleep 1; done | aws-logs-sink <log-group-name> <log-stream-name>

The log group and stream will be created if they don't exist. If the log stream already exists, its next sequence token will be queried, in order to be able to append to the stream.

CLI options:

OptionDescription
-f, --flush-intervalFlush to CloudWatch every X seconds (default: 1)
--teeAlso print all input to stdout
--eolLine termination character(s) (default: \n on Mac/Linux, \r\n on Windows)
--profileAWS profile to use. Setting environment variable AWS_PROFILE works too
--regionAWS region to use. Setting environment variable AWS_REGION works too

Programmatic usage

Install locally:

npm install aws-logs-sink
import awsLogsSink from "aws-logs-sink";

// awsLogsSink is a function that returns a NodeJS writable stream:
const writable = awsLogsSink({
  logGroupName: "<log-group-name>",
  logStreamName: "<log-stream-name>",
  flushInterval: 1, // optional, see table above
  tee: false, // optional, see table above
  eol: "\n", // optional, see table above
  profile: "default", // optional, see table above
  region: "eu-west-1", // optional, see table above
  client: new CloudWatchLogsClient({}), // optional, will be created if not provided
});

writable.write("Hello, world!");

// Or more fancy with a pipeline:

import * as stream from "stream";

const readable = new stream.Readable({ read: () => {} });
readable.push("Hello, world! From pipeline");
readable.push(null);

stream.pipeline(readable, writable, (err) => {
  err ? console.error(err) : console.log("Done writing to CloudWatch");
});

Required AWS permissions

PermissionRequired
logs:CreateLogGroupYes
logs:CreateLogStreamYes
logs:PutLogEventsYes
logs:DescribeLogStreamsOnly if writing to an existing log stream, to query its next sequence token

Keywords

FAQs

Last updated on 24 Nov 2021

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc