9
9
"github.com/aws/aws-sdk-go/aws/arn"
10
10
"github.com/aws/aws-sdk-go/service/ec2"
11
11
"github.com/hashicorp/aws-sdk-go-base/v2/awsv1shim/v2/tfawserr"
12
- "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
13
12
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
14
13
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
15
14
"github.com/hashicorp/terraform-provider-aws/internal/conns"
@@ -24,6 +23,7 @@ func ResourceEBSSnapshot() *schema.Resource {
24
23
Read : resourceEBSSnapshotRead ,
25
24
Update : resourceEBSSnapshotUpdate ,
26
25
Delete : resourceEBSSnapshotDelete ,
26
+
27
27
Importer : & schema.ResourceImporter {
28
28
State : schema .ImportStatePassthrough ,
29
29
},
@@ -76,13 +76,10 @@ func ResourceEBSSnapshot() *schema.Resource {
76
76
Optional : true ,
77
77
},
78
78
"storage_tier" : {
79
- Type : schema .TypeString ,
80
- Optional : true ,
81
- Computed : true ,
82
- ValidateFunc : validation .Any (
83
- validation .StringInSlice (ec2 .TargetStorageTier_Values (), false ),
84
- validation .StringInSlice ([]string {"standard" }, false ), //Enum slice does not include `standard` type.
85
- ),
79
+ Type : schema .TypeString ,
80
+ Optional : true ,
81
+ Computed : true ,
82
+ ValidateFunc : validation .StringInSlice (append (ec2 .TargetStorageTier_Values (), TargetStorageTierStandard ), false ),
86
83
},
87
84
"tags" : tftags .TagsSchema (),
88
85
"tags_all" : tftags .TagsSchemaComputed (),
@@ -108,45 +105,43 @@ func resourceEBSSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
108
105
defaultTagsConfig := meta .(* conns.AWSClient ).DefaultTagsConfig
109
106
tags := defaultTagsConfig .MergeTags (tftags .New (d .Get ("tags" ).(map [string ]interface {})))
110
107
111
- request := & ec2. CreateSnapshotInput {
112
- VolumeId : aws . String ( d . Get ( "volume_id" ).( string )),
108
+ volumeID := d . Get ( "volume_id" ).( string )
109
+ input := & ec2. CreateSnapshotInput {
113
110
TagSpecifications : tagSpecificationsFromKeyValueTags (tags , ec2 .ResourceTypeSnapshot ),
111
+ VolumeId : aws .String (volumeID ),
114
112
}
113
+
115
114
if v , ok := d .GetOk ("description" ); ok {
116
- request .Description = aws .String (v .(string ))
115
+ input .Description = aws .String (v .(string ))
117
116
}
118
117
119
118
if v , ok := d .GetOk ("outpost_arn" ); ok {
120
- request .OutpostArn = aws .String (v .(string ))
119
+ input .OutpostArn = aws .String (v .(string ))
121
120
}
122
121
123
- var res * ec2.Snapshot
124
- err := resource .Retry (1 * time .Minute , func () * resource.RetryError {
125
- var err error
126
- res , err = conn .CreateSnapshot (request )
127
-
128
- if tfawserr .ErrMessageContains (err , "SnapshotCreationPerVolumeRateExceeded" , "The maximum per volume CreateSnapshot request rate has been exceeded" ) {
129
- return resource .RetryableError (err )
130
- }
131
-
132
- if err != nil {
133
- return resource .NonRetryableError (err )
134
- }
122
+ log .Printf ("[DEBUG] Creating EBS Snapshot: %s" , input )
123
+ outputRaw , err := tfresource .RetryWhenAWSErrMessageContains (1 * time .Minute ,
124
+ func () (interface {}, error ) {
125
+ return conn .CreateSnapshot (input )
126
+ },
127
+ errCodeSnapshotCreationPerVolumeRateExceeded , "The maximum per volume CreateSnapshot request rate has been exceeded" )
135
128
136
- return nil
137
- })
138
- if tfresource .TimedOut (err ) {
139
- res , err = conn .CreateSnapshot (request )
140
- }
141
129
if err != nil {
142
- return fmt .Errorf ("error creating EBS Snapshot: %w" , err )
130
+ return fmt .Errorf ("creating EBS Snapshot (%s) : %w" , volumeID , err )
143
131
}
144
132
145
- d .SetId (aws .StringValue (res .SnapshotId ))
133
+ d .SetId (aws .StringValue (outputRaw .(* ec2.Snapshot ).SnapshotId ))
134
+
135
+ _ , err = tfresource .RetryWhenAWSErrCodeEquals (d .Timeout (schema .TimeoutCreate ),
136
+ func () (interface {}, error ) {
137
+ return nil , conn .WaitUntilSnapshotCompleted (& ec2.DescribeSnapshotsInput {
138
+ SnapshotIds : aws .StringSlice ([]string {d .Id ()}),
139
+ })
140
+ },
141
+ errCodeResourceNotReady )
146
142
147
- err = resourceEBSSnapshotWaitForAvailable (d , conn )
148
143
if err != nil {
149
- return err
144
+ return fmt . Errorf ( "waiting for EBS Snapshot (%s) create: %w" , d . Id (), err )
150
145
}
151
146
152
147
if v , ok := d .GetOk ("storage_tier" ); ok && v .(string ) == ec2 .TargetStorageTierArchive {
@@ -156,12 +151,11 @@ func resourceEBSSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
156
151
})
157
152
158
153
if err != nil {
159
- return fmt .Errorf ("error setting EBS Snapshot (%s) Storage Tier: %w" , d .Id (), err )
154
+ return fmt .Errorf ("updating EBS Snapshot (%s) Storage Tier: %w" , d .Id (), err )
160
155
}
161
156
162
- _ , err = WaitEBSSnapshotTierArchive (conn , d .Id ())
163
- if err != nil {
164
- return fmt .Errorf ("Error waiting for EBS Snapshot (%s) Storage Tier to be archived: %w" , d .Id (), err )
157
+ if _ , err := waitEBSSnapshotTierArchive (conn , d .Id (), ebsSnapshotArchivedTimeout ); err != nil {
158
+ return fmt .Errorf ("waiting for EBS Snapshot (%s) Storage Tier archive: %w" , d .Id (), err )
165
159
}
166
160
}
167
161
@@ -173,18 +167,25 @@ func resourceEBSSnapshotRead(d *schema.ResourceData, meta interface{}) error {
173
167
defaultTagsConfig := meta .(* conns.AWSClient ).DefaultTagsConfig
174
168
ignoreTagsConfig := meta .(* conns.AWSClient ).IgnoreTagsConfig
175
169
176
- snapshot , err := FindSnapshotById (conn , d .Id ())
170
+ snapshot , err := FindSnapshotByID (conn , d .Id ())
177
171
178
172
if ! d .IsNewResource () && tfresource .NotFound (err ) {
179
- log .Printf ("[WARN] EBS Snapshot (%s) Not found - removing from state" , d .Id ())
173
+ log .Printf ("[WARN] EBS Snapshot %s not found, removing from state" , d .Id ())
180
174
d .SetId ("" )
181
175
return nil
182
176
}
183
177
184
178
if err != nil {
185
- return fmt .Errorf ("error reading EBS Snapshot (%s): %w" , d .Id (), err )
179
+ return fmt .Errorf ("reading EBS Snapshot (%s): %w" , d .Id (), err )
186
180
}
187
181
182
+ arn := arn.ARN {
183
+ Partition : meta .(* conns.AWSClient ).Partition ,
184
+ Service : ec2 .ServiceName ,
185
+ Region : meta .(* conns.AWSClient ).Region ,
186
+ Resource : fmt .Sprintf ("snapshot/%s" , d .Id ()),
187
+ }.String ()
188
+ d .Set ("arn" , arn )
188
189
d .Set ("data_encryption_key_id" , snapshot .DataEncryptionKeyId )
189
190
d .Set ("description" , snapshot .Description )
190
191
d .Set ("encrypted" , snapshot .Encrypted )
@@ -200,43 +201,32 @@ func resourceEBSSnapshotRead(d *schema.ResourceData, meta interface{}) error {
200
201
201
202
//lintignore:AWSR002
202
203
if err := d .Set ("tags" , tags .RemoveDefaultConfig (defaultTagsConfig ).Map ()); err != nil {
203
- return fmt .Errorf ("error setting tags: %w" , err )
204
+ return fmt .Errorf ("setting tags: %w" , err )
204
205
}
205
206
206
207
if err := d .Set ("tags_all" , tags .Map ()); err != nil {
207
- return fmt .Errorf ("error setting tags_all: %w" , err )
208
+ return fmt .Errorf ("setting tags_all: %w" , err )
208
209
}
209
210
210
- snapshotArn := arn.ARN {
211
- Partition : meta .(* conns.AWSClient ).Partition ,
212
- Region : meta .(* conns.AWSClient ).Region ,
213
- Resource : fmt .Sprintf ("snapshot/%s" , d .Id ()),
214
- Service : ec2 .ServiceName ,
215
- }.String ()
216
-
217
- d .Set ("arn" , snapshotArn )
218
-
219
211
return nil
220
212
}
221
213
222
214
func resourceEBSSnapshotUpdate (d * schema.ResourceData , meta interface {}) error {
223
215
conn := meta .(* conns.AWSClient ).EC2Conn
224
216
225
217
if d .HasChange ("storage_tier" ) {
226
- tier := d .Get ("storage_tier" ).(string )
227
- if tier == ec2 .TargetStorageTierArchive {
218
+ if tier := d .Get ("storage_tier" ).(string ); tier == ec2 .TargetStorageTierArchive {
228
219
_ , err := conn .ModifySnapshotTier (& ec2.ModifySnapshotTierInput {
229
220
SnapshotId : aws .String (d .Id ()),
230
221
StorageTier : aws .String (tier ),
231
222
})
232
223
233
224
if err != nil {
234
- return fmt .Errorf ("error upadating EBS Snapshot (%s) Storage Tier: %w" , d .Id (), err )
225
+ return fmt .Errorf ("updating EBS Snapshot (%s) Storage Tier: %w" , d .Id (), err )
235
226
}
236
227
237
- _ , err = WaitEBSSnapshotTierArchive (conn , d .Id ())
238
- if err != nil {
239
- return fmt .Errorf ("Error waiting for EBS Snapshot (%s) Storage Tier to be archived: %w" , d .Id (), err )
228
+ if _ , err := waitEBSSnapshotTierArchive (conn , d .Id (), ebsSnapshotArchivedTimeout ); err != nil {
229
+ return fmt .Errorf ("waiting for EBS Snapshot (%s) Storage Tier archive: %w" , d .Id (), err )
240
230
}
241
231
} else {
242
232
input := & ec2.RestoreSnapshotTierInput {
@@ -255,15 +245,16 @@ func resourceEBSSnapshotUpdate(d *schema.ResourceData, meta interface{}) error {
255
245
_ , err := conn .RestoreSnapshotTier (input )
256
246
257
247
if err != nil {
258
- return fmt .Errorf ("error restoring EBS Snapshot (%s): %w" , d .Id (), err )
248
+ return fmt .Errorf ("restoring EBS Snapshot (%s): %w" , d .Id (), err )
259
249
}
260
250
}
261
251
}
262
252
263
253
if d .HasChange ("tags_all" ) {
264
254
o , n := d .GetChange ("tags_all" )
255
+
265
256
if err := UpdateTags (conn , d .Id (), o , n ); err != nil {
266
- return fmt .Errorf ("error updating tags: %w" , err )
257
+ return fmt .Errorf ("updating EBS Snapshot (%s) tags: %w" , d . Id () , err )
267
258
}
268
259
}
269
260
@@ -280,33 +271,13 @@ func resourceEBSSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
280
271
})
281
272
}, errCodeInvalidSnapshotInUse )
282
273
283
- if err != nil {
284
- return fmt . Errorf ( "error deleting EBS Snapshot (%s): %w" , d . Id (), err )
274
+ if tfawserr . ErrCodeEquals ( err , errCodeInvalidSnapshotNotFound ) {
275
+ return nil
285
276
}
286
277
287
- return nil
288
- }
289
-
290
- func resourceEBSSnapshotWaitForAvailable (d * schema.ResourceData , conn * ec2.EC2 ) error {
291
- log .Printf ("Waiting for Snapshot %s to become available..." , d .Id ())
292
- input := & ec2.DescribeSnapshotsInput {
293
- SnapshotIds : []* string {aws .String (d .Id ())},
294
- }
295
- err := resource .Retry (d .Timeout (schema .TimeoutCreate ), func () * resource.RetryError {
296
- err := conn .WaitUntilSnapshotCompleted (input )
297
- if err == nil {
298
- return nil
299
- }
300
- if tfawserr .ErrCodeEquals (err , "ResourceNotReady" ) {
301
- return resource .RetryableError (fmt .Errorf ("EBS Snapshot - waiting for snapshot to become available" ))
302
- }
303
- return resource .NonRetryableError (err )
304
- })
305
- if tfresource .TimedOut (err ) {
306
- err = conn .WaitUntilSnapshotCompleted (input )
307
- }
308
278
if err != nil {
309
- return fmt .Errorf ("Error waiting for EBS snapshot to complete : %w" , err )
279
+ return fmt .Errorf ("deleting EBS Snapshot (%s) : %w" , d . Id () , err )
310
280
}
281
+
311
282
return nil
312
283
}
0 commit comments