@@ -31,7 +31,6 @@ import (
3131
3232 "github.com/aws/aws-sdk-go/aws"
3333 "github.com/aws/aws-sdk-go/aws/arn"
34- "github.com/aws/aws-sdk-go/aws/awserr"
3534 "github.com/aws/aws-sdk-go/aws/request"
3635 "github.com/aws/aws-sdk-go/aws/session"
3736 "github.com/aws/aws-sdk-go/service/autoscaling"
@@ -274,58 +273,55 @@ func (s sdk) withTemplate(ctx context.Context, name string, template []byte, reg
274273 return fn (aws .String (string (template )), nil )
275274 }
276275
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+
278283 var configuration * s3.CreateBucketConfiguration
279284 if region != "us-east-1" {
280285 configuration = & s3.CreateBucketConfiguration {
281286 LocationConstraint : aws .String (region ),
282287 }
283288 }
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 {
288290 Bucket : aws .String (bucket ),
289291 CreateBucketConfiguration : configuration ,
290292 })
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 ()
302293 if err != nil {
303294 return "" , err
304295 }
305296
306297 upload , err := s .uploader .UploadWithContext (ctx , & s3manager.UploadInput {
307- Key : aws .String (key ),
298+ Key : aws .String ("template.yaml" ),
308299 Body : bytes .NewReader (template ),
309300 Bucket : aws .String (bucket ),
310- ContentType : aws .String ("application/json " ),
301+ ContentType : aws .String ("application/x-yaml " ),
311302 Tagging : aws .String (name ),
312303 })
313304
314305 if err != nil {
315306 return "" , err
316307 }
317308
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+ }()
329325
330326 return fn (nil , aws .String (upload .Location ))
331327}
0 commit comments