message: Handle old events are printed as stack events
diff: @@ -424,9 +424,11 @@ async function deployPoll(cdkOptions, stackStates) {
  .promise();
  const stackEvents = ret.StackEvents || [];
  - // look through all the stack events and find the first relevant
  + // Get the first relevant event
  + if (!stackState.eventsFirstEventAt) {
  + // look through all the stack events and find the MOST RECENT relevant
  // event which is a "Stack" event and has a CREATE, UPDATE or DELETE status
  - const firstRelevantEvent = stackEvents.find((event) => {
  + const firstRelevantEvent = stackEvents.reverse().find((event) => {
  const isStack = "AWS::CloudFormation::Stack";
  const updateIsInProgress = "UPDATE_IN_PROGRESS";
  const createIsInProgress = "CREATE_IN_PROGRESS";
  @@ -447,6 +449,7 @@ async function deployPoll(cdkOptions, stackStates) {
  const updatedDate = eventDate.setSeconds(eventDate.getSeconds() - 5);
  stackState.eventsFirstEventAt = new Date(updatedDate);
  }
  + }
  // Loop through stack events
  const events = stackState.events || [];
  @@ -1113,9 +1116,11 @@ async function destroyPoll(cdkOptions, stackStates) {
  .promise();
  const stackEvents = ret.StackEvents || [];
  + // Get the first relevant event
  + if (!stackState.eventsFirstEventAt) {
  // look through all the stack events and find the first relevant
  // event which is a "Stack" event and has a CREATE, UPDATE or DELETE status
  - const firstRelevantEvent = stackEvents.find((event) => {
  + const firstRelevantEvent = stackEvents.reverse().find((event) => {
  const isStack = "AWS::CloudFormation::Stack";
  const updateIsInProgress = "UPDATE_IN_PROGRESS";
  const createIsInProgress = "CREATE_IN_PROGRESS";
  @@ -1136,20 +1141,32 @@ async function destroyPoll(cdkOptions, stackStates) {
  const updatedDate = eventDate.setSeconds(eventDate.getSeconds() - 5);
  stackState.eventsFirstEventAt = new Date(updatedDate);
  }
  + }
  // Loop through stack events
  const events = stackState.events || [];
  + if (stackState.eventsFirstEventAt) {
  + const eventsFirstEventAtTs = Date.parse(stackState.eventsFirstEventAt);
  +
  stackEvents.reverse().forEach((event) => {
  + // Validate event in range
  const eventInRange =
  - stackState.eventsFirstEventAt &&
  - stackState.eventsFirstEventAt <= event.Timestamp;
  + eventsFirstEventAtTs <= event.Timestamp;
  + if (!eventInRange) {
  + return;
  + }
  +
  + // Validate event not logged
  const eventNotLogged = events.every(
  (loggedEvent) => loggedEvent.eventId !== event.EventId
  );
  + if (!eventNotLogged) {
  + return;
  + }
  +
  + // Keep track of first failed event
  let eventStatus = event.ResourceStatus;
  - if (eventInRange && eventNotLogged) {
  let isFirstError = false;
  - // Keep track of first failed event
  if (
  eventStatus &&
  (eventStatus.endsWith("FAILED") ||
  @@ -1179,8 +1196,8 @@ async function destroyPoll(cdkOptions, stackStates) {
  resourceStatusReason: event.ResourceStatusReason,
  logicalResourceId: event.LogicalResourceId,
  });
  - }
  });
  + }
  stackState.events = events;
  };
  