-
-
Save iwashi623/dc98f51ec72364f046e02cd180ddb2a1 to your computer and use it in GitHub Desktop.
snapshot_to_s3
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
func transferSnapshotToS3(db rdsiface.RDSAPI, bucketName, dbclusterIdentifier, roleArn, kmsKeyId string) (*rds.StartExportTaskOutput, error) { | |
currentTime := time.Now() | |
snapshotTimeFormat := currentTime.Format("2006-01-02") | |
expectSnapshotName := dbclusterIdentifier + "-" + snapshotTimeFormat | |
snapshotsReslut, err := db.DescribeDBClusterSnapshots(&rds.DescribeDBClusterSnapshotsInput{ | |
DBClusterIdentifier: aws.String(dbclusterIdentifier), | |
}) | |
if err != nil { | |
return nil, err | |
} | |
for _, snapshot := range snapshotsReslut.DBClusterSnapshots { | |
// currentTimeの日付と一致するスナップショットだけifの中に入る | |
if strings.Contains(*snapshot.DBClusterSnapshotArn, expectSnapshotName) { | |
snapshotReslut, err := db.StartExportTask(&rds.StartExportTaskInput{ | |
ExportTaskIdentifier: aws.String(expectSnapshotName), | |
IamRoleArn: aws.String(roleArn), | |
KmsKeyId: aws.String(kmsKeyId), | |
S3BucketName: aws.String(bucketName), | |
SourceArn: aws.String(*snapshot.DBClusterSnapshotArn), | |
}) | |
return snapshotReslut, err | |
} | |
} | |
return nil, nil | |
} | |
func lambdaHander() { | |
sess := session.Must(session.NewSessionWithOptions(session.Options{ | |
SharedConfigState: session.SharedConfigEnable, | |
})) | |
db := rds.New(sess) | |
bucketName := os.Getenv("S3_BUCKET") | |
dbclusterIdentifier := os.Getenv("DB_CLUSTER_IDENTIFIER") | |
roleArn := os.Getenv("ROLE_ARN") | |
kmsKeyId := os.Getenv("KMS_KEY_ID") | |
result, err := transferSnapshotToS3(db, bucketName, dbclusterIdentifier, roleArn, kmsKeyId) | |
if err != nil { | |
log.Printf("ERROR: %v", err) | |
return | |
} | |
log.Println(result) | |
} | |
func init() { | |
log.SetFlags(log.Ldate | log.Ltime | log.Lshortfile) | |
} | |
func main() { | |
lambda.Start(lambdaHander) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment