Merge pull request #16290 from hashicorp/jbardin/s3-errors
retry on s3 state upload errors
This commit is contained in:
commit
31912956ce
|
@ -149,32 +149,46 @@ func (c *RemoteClient) Put(data []byte) error {
|
||||||
contentType := "application/json"
|
contentType := "application/json"
|
||||||
contentLength := int64(len(data))
|
contentLength := int64(len(data))
|
||||||
|
|
||||||
i := &s3.PutObjectInput{
|
// we immediately retry on an internal error, as those are usually transient
|
||||||
ContentType: &contentType,
|
maxRetries := 2
|
||||||
ContentLength: &contentLength,
|
for retryCount := 0; ; retryCount++ {
|
||||||
Body: bytes.NewReader(data),
|
i := &s3.PutObjectInput{
|
||||||
Bucket: &c.bucketName,
|
ContentType: &contentType,
|
||||||
Key: &c.path,
|
ContentLength: &contentLength,
|
||||||
}
|
Body: bytes.NewReader(data),
|
||||||
|
Bucket: &c.bucketName,
|
||||||
if c.serverSideEncryption {
|
Key: &c.path,
|
||||||
if c.kmsKeyID != "" {
|
|
||||||
i.SSEKMSKeyId = &c.kmsKeyID
|
|
||||||
i.ServerSideEncryption = aws.String("aws:kms")
|
|
||||||
} else {
|
|
||||||
i.ServerSideEncryption = aws.String("AES256")
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if c.acl != "" {
|
if c.serverSideEncryption {
|
||||||
i.ACL = aws.String(c.acl)
|
if c.kmsKeyID != "" {
|
||||||
}
|
i.SSEKMSKeyId = &c.kmsKeyID
|
||||||
|
i.ServerSideEncryption = aws.String("aws:kms")
|
||||||
|
} else {
|
||||||
|
i.ServerSideEncryption = aws.String("AES256")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
log.Printf("[DEBUG] Uploading remote state to S3: %#v", i)
|
if c.acl != "" {
|
||||||
|
i.ACL = aws.String(c.acl)
|
||||||
|
}
|
||||||
|
|
||||||
_, err := c.s3Client.PutObject(i)
|
log.Printf("[DEBUG] Uploading remote state to S3: %#v", i)
|
||||||
if err != nil {
|
|
||||||
return fmt.Errorf("Failed to upload state: %v", err)
|
_, err := c.s3Client.PutObject(i)
|
||||||
|
if err != nil {
|
||||||
|
if awserr, ok := err.(awserr.Error); ok {
|
||||||
|
if awserr.Code() == s3ErrCodeInternalError {
|
||||||
|
if retryCount > maxRetries {
|
||||||
|
return fmt.Errorf("failed to upload state: %s", err)
|
||||||
|
}
|
||||||
|
log.Println("[WARN] s3 internal error, retrying...")
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return fmt.Errorf("failed to upload state: %s", err)
|
||||||
|
}
|
||||||
|
break
|
||||||
}
|
}
|
||||||
|
|
||||||
sum := md5.Sum(data)
|
sum := md5.Sum(data)
|
||||||
|
|
Loading…
Reference in New Issue