Created
April 24, 2024 09:34
-
-
Save niski84/3b1d4d2e0ca8b7c312c085e9855e3bb9 to your computer and use it in GitHub Desktop.
trace ebs snapshot back to elasticbeanstalk environment name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"fmt" | |
"github.com/aws/aws-sdk-go-v2/aws" | |
"github.com/aws/aws-sdk-go-v2/config" | |
"github.com/aws/aws-sdk-go-v2/service/ec2" | |
"github.com/aws/aws-sdk-go-v2/service/resourcegroupstaggingapi" | |
) | |
// TraceEBSnapshot traces an EBS snapshot to its volume, instance, and Elastic Beanstalk environment. | |
func TraceEBSnapshot(ctx context.Context, snapshotID string) (string, error) { | |
cfg, err := config.LoadDefaultConfig(ctx) | |
if err != nil { | |
return "", fmt.Errorf("failed to load AWS SDK config: %w", err) | |
} | |
ec2Client := ec2.NewFromConfig(cfg) | |
taggingClient := resourcegroupstaggingapi.NewFromConfig(cfg) | |
// Get snapshot information | |
snapshotOutput, err := ec2Client.DescribeSnapshots(ctx, &ec2.DescribeSnapshotsInput{ | |
SnapshotIds: []string{snapshotID}, | |
}) | |
if err != nil { | |
return "", fmt.Errorf("failed to describe snapshot %s: %w", snapshotID, err) | |
} | |
if len(snapshotOutput.Snapshots) == 0 { | |
return "", fmt.Errorf("no snapshot found with ID %s", snapshotID) | |
} | |
volumeID := *snapshotOutput.Snapshots[0].VolumeId | |
// Get volume information | |
volumeOutput, err := ec2Client.DescribeVolumes(ctx, &ec2.DescribeVolumesInput{ | |
VolumeIds: []string{volumeID}, | |
}) | |
if err != nil { | |
return "", fmt.Errorf("failed to describe volume %s: %w", volumeID, err) | |
} | |
if len(volumeOutput.Volumes) == 0 || len(volumeOutput.Volumes[0].Attachments) == 0 { | |
return "", fmt.Errorf("volume %s is not attached to any instance", volumeID) | |
} | |
instanceID := *volumeOutput.Volumes[0].Attachments[0].InstanceId | |
// Get instance tags to find the Elastic Beanstalk environment | |
instanceOutput, err := ec2Client.DescribeInstances(ctx, &ec2.DescribeInstancesInput{ | |
InstanceIds: []string{instanceID}, | |
}) | |
if err != nil { | |
return "", fmt.Errorf("failed to describe instance %s: %w", instanceID, err) | |
} | |
for _, tag := range instanceOutput.Reservations[0].Instances[0].Tags { | |
if *tag.Key == "elasticbeanstalk:environment-name" { | |
return *tag.Value, nil | |
} | |
} | |
return "", fmt.Errorf("instance %s is not part of an Elastic Beanstalk environment", instanceID) | |
} | |
func main() { | |
ctx := context.Background() | |
envName, err := TraceEBSnapshot(ctx, "snap-1234567890abcdef0") | |
if err != nil { | |
fmt.Println("Error:", err) | |
return | |
} | |
fmt.Println("Elastic Beanstalk Environment Name:", envName) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment