Skip to content

Instantly share code, notes, and snippets.

@mpuhacz
Created March 27, 2021 12:02
Show Gist options
  • Save mpuhacz/b7e0e957c427cf43484cd23f51dd1f02 to your computer and use it in GitHub Desktop.
Save mpuhacz/b7e0e957c427cf43484cd23f51dd1f02 to your computer and use it in GitHub Desktop.
Generates AWS Cloudwatch insights console URL
export function getInsightsUrl(
start: Date,
end: Date,
query: string,
sourceGroup: string,
region = "us-east-1"
) {
const p = (m: string) => escape(m);
// encodes inner values
const s = (m: string) => escape(m).replace(/\%/gi, "*");
const queryDetail =
p(`~(end~'`) +
s(end.toISOString()) +
p(`~start~'`) +
s(start.toISOString()) +
p(`~timeType~'ABSOLUTE~tz~'UTC~editorString~'`) +
s(query) +
p(`~isLiveTail~false~queryId~'`) +
s(v4()) +
p(`~source~(~'`) +
s(sourceGroup) +
p(`))`);
return (
`https://console.aws.amazon.com/cloudwatch/home?region=${region}#logsV2:logs-insights` +
escape("?queryDetail=" + queryDetail).replace(/\%/gi, "$")
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment