Package: aws-cloudwatch-insights
AWS Cloudwatch InsightsBoth a command line tool and a python API to simplify interacting with AWS cloudwatch insightsCLIInstallation# globally$pipxinstall'aws_cloudwatch_insights[cli]'# or in a particular virtual env$pipinstall'aws_cloudwatch_insights[cli]'UsageRun via theacwicommand.  Results returned in json.It takes either a file of AWS Inisghts code or a yaml file with aqueryargument and other options.For example, if fileacwi.acwicontained this:fields @timestamp, @message, @logStream, @log
| sort @timestamp desc
| limit 20And you ran this:$acwi--group-names/aws/lambda/log_maker--regionus-west-2--start-30dtmp/acwi.acwi--outresults.jsonIt would run the query against the stated log group in the stated region from 30 days ago.Alternatively, if you had a fileacwi.ymlcontaining this:query:|fields @timestamp, @message, @logStream, @log| sort @timestamp desc| limit 20groups:-/aws/lambda/log_makerregion:us-west-2start:-30dout_file:results.jsonAnd running this:$acwiacwi.ymlYou would get the same result.If an option is on both the command line of in the .yml file, the command line overrides the value in the file.There is some fancy partial results returned as the query runs.  You can shut these off using the--quietoption.APIIf you're only using the api, you don't need to install with the[cli]extras.$pipinstallaws_cloudwatch_insightsUsageExamplesfromaws_cloudwatch_insightsimportInsightsfromdatetimeimporttimedeltainsights=Insights()query="""fields @timestamp, @message, @logStream, @log| sort @timestamp desc| limit 20"""results=insights.get_insights(query,group_names=["/aws/lambda/log_maker"],result_limit=20,start_time=-timedelta(days=1))ReferenceFrom the inline documentation:classInsights:def__init__(self,logs_client:Optional[BaseClient]=None):"""Object for querying AWS Cloudwatch.  Optionally takes a boto3 client as an argument, otherwise creates its own"""...defget_insights(self,query:str,result_limit:int,group_names:List[str],start_time:Union[int,datetime,timedelta],end_time:Union[int,datetime,timedelta,None]=None,callback:Optional[CallbackFunction]=None,error:Optional[ErrorFunction]=None,jsonify:bool=True)->Iterable[GenericDict]:"""Gets elements from AWS Cloudwatch Logs using an Insights query:Returns an iterable of dictsquery: The Insights queryresult_limit: Limit of the number of results returnedgroup_names: The log groups searched throughstart_time: The time of the earliest record the query looks for.  Can be an int timestamp, a datetime, or atimedelta.  If it's a timedelta, the start time is now offset by the deltaend_time: The time of the latest record the query looks for.  Accepts same values as `start_time`callback: A function witch is called when partial results are returned, before the function returns the finalresults.  Takes the partial results as an argument.error: If included, this function is called when an exception is encountered (instead of not catching theexception).  The arguments passed to the function are the Exception instance and the partial results.  Ifthis function doesn't through an exception, get_insights() returns the returned value of this function, emptylist if that value is Nonejsonify: If set to True, attempts to parse suspected json objects.  If parsing fails, just returns the string.Default: True"""...DevelopmentRequires make:# setting up dev environment$makedevelop# run tests$maketest# ... or$pytest# run tests for all environments$maketest-allNo CI/CD or coverage yetTo DoIf someone tells me they actually use this, I'll bump it to v1.0.0More extensive CLI unit tests--csvoutput optionIntegration testsCreditsThis package was created withcookiecutterand theaudreyr/cookiecutter-pypackageproject template.