@@ -25,6 +25,12 @@ provider "aws" {
25
25
}
26
26
27
27
# Variables
28
+ variable "test_mode" {
29
+ description = " Set to true when running tests to skip AWS API calls"
30
+ type = bool
31
+ default = false
32
+ }
33
+
28
34
variable "instance_id" {
29
35
description = " The EC2 instance ID to create snapshots from"
30
36
type = string
@@ -96,11 +102,11 @@ data "coder_parameter" "use_previous_snapshot" {
96
102
description = " Start with a fresh instance"
97
103
}
98
104
dynamic "option" {
99
- for_each = data . aws_ami_ids . workspace_snapshots . ids
105
+ for_each = local . workspace_snapshot_ids
100
106
content {
101
- name = " ${ data . aws_ami . snapshot_info [option . value ]. name } (${ formatdate (" YYYY-MM-DD hh:mm" , data . aws_ami . snapshot_info [option . value ]. creation_date )} )"
107
+ name = var . test_mode ? " Test Snapshot " : " ${ local . snapshot_info [option . value ]. name } (${ formatdate (" YYYY-MM-DD hh:mm" , local . snapshot_info [option . value ]. creation_date )} )"
102
108
value = option. value
103
- description = data . aws_ami . snapshot_info [option . value ]. description
109
+ description = var . test_mode ? " Test Description " : local . snapshot_info [option . value ]. description
104
110
}
105
111
}
106
112
}
@@ -109,8 +115,17 @@ data "coder_parameter" "use_previous_snapshot" {
109
115
data "coder_workspace" "me" {}
110
116
data "coder_workspace_owner" "me" {}
111
117
118
+ # Local values to handle test mode
119
+ locals {
120
+ workspace_snapshot_ids = var. test_mode ? [] : data. aws_ami_ids . workspace_snapshots [0 ]. ids
121
+ snapshot_info = var. test_mode ? {} : {
122
+ for ami_id in local . workspace_snapshot_ids : ami_id => data . aws_ami . snapshot_info [ami_id ]
123
+ }
124
+ }
125
+
112
126
# Retrieve existing snapshots for this workspace
113
127
data "aws_ami_ids" "workspace_snapshots" {
128
+ count = var. test_mode ? 0 : 1
114
129
owners = [" self" ]
115
130
116
131
filter {
@@ -136,7 +151,7 @@ data "aws_ami_ids" "workspace_snapshots" {
136
151
137
152
# Get detailed information about each snapshot
138
153
data "aws_ami" "snapshot_info" {
139
- for_each = toset (data . aws_ami_ids . workspace_snapshots . ids )
154
+ for_each = toset (local . workspace_snapshot_ids )
140
155
owners = [" self" ]
141
156
142
157
filter {
@@ -179,7 +194,7 @@ resource "aws_ami_from_instance" "workspace_snapshot" {
179
194
180
195
# Optional: Data Lifecycle Manager policy for automated cleanup
181
196
resource "aws_dlm_lifecycle_policy" "workspace_snapshots" {
182
- count = var. enable_dlm_cleanup ? 1 : 0
197
+ count = var. enable_dlm_cleanup && ! var . test_mode ? 1 : 0
183
198
description = " Lifecycle policy for Coder workspace AMI snapshots"
184
199
execution_role_arn = var. dlm_role_arn
185
200
state = " ENABLED"
@@ -227,17 +242,17 @@ output "snapshot_ami_id" {
227
242
228
243
output "available_snapshots" {
229
244
description = " List of available snapshot AMI IDs for this workspace"
230
- value = data . aws_ami_ids . workspace_snapshots . ids
245
+ value = local . workspace_snapshot_ids
231
246
}
232
247
233
248
output "snapshot_info" {
234
249
description = " Detailed information about available snapshots"
235
- value = {
236
- for ami_id in data . aws_ami_ids . workspace_snapshots . ids : ami_id => {
237
- name = data.aws_ami .snapshot_info[ami_id].name
238
- description = data.aws_ami .snapshot_info[ami_id].description
239
- created_date = data.aws_ami .snapshot_info[ami_id].creation_date
240
- tags = data.aws_ami .snapshot_info[ami_id].tags
250
+ value = var . test_mode ? {} : {
251
+ for ami_id in local . workspace_snapshot_ids : ami_id => {
252
+ name = local .snapshot_info[ami_id].name
253
+ description = local .snapshot_info[ami_id].description
254
+ created_date = local .snapshot_info[ami_id].creation_date
255
+ tags = local .snapshot_info[ami_id].tags
241
256
}
242
257
}
243
258
}
0 commit comments