Skip to content

Commit eaee4df

Browse files
authored
feat(aws): Add support for EC2 Image Last Launched Time (#11224)
#### Summary User requested access to LastLaunchedTime for EC2 Images
1 parent eda26df commit eaee4df

7 files changed

Lines changed: 85 additions & 5 deletions

File tree

plugins/source/aws/docs/tables/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,7 @@
182182
- [aws_ec2_flow_logs](../../../../../website/tables/aws/aws_ec2_flow_logs.md)
183183
- [aws_ec2_hosts](../../../../../website/tables/aws/aws_ec2_hosts.md)
184184
- [aws_ec2_images](../../../../../website/tables/aws/aws_ec2_images.md)
185+
- [aws_ec2_image_last_launched_times](../../../../../website/tables/aws/aws_ec2_image_last_launched_times.md)
185186
- [aws_ec2_image_launch_permissions](../../../../../website/tables/aws/aws_ec2_image_launch_permissions.md)
186187
- [aws_ec2_instance_statuses](../../../../../website/tables/aws/aws_ec2_instance_statuses.md)
187188
- [aws_ec2_instance_types](../../../../../website/tables/aws/aws_ec2_instance_types.md)
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package ec2
2+
3+
import (
4+
"context"
5+
6+
"github.com/apache/arrow/go/v13/arrow"
7+
"github.com/aws/aws-sdk-go-v2/aws"
8+
"github.com/aws/aws-sdk-go-v2/service/ec2"
9+
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
10+
"github.com/cloudquery/cloudquery/plugins/source/aws/client"
11+
"github.com/cloudquery/plugin-sdk/v3/schema"
12+
)
13+
14+
func imageAttributesLastLaunchTime() *schema.Table {
15+
return &schema.Table{
16+
Name: "aws_ec2_image_last_launched_times",
17+
Description: `https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImageAttribute.html.
18+
The date and time, in ISO 8601 date-time format, when the AMI was last used to launch an EC2 instance. When the AMI is used to launch an instance, there is a 24-hour delay before that usage is reported.`,
19+
Resolver: fetchEc2ImageAttributeLastLaunchTime,
20+
Columns: []schema.Column{
21+
{
22+
Name: "image_arn",
23+
Type: arrow.BinaryTypes.String,
24+
Resolver: schema.ParentColumnResolver("arn"),
25+
PrimaryKey: true,
26+
},
27+
{
28+
Name: "last_launched_time",
29+
Type: arrow.FixedWidthTypes.Timestamp_us,
30+
Resolver: schema.PathResolver("Value"),
31+
},
32+
},
33+
}
34+
}
35+
36+
func fetchEc2ImageAttributeLastLaunchTime(ctx context.Context, meta schema.ClientMeta, parent *schema.Resource, res chan<- any) error {
37+
c := meta.(*client.Client)
38+
p := parent.Item.(types.Image)
39+
if aws.ToString(p.OwnerId) != c.AccountID {
40+
return nil
41+
}
42+
svc := c.Services().Ec2
43+
output, err := svc.DescribeImageAttribute(ctx, &ec2.DescribeImageAttributeInput{
44+
Attribute: types.ImageAttributeNameLastLaunchedTime,
45+
ImageId: p.ImageId,
46+
}, func(options *ec2.Options) {
47+
options.Region = c.Region
48+
})
49+
if err != nil {
50+
return err
51+
}
52+
res <- output.LastLaunchedTime
53+
return nil
54+
}

plugins/source/aws/resources/services/ec2/images.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ func Images() *schema.Table {
4141
},
4242
Relations: []*schema.Table{
4343
imageAttributesLaunchPermissions(),
44+
imageAttributesLastLaunchTime(),
4445
},
4546
}
4647
}

plugins/source/aws/resources/services/ec2/images_mock_test.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,19 +57,18 @@ func buildEc2ImagesMock(t *testing.T, ctrl *gomock.Controller) client.Services {
5757
if err := faker.FakeObject(&lp); err != nil {
5858
t.Fatal(err)
5959
}
60+
6061
m.EXPECT().DescribeImageAttribute(
6162
gomock.Any(),
62-
&ec2.DescribeImageAttributeInput{
63-
Attribute: types.ImageAttributeNameLaunchPermission,
64-
ImageId: g.ImageId,
65-
},
63+
gomock.Any(),
6664
gomock.Any(),
6765
).Return(
6866
&ec2.DescribeImageAttributeOutput{
6967
LaunchPermissions: []types.LaunchPermission{lp},
68+
LastLaunchedTime: &types.AttributeValue{Value: &creationDate},
7069
},
7170
nil,
72-
)
71+
).Times(2)
7372

7473
return services
7574
}

website/pages/docs/plugins/sources/aws/tables.md

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Table: aws_ec2_image_last_launched_times
2+
3+
This table shows data for Amazon Elastic Compute Cloud (EC2) Image Last Launched Times.
4+
5+
https://docs.aws.amazon.com/AWSEC2/latest/APIReference/API_DescribeImageAttribute.html.
6+
The date and time, in ISO 8601 date-time format, when the AMI was last used to launch an EC2 instance. When the AMI is used to launch an instance, there is a 24-hour delay before that usage is reported.
7+
8+
The primary key for this table is **image_arn**.
9+
10+
## Relations
11+
12+
This table depends on [aws_ec2_images](aws_ec2_images).
13+
14+
## Columns
15+
16+
| Name | Type |
17+
| ------------- | ------------- |
18+
|_cq_source_name|`utf8`|
19+
|_cq_sync_time|`timestamp[us, tz=UTC]`|
20+
|_cq_id|`uuid`|
21+
|_cq_parent_id|`uuid`|
22+
|image_arn (PK)|`utf8`|
23+
|last_launched_time|`timestamp[us, tz=UTC]`|

website/tables/aws/aws_ec2_images.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ The composite primary key for this table is (**account_id**, **region**, **arn**
99
## Relations
1010

1111
The following tables depend on aws_ec2_images:
12+
- [aws_ec2_image_last_launched_times](aws_ec2_image_last_launched_times)
1213
- [aws_ec2_image_launch_permissions](aws_ec2_image_launch_permissions)
1314

1415
## Columns

0 commit comments

Comments
 (0)