Skip to content

Commit

Permalink
✨ add state transition time to aws ec2 instance
Browse files Browse the repository at this point in the history
  • Loading branch information
vjeffrey committed Sep 21, 2023
1 parent ebe8c80 commit ade9c36
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 1 deletion.
2 changes: 2 additions & 0 deletions providers/aws/resources/aws.lr
Original file line number Diff line number Diff line change
Expand Up @@ -1659,6 +1659,8 @@ private aws.ec2.instance @defaults("arn state") {
privateDnsName string
// Keypair associated with the instance
keypair() aws.ec2.keypair
// Time when the last state transition occurred
stateTransitionTime time
}

// Amazon EC2 Key Pair
Expand Down
12 changes: 12 additions & 0 deletions providers/aws/resources/aws.lr.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions providers/aws/resources/aws.lr.manifest.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -502,6 +502,7 @@ resources:
state: {}
stateReason: {}
stateTransitionReason: {}
stateTransitionTime: {}
tags: {}
vpc: {}
is_private: true
Expand Down
14 changes: 13 additions & 1 deletion providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ package resources
import (
"context"
"fmt"
"regexp"
"strconv"
"strings"
"time"

"github.com/aws/aws-sdk-go-v2/service/ec2"
ec2types "github.com/aws/aws-sdk-go-v2/service/ec2/types"
Expand Down Expand Up @@ -692,7 +694,16 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
if err != nil {
return nil, err
}

var stateTransitionTime time.Time
reg := regexp.MustCompile(`.*\((\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}) GMT\)`)
timeString := reg.FindStringSubmatch(convert.ToString(instance.StateTransitionReason))
if len(timeString) == 2 {
stateTransitionTime, err = time.Parse(time.DateTime, timeString[1])
if err != nil {
log.Error().Err(err).Msg("cannot parse state transition time for ec2 instance")
stateTransitionTime = llx.NeverPastTime
}
}
args := map[string]*llx.RawData{
"platformDetails": llx.StringData(convert.ToString(instance.PlatformDetails)),
"arn": llx.StringData(fmt.Sprintf(ec2InstanceArnPattern, regionVal, conn.AccountId(), convert.ToString(instance.InstanceId))),
Expand All @@ -713,6 +724,7 @@ func (a *mqlAwsEc2) gatherInstanceInfo(instances []ec2types.Reservation, imdsvVe
"launchTime": llx.TimeData(toTime(instance.LaunchTime)),
"privateIp": llx.StringData(convert.ToString(instance.PrivateIpAddress)),
"privateDnsName": llx.StringData(convert.ToString(instance.PrivateDnsName)),
"stateTransitionTime": llx.TimeData(stateTransitionTime),
}

if instance.ImageId != nil {
Expand Down

0 comments on commit ade9c36

Please sign in to comment.