aws-cloudformation / rain

A development workflow tool for working with AWS CloudFormation.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Implement a "User Initiated" filter option for the rain logs command

Tommes-M opened this issue · comments

Dear rain maintainers,

I propose a feature for the rain logs command to improve log readability in CI/CD environments. The idea is to add an option that filters logs to only those after the last "User Initiated" CloudFormation event. While the --days parameter exists, it's not precise enough, often including logs from multiple updates within the timeframe.

Proposed Solution:

Introduce something like a --since-user-initiated flag to filter logs effectively.
This targets logs post the most recent "User Initiated" event, avoiding the clutter of unrelated entries.
Challenge:

The current --days parameter often captures logs from several updates if multiple deployments occur in the specified period.

I'm keen to contribute to implementing this feature. However, I completely lack expertise in the language "Go".
So I would be very grateful if someone else could handle the implementation. I don't think this requires much effort?

This feature would significantly enhance log management efficiency in busy CI/CD pipelines.

Thanks for considering this request. I look forward to any feedback.

Best regards,
Tommes

What would the filter look like if you were doing this with the CLI/API?

Hello Eric,

unfortunately, as far as I know, there is no direct parameter provided by the AWS API that allows all CloudFormation event entries from the last "User initiated" to be output. Nevertheless, it is possible to extract the whole thing from the CloudFormation event entries with a few combined API commands. For example, the workflow in Python would look like this:

Step 1: Fetching All Stack Events

We start by retrieving all the events associated with a specific CloudFormation stack:

import boto3

# Initialize the boto3 client for CloudFormation
cloudformation_client = boto3.client('cloudformation')

# This line calls the AWS CloudFormation API to get a detailed list of all events for the specified stack.
response = cloudformation_client.describe_stack_events(StackName="example")

Step 2: Identifying the Last 'User Initiated' Event

Next, we find the last "User Initiated" event from these stack events:

# This loop iterates through the stack events to find the event where the ResourceStatusReason contains 'User Initiated', which marks the start of relevant logs.
for event in response['StackEvents']:
    if 'User Initiated' in event.get('ResourceStatusReason', ''):
        return event

Step 3: Displaying Events Since the Last 'User Initiated' Event

Once we have identified the last "User Initiated" event, we display all subsequent events:

# Here, we calculate the index of the last "User Initiated" event in the list of events and then print all events up to and including this index. The +1 ensures that the "User Initiated" event itself is included in the output.
last_event_index = response['StackEvents'].index(event)
print(response['StackEvents'][:last_event_index+1])

As I explained above, this is just an example of how the whole thing could be implemented in Python. However, "rain" already has the "rain logs" command. You can certainly modify this so that the workflow described above runs in the background using a filter such as "rain logs --since-user-initiated", but the logs are then output in the format that is currently the case when I retrieve them with "rain logs". I think the format in which the logs are currently displayed is very good to look at. However, as I said, I have no experience in "Go", so unfortunately I can't edit the code directly to incorporate the logic I explained into the "rain logs" command/function.

I hope this clarifies the approach. If you have any further questions or need additional details, please let me know.

Best regards,
Tommes

Thanks Tommes, this looks to be fairly straightforward.

You're welcome Eric 😃.
If you have any more questions or need further assistance, just let me know.

I'm looking forward to the result!