|
| 1 | +/* |
| 2 | + * Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"). |
| 5 | + * You may not use this file except in compliance with the License. |
| 6 | + * A copy of the License is located at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * or in the "license" file accompanying this file. This file is distributed |
| 11 | + * on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 12 | + * express or implied. See the License for the specific language governing |
| 13 | + * permissions and limitations under the License. |
| 14 | + */ |
| 15 | + |
| 16 | +package main |
| 17 | + |
| 18 | +import ( |
| 19 | + "github.com/aws/aws-sdk-go/aws" |
| 20 | + "github.com/aws/aws-sdk-go/aws/session" |
| 21 | + "github.com/aws/aws-sdk-go/service/ec2" |
| 22 | + "github.com/aws/aws-sdk-go/service/elbv2" |
| 23 | + "log" |
| 24 | + "strings" |
| 25 | + "time" |
| 26 | +) |
| 27 | + |
| 28 | +const ( |
| 29 | + containLbName = "aoc-lb" |
| 30 | + pastDayDelete = 5 |
| 31 | + pastDayDeleteCalculation = -1 * time.Hour * 24 * pastDayDelete //Currently, deleting resources over 5 days |
| 32 | +) |
| 33 | + |
| 34 | +func main() { |
| 35 | + log.Printf("Beging terminating EC2 Instances") |
| 36 | + terminateEc2Instances() |
| 37 | + |
| 38 | + log.Printf("Begin destroy Load Balancer resources") |
| 39 | + destroyLoadBalancerResource() |
| 40 | + |
| 41 | + log.Printf("Finish destroy AWS resources") |
| 42 | +} |
| 43 | + |
| 44 | +func terminateEc2Instances() { |
| 45 | + // set up aws go sdk ec2 client |
| 46 | + testSession, err := session.NewSession() |
| 47 | + |
| 48 | + if err != nil { |
| 49 | + log.Fatalf("Error creating session %v", err) |
| 50 | + } |
| 51 | + |
| 52 | + ec2client := ec2.New(testSession) |
| 53 | + |
| 54 | + // Get list of instance |
| 55 | + //State filter |
| 56 | + instanceStateFilter := ec2.Filter{Name: aws.String("instance-state-name"), Values: []*string{aws.String("running")}} |
| 57 | + |
| 58 | + //Tag filter |
| 59 | + instanceTagFilter := ec2.Filter{Name: aws.String("tag:Name"), Values: []*string{aws.String("Integ-test-Sample-App"), aws.String("Integ-test-aoc")}} |
| 60 | + |
| 61 | + //get instances to delete |
| 62 | + var deleteInstanceIds []*string |
| 63 | + var nextToken *string |
| 64 | + for { |
| 65 | + describeInstancesInput := ec2.DescribeInstancesInput{Filters: []*ec2.Filter{&instanceStateFilter, &instanceTagFilter}, NextToken: nextToken} |
| 66 | + describeInstancesOutput, err := ec2client.DescribeInstances(&describeInstancesInput) |
| 67 | + |
| 68 | + if err != nil { |
| 69 | + log.Fatalf("Failed to get instance for error %v", err) |
| 70 | + } |
| 71 | + |
| 72 | + for _, reservation := range describeInstancesOutput.Reservations { |
| 73 | + for _, instance := range reservation.Instances { |
| 74 | + //only delete instance if older than 5 days |
| 75 | + if time.Now().UTC().Add(pastDayDeleteCalculation).After(*instance.LaunchTime) { |
| 76 | + log.Printf("Try to delete instance %s tags %v launch-date %v", *instance.InstanceId, instance.Tags, instance.LaunchTime) |
| 77 | + deleteInstanceIds = append(deleteInstanceIds, instance.InstanceId) |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + if describeInstancesOutput.NextToken == nil { |
| 82 | + break |
| 83 | + } |
| 84 | + nextToken = describeInstancesOutput.NextToken |
| 85 | + } |
| 86 | + |
| 87 | + if len(deleteInstanceIds) < 1 { |
| 88 | + log.Printf("No instances to delete") |
| 89 | + return |
| 90 | + } |
| 91 | + |
| 92 | + terminateInstancesInput := ec2.TerminateInstancesInput{InstanceIds: deleteInstanceIds} |
| 93 | + _, err = ec2client.TerminateInstances(&terminateInstancesInput) |
| 94 | + if err != nil { |
| 95 | + log.Fatalf("Failed to terminate instances %v because of %v", deleteInstanceIds, err) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +func destroyLoadBalancerResource() { |
| 100 | + // Set up aws go sdk session |
| 101 | + // Only using default environment variables instead of loading other metadata from session.NewSessionWithOptions |
| 102 | + //Documents: https://docs.aws.amazon.com/ja_jp/sdk-for-go/v1/developer-guide/configuring-sdk.html |
| 103 | + testSession, err := session.NewSession() |
| 104 | + |
| 105 | + if err != nil { |
| 106 | + log.Fatalf("Error creating session %v", err) |
| 107 | + } |
| 108 | + |
| 109 | + svc := elbv2.New(testSession) |
| 110 | + |
| 111 | + //Allow to load all the load balancers since the default respond is paginated load balancers. |
| 112 | + //Look into the documentations and read the starting-token for more details |
| 113 | + //Documentation: https://docs.aws.amazon.com/cli/latest/reference/elbv2/describe-load-balancers.html#options |
| 114 | + var nextMarker *string |
| 115 | + |
| 116 | + for { |
| 117 | + //ELB Go SDK currently does not support filter tag or filter wildcard. Only supports with matching name |
| 118 | + //Documentation: https://github.com/aws/aws-sdk-go/blob/02266ed24221ac21bb37d6ac614d1ced95407556/service/elbv2/api.go#L5879-L5895 |
| 119 | + describeLoadBalancerInputs := &elbv2.DescribeLoadBalancersInput{Marker: nextMarker} |
| 120 | + describeLoadBalancerOutputs, err := svc.DescribeLoadBalancers(describeLoadBalancerInputs) |
| 121 | + |
| 122 | + if err != nil { |
| 123 | + log.Fatalf("Failed to get metadata for load balancer because of %v", err) |
| 124 | + } |
| 125 | + |
| 126 | + for _, lb := range describeLoadBalancerOutputs.LoadBalancers { |
| 127 | + |
| 128 | + //Skipping lb that does not contain aoc-lb string (relating to aws-otel-test-framework) |
| 129 | + if filterLbNameResult := strings.Contains(*lb.LoadBalancerName, containLbName); !filterLbNameResult { |
| 130 | + continue |
| 131 | + } |
| 132 | + |
| 133 | + //Skipping lb that does not older than 5 days |
| 134 | + if !time.Now().UTC().Add(pastDayDeleteCalculation).After(*lb.CreatedTime) { |
| 135 | + continue |
| 136 | + } |
| 137 | + |
| 138 | + log.Printf("Trying to delete lb %s with launch-date %v", *lb.LoadBalancerName, lb.CreatedTime) |
| 139 | + |
| 140 | + //Delete load balancer |
| 141 | + //Documentation: https://github.com/aws/aws-sdk-go/blob/main/service/elbv2/api.go#L829-L844 |
| 142 | + deleteLoadBalancerInput := &elbv2.DeleteLoadBalancerInput{ |
| 143 | + LoadBalancerArn: lb.LoadBalancerArn, |
| 144 | + } |
| 145 | + _, err = svc.DeleteLoadBalancer(deleteLoadBalancerInput) |
| 146 | + |
| 147 | + if err != nil { |
| 148 | + log.Fatalf("Failed to delete lb %s because of %v", *lb.LoadBalancerName, err) |
| 149 | + } |
| 150 | + log.Printf("Delete lb %s successfully", *lb.LoadBalancerName) |
| 151 | + } |
| 152 | + |
| 153 | + if describeLoadBalancerOutputs.NextMarker == nil { |
| 154 | + break |
| 155 | + } |
| 156 | + nextMarker = describeLoadBalancerOutputs.NextMarker |
| 157 | + } |
| 158 | +} |
0 commit comments