Skip to content

Commit 1007f5b

Browse files
authored
Delete all cortex-created AWS resources when deleting a cluster (#2161)
1 parent 9d05891 commit 1007f5b

File tree

8 files changed

+221
-96
lines changed

8 files changed

+221
-96
lines changed

cli/cmd/cluster.go

Lines changed: 163 additions & 70 deletions
Large diffs are not rendered by default.

dev/minimum_aws_policy.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
"iam:ListInstanceProfiles",
7777
"logs:CreateLogGroup",
7878
"logs:PutLogEvents",
79+
"logs:DeleteLogGroup",
7980
"iam:CreateOpenIDConnectProvider",
8081
"iam:GetOpenIDConnectProvider",
8182
"iam:GetRolePolicy"

docs/clients/cli.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -164,12 +164,12 @@ Usage:
164164
cortex cluster down [flags]
165165
166166
Flags:
167-
-c, --config string path to a cluster configuration file
168-
-n, --name string name of the cluster
169-
-r, --region string aws region of the cluster
170-
-y, --yes skip prompts
171-
--keep-volumes keep cortex provisioned persistent volumes
172-
-h, --help help for down
167+
-c, --config string path to a cluster configuration file
168+
-n, --name string name of the cluster
169+
-r, --region string aws region of the cluster
170+
-y, --yes skip prompts
171+
--keep-aws-resources skip deletion of resources that cortex provisioned on aws (bucket contents, ebs volumes, log group)
172+
-h, --help help for down
173173
```
174174

175175
## cluster export

docs/clusters/management/auth.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ Replace the following placeholders with their respective values in the policy te
141141
"iam:ListInstanceProfiles",
142142
"logs:CreateLogGroup",
143143
"logs:PutLogEvents",
144+
"logs:DeleteLogGroup",
144145
"iam:CreateOpenIDConnectProvider",
145146
"iam:GetOpenIDConnectProvider",
146147
"iam:GetRolePolicy"

docs/clusters/management/delete.md

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,18 @@
44
cortex cluster down
55
```
66

7-
## Delete metadata and log groups
7+
## Bucket Contents
88

9-
Since you may wish to have access to your data after spinning down your cluster, Cortex's bucket, log groups, and
10-
Prometheus volume are not automatically deleted when running `cortex cluster down`.
11-
12-
To delete them:
13-
14-
```bash
15-
# identify the name of your cortex S3 bucket
16-
aws s3 ls
17-
18-
# delete the S3 bucket
19-
aws s3 rb --force s3://<bucket>
20-
21-
# delete the log group (replace <cluster_name> with the name of your cluster, default: cortex)
22-
aws logs describe-log-groups --log-group-name-prefix=<cluster_name> --query logGroups[*].[logGroupName] --output text | xargs -I {} aws logs delete-log-group --log-group-name {}
23-
```
9+
When a Cortex cluster is created, an S3 bucket is created for its internal use. When running `cortex cluster down`, a lifecycle rule is applied to the bucket such that its entire contents are removed within the next 24 hours. You can safely delete the bucket at any time after `cortex cluster down` has finished running.
2410

2511
## Delete Certificates
2612

2713
If you've configured a custom domain for your APIs, you can remove the SSL Certificate and Hosted Zone for the domain by
2814
following these [instructions](../networking/custom-domain.md#cleanup).
2915

30-
## Keep Cortex Volumes
16+
## Keep Cortex Resources
3117

32-
The volumes used by Cortex's Prometheus and Grafana instances are deleted by default on a cluster down operation.
33-
If you want to keep the metrics and dashboards volumes for any reason,
34-
you can pass the `--keep-volumes` flag to the `cortex cluster down` command.
18+
The contents of Cortex's S3 bucket, the EBS volumes (used by Cortex's Prometheus and Grafana instances), and the log group are deleted by default when running `cortex cluster down`. If you want to keep these resources, you can pass the `--keep-aws-resources` flag to the `cortex cluster down` command.
3519

3620
## Troubleshooting
3721

pkg/lib/aws/cloudwatch.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,17 @@ func (c *Client) CreateLogGroup(logGroup string, tags map[string]string) error {
105105
return nil
106106
}
107107

108+
func (c *Client) DeleteLogGroup(logGroup string) error {
109+
_, err := c.CloudWatchLogs().DeleteLogGroup(&cloudwatchlogs.DeleteLogGroupInput{
110+
LogGroupName: aws.String(logGroup),
111+
})
112+
if err != nil {
113+
return errors.Wrap(err, "log group "+logGroup)
114+
}
115+
116+
return nil
117+
}
118+
108119
func (c *Client) TagLogGroup(logGroup string, tagMap map[string]string) error {
109120
tags := map[string]*string{}
110121
for key, value := range tagMap {

pkg/lib/aws/iam.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,3 +209,20 @@ func (c *Client) DeletePolicy(policyARN string) error {
209209
}
210210
return nil
211211
}
212+
213+
func (c *Client) GetPolicyOrNil(policyARN string) (*iam.Policy, error) {
214+
policyOutput, err := c.IAM().GetPolicy(&iam.GetPolicyInput{
215+
PolicyArn: aws.String(policyARN),
216+
})
217+
if err != nil {
218+
if IsErrCode(err, iam.ErrCodeNoSuchEntityException) {
219+
return nil, nil
220+
}
221+
return nil, errors.WithStack(err)
222+
}
223+
224+
if policyOutput != nil {
225+
return policyOutput.Policy, nil
226+
}
227+
return nil, nil
228+
}

pkg/lib/errors/errors.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ limitations under the License.
1717
package errors
1818

1919
import (
20+
"fmt"
2021
"strings"
2122

2223
s "github.com/cortexlabs/cortex/pkg/lib/strings"
@@ -37,3 +38,20 @@ func ErrorUnexpected(msgs ...interface{}) error {
3738
Message: strings.Join(strs, ": "),
3839
})
3940
}
41+
42+
func ListOfErrors(errKind string, shouldPrint bool, errors ...error) error {
43+
var errorsContents string
44+
for i, err := range errors {
45+
if err != nil {
46+
errorsContents += fmt.Sprintf("error #%d: %s\n", i+1, err.Error())
47+
}
48+
}
49+
if errorsContents == "" {
50+
return nil
51+
}
52+
return WithStack(&Error{
53+
Kind: errKind,
54+
Message: errorsContents,
55+
NoPrint: !shouldPrint,
56+
})
57+
}

0 commit comments

Comments
 (0)