/
ebs_snapshot_detail.sp
322 lines (280 loc) · 6.61 KB
/
ebs_snapshot_detail.sp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
dashboard "ebs_snapshot_detail" {
title = "AWS EBS Snapshot Detail"
documentation = file("./dashboards/ebs/docs/ebs_snapshot_detail.md")
tags = merge(local.ebs_common_tags, {
type = "Detail"
})
input "ebs_snapshot_id" {
title = "Select a snapshot:"
query = query.ebs_snapshot_input
width = 4
}
container {
card {
width = 3
query = query.ebs_snapshot_state
args = [self.input.ebs_snapshot_id.value]
}
card {
width = 3
query = query.ebs_snapshot_storage
args = [self.input.ebs_snapshot_id.value]
}
card {
width = 3
query = query.ebs_snapshot_encryption
args = [self.input.ebs_snapshot_id.value]
}
}
with "target_ebs_volumes_for_ebs_snapshot" {
query = query.target_ebs_volumes_for_ebs_snapshot
args = [self.input.ebs_snapshot_id.value]
}
with "source_ebs_volumes_for_ebs_snapshot" {
query = query.source_ebs_volumes_for_ebs_snapshot
args = [self.input.ebs_snapshot_id.value]
}
with "ec2_amis_for_ebs_snapshot" {
query = query.ec2_amis_for_ebs_snapshot
args = [self.input.ebs_snapshot_id.value]
}
with "ec2_launch_configurations_for_ebs_snapshot" {
query = query.ec2_launch_configurations_for_ebs_snapshot
args = [self.input.ebs_snapshot_id.value]
}
with "kms_keys_for_ebs_snapshot" {
query = query.kms_keys_for_ebs_snapshot
args = [self.input.ebs_snapshot_id.value]
}
container {
graph {
title = "Relationships"
type = "graph"
direction = "TD"
node {
base = node.ebs_snapshot
args = {
ebs_snapshot_ids = [self.input.ebs_snapshot_id.value]
}
}
node {
base = node.ebs_volume
args = {
ebs_volume_arns = with.target_ebs_volumes_for_ebs_snapshot.rows[*].volume_arn
}
}
node {
base = node.ebs_volume
args = {
ebs_volume_arns = with.source_ebs_volumes_for_ebs_snapshot.rows[*].volume_arn
}
}
node {
base = node.ec2_ami
args = {
ec2_ami_image_ids = with.ec2_amis_for_ebs_snapshot.rows[*].image_id
}
}
node {
base = node.ec2_launch_configuration
args = {
ec2_launch_configuration_arns = with.ec2_launch_configurations_for_ebs_snapshot.rows[*].launch_configuration_arn
}
}
node {
base = node.kms_key
args = {
kms_key_arns = with.kms_keys_for_ebs_snapshot.rows[*].key_arn
}
}
edge {
base = edge.ebs_snapshot_to_ec2_ami
args = {
ebs_snapshot_ids = [self.input.ebs_snapshot_id.value]
}
}
edge {
base = edge.ebs_snapshot_to_kms_key
args = {
ebs_snapshot_ids = [self.input.ebs_snapshot_id.value]
}
}
edge {
base = edge.ebs_snapshot_to_ebs_volume
args = {
ebs_snapshot_ids = [self.input.ebs_snapshot_id.value]
}
}
edge {
base = edge.ebs_volume_to_ebs_snapshot
args = {
ebs_volume_arns = with.source_ebs_volumes_for_ebs_snapshot.rows[*].volume_arn
}
}
edge {
base = edge.ec2_launch_configuration_to_ebs_snapshot
args = {
ebs_snapshot_ids = [self.input.ebs_snapshot_id.value]
}
}
}
}
container {
table {
title = "Overview"
type = "line"
width = 3
query = query.ebs_snapshot_overview
args = [self.input.ebs_snapshot_id.value]
}
table {
title = "Tags"
width = 3
query = query.ebs_snapshot_tags
args = [self.input.ebs_snapshot_id.value]
}
}
}
# Input queries
query "ebs_snapshot_input" {
sql = <<-EOQ
select
title as label,
snapshot_id as value,
json_build_object(
'account_id', account_id,
'region', region,
'volume_id', volume_id,
'state', state
) as tags
from
aws_ebs_snapshot
order by
title;
EOQ
}
# With queries
query "target_ebs_volumes_for_ebs_snapshot" {
sql = <<-EOQ
select
v.arn as volume_arn
from
aws_ebs_volume as v
where
v.snapshot_id = $1;
EOQ
}
query "source_ebs_volumes_for_ebs_snapshot" {
sql = <<-EOQ
select
v.arn as volume_arn
from
aws_ebs_snapshot as s,
aws_ebs_volume as v
where
s.volume_id = v.volume_id
and s.snapshot_id = $1;
EOQ
}
query "ec2_amis_for_ebs_snapshot" {
sql = <<-EOQ
select
images.image_id as image_id
from
aws_ec2_ami as images,
jsonb_array_elements(images.block_device_mappings) as bdm,
aws_ebs_snapshot as s
where
bdm -> 'Ebs' is not null
and bdm -> 'Ebs' ->> 'SnapshotId' = s.snapshot_id
and s.snapshot_id = $1;
EOQ
}
query "ec2_launch_configurations_for_ebs_snapshot" {
sql = <<-EOQ
select
launch_config.launch_configuration_arn as launch_configuration_arn
from
aws_ec2_launch_configuration as launch_config,
jsonb_array_elements(launch_config.block_device_mappings) as bdm,
aws_ebs_snapshot as s
where
bdm -> 'Ebs' ->> 'SnapshotId' = s.snapshot_id
and s.snapshot_id = $1;
EOQ
}
query "kms_keys_for_ebs_snapshot" {
sql = <<-EOQ
select
kms_key_id as key_arn
from
aws_ebs_snapshot
where
kms_key_id is not null
and snapshot_id = $1
EOQ
}
# Card queries
query "ebs_snapshot_storage" {
sql = <<-EOQ
select
'Storage (GB)' as label,
volume_size as value
from
aws_ebs_snapshot
where
snapshot_id = $1;
EOQ
}
query "ebs_snapshot_encryption" {
sql = <<-EOQ
select
'Encryption' as label,
case when encrypted then 'Enabled' else 'Disabled' end as value,
case when encrypted then 'ok' else 'alert' end as type
from
aws_ebs_snapshot
where
snapshot_id = $1;
EOQ
}
query "ebs_snapshot_state" {
sql = <<-EOQ
select
'State' as label,
initcap(state) as value
from
aws_ebs_snapshot
where
snapshot_id = $1;
EOQ
}
# Other detail page queries
query "ebs_snapshot_overview" {
sql = <<-EOQ
select
snapshot_id as "Snapshot ID",
title as "Title",
region as "Region",
account_id as "Account ID",
arn as "ARN"
from
aws_ebs_snapshot
where
snapshot_id = $1
EOQ
}
query "ebs_snapshot_tags" {
sql = <<-EOQ
select
tag ->> 'Key' as "Key",
tag ->> 'Value' as "Value"
from
aws_ebs_snapshot,
jsonb_array_elements(tags_src) as tag
where
snapshot_id = $1
order by
tag ->> 'Key';
EOQ
}