@@ -31,7 +31,6 @@ import (
31
31
32
32
"github.com/aws/aws-sdk-go/aws"
33
33
"github.com/aws/aws-sdk-go/aws/arn"
34
- "github.com/aws/aws-sdk-go/aws/awserr"
35
34
"github.com/aws/aws-sdk-go/aws/request"
36
35
"github.com/aws/aws-sdk-go/aws/session"
37
36
"github.com/aws/aws-sdk-go/service/autoscaling"
@@ -274,58 +273,55 @@ func (s sdk) withTemplate(ctx context.Context, name string, template []byte, reg
274
273
return fn (aws .String (string (template )), nil )
275
274
}
276
275
277
- logrus .Debug ("Create s3 bucket to store cloudformation template" )
276
+ key , err := uuid .GenerateUUID ()
277
+ if err != nil {
278
+ return "" , err
279
+ }
280
+ bucket := "com.docker.compose." + key
281
+ logrus .Debugf ("Create s3 bucket %q to store cloudformation template" , bucket )
282
+
278
283
var configuration * s3.CreateBucketConfiguration
279
284
if region != "us-east-1" {
280
285
configuration = & s3.CreateBucketConfiguration {
281
286
LocationConstraint : aws .String (region ),
282
287
}
283
288
}
284
- // CloudFormation will only allow URL from a same-region bucket
285
- // to avoid conflicts we suffix bucket name by region, so we can create comparable buckets in other regions.
286
- bucket := "com.docker.compose." + region
287
- _ , err := s .S3 .CreateBucket (& s3.CreateBucketInput {
289
+ _ , err = s .S3 .CreateBucket (& s3.CreateBucketInput {
288
290
Bucket : aws .String (bucket ),
289
291
CreateBucketConfiguration : configuration ,
290
292
})
291
- if err != nil {
292
- ae , ok := err .(awserr.Error )
293
- if ! ok {
294
- return "" , err
295
- }
296
- if ae .Code () != s3 .ErrCodeBucketAlreadyOwnedByYou {
297
- return "" , err
298
- }
299
- }
300
-
301
- key , err := uuid .GenerateUUID ()
302
293
if err != nil {
303
294
return "" , err
304
295
}
305
296
306
297
upload , err := s .uploader .UploadWithContext (ctx , & s3manager.UploadInput {
307
- Key : aws .String (key ),
298
+ Key : aws .String ("template.yaml" ),
308
299
Body : bytes .NewReader (template ),
309
300
Bucket : aws .String (bucket ),
310
- ContentType : aws .String ("application/json " ),
301
+ ContentType : aws .String ("application/x-yaml " ),
311
302
Tagging : aws .String (name ),
312
303
})
313
304
314
305
if err != nil {
315
306
return "" , err
316
307
}
317
308
318
- defer s .S3 .DeleteObjects (& s3.DeleteObjectsInput { //nolint: errcheck
319
- Bucket : aws .String (bucket ),
320
- Delete : & s3.Delete {
321
- Objects : []* s3.ObjectIdentifier {
322
- {
323
- Key : aws .String (key ),
324
- VersionId : upload .VersionID ,
325
- },
326
- },
327
- },
328
- })
309
+ defer func () {
310
+ _ , err := s .S3 .DeleteObjectWithContext (ctx , & s3.DeleteObjectInput {
311
+ Bucket : aws .String (bucket ),
312
+ Key : aws .String ("template.yaml" ),
313
+ VersionId : upload .VersionID ,
314
+ })
315
+ if err != nil {
316
+ logrus .Warnf ("Failed to remove S3 bucket: %s" , err )
317
+ }
318
+ _ , err = s .S3 .DeleteBucketWithContext (ctx , & s3.DeleteBucketInput {
319
+ Bucket : aws .String (bucket ),
320
+ })
321
+ if err != nil {
322
+ logrus .Warnf ("Failed to remove S3 bucket: %s" , err )
323
+ }
324
+ }()
329
325
330
326
return fn (nil , aws .String (upload .Location ))
331
327
}
0 commit comments