CreateInstanceSnapshot.go - AWS Code Sample

CreateInstanceSnapshot.go

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: MIT-0 package main import ( "flag" "fmt" "strings" "time" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/rds" "github.com/aws/aws-sdk-go/service/rds/rdsiface" ) // MakeInstanceSnapshot creates a snapshot for an Amazon RDS instance // Inputs: // sess is the current session, which provides configuration for the SDK's service clients // instance is the name of the instance // Output: // If success, nil // Otherwise, an error from the call to CreateDBSnapshot func MakeInstanceSnapshot(svc rdsiface.RDSAPI, instance *string) error { // Get the current date and time to uniquely identify snapshot currentTime := time.Now() t := currentTime.Format("2006-01-02 15:04:05") // Replace space with underscore for snapshot ID t = strings.Replace(t, " ", "_", -1) _, err := svc.CreateDBSnapshot(&rds.CreateDBSnapshotInput{ DBInstanceIdentifier: instance, DBSnapshotIdentifier: aws.String(*instance + t), }) if err != nil { return err } return nil } func main() { instance := flag.String("i", "", "The name of the instance") flag.Parse() if *instance == "" { fmt.Println("You must supply and instance name (-i INSTANCE)") return } sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := rds.New(sess) err := MakeInstanceSnapshot(svc, instance) if err != nil { fmt.Println("Got an error creating instance snapshot:") fmt.Println(err) return } // Wait until snapshot is created before finishing fmt.Println("Waiting for snapshot in instance " + *instance + " to be created") err = svc.WaitUntilDBSnapshotAvailable(&rds.DescribeDBSnapshotsInput{ DBInstanceIdentifier: instance, }) if err != nil { fmt.Println("Got an error waiting for snapshot to be created:") fmt.Println(err) return } fmt.Println("Snapshot for instance " + *instance + " successfully created") }