AWS Doc SDK ExamplesWord
기계 번역으로 제공되는 번역입니다. 제공된 번역과 원본 영어의 내용이 상충하는 경우에는 영어 버전이 우선합니다.
RDS for Go V2를 사용한 Amazon SDK 예제
다음 코드 예제에서는 Amazon RDS와 함께 AWS SDK for Go V2를 사용하여 작업을 수행하고 일반적인 시나리오를 구현하는 방법을 보여줍니다.
기본 사항은 서비스 내에서 필수 작업을 수행하는 방법을 보여주는 코드 예제입니다.
작업은 대규모 프로그램에서 발췌한 코드이며 컨텍스트에 맞춰 실행해야 합니다. 작업은 개별 서비스 함수를 직접적으로 호출하는 방법을 보여주며 관련 시나리오의 컨텍스트에 맞는 작업을 볼 수 있습니다.
각 예제에는 컨텍스트에서 코드를 설정하고 실행하는 방법에 대한 지침을 찾을 수 있는 전체 소스 코드에 대한 링크가 포함되어 있습니다.
시작
다음 코드 예제에서는 Amazon RDS 사용을 시작하는 방법을 보여줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. 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/rds" ) // main uses the AWS SDK for Go V2 to create an Amazon Relational Database Service (Amazon RDS) // client and list up to 20 DB instances in your account. // This example uses the default settings specified in your shared credentials // and config files. func main() { ctx := context.Background() sdkConfig, err := config.LoadDefaultConfig(ctx) if err != nil { fmt.Println("Couldn't load default configuration. Have you set up your AWS account?") fmt.Println(err) return } rdsClient := rds.NewFromConfig(sdkConfig) const maxInstances = 20 fmt.Printf("Let's list up to %v DB instances.\n", maxInstances) output, err := rdsClient.DescribeDBInstances(ctx, &rds.DescribeDBInstancesInput{MaxRecords: aws.Int32(maxInstances)}) if err != nil { fmt.Printf("Couldn't list DB instances: %v\n", err) return } if len(output.DBInstances) == 0 { fmt.Println("No DB instances found.") } else { for _, instance := range output.DBInstances { fmt.Printf("DB instance %v has database %v.\n", *instance.DBInstanceIdentifier, *instance.DBName) } } }
-
API 세부 정보는 AWS SDK for Go API 참조의 DescribeDBInstances
를 참조하세요.
-
기본 사항
다음 코드 예시는 다음과 같은 작업을 수행하는 방법을 보여줍니다.
사용자 지정 DB 파라미터 그룹을 생성하고 파라미터 값을 설정합니다.
파라미터 그룹을 사용하도록 구성된 DB 인스턴스를 생성합니다. DB 인스턴스에는 데이터베이스도 포함되어 있습니다.
인스턴스의 스냅샷을 만듭니다.
인스턴스 및 파라미터 그룹을 삭제합니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. 명령 프롬프트에서 대화형 시나리오를 실행합니다.
import ( "context" "fmt" "log" "sort" "strconv" "strings" "time" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" "github.com/awsdocs/aws-doc-sdk-examples/gov2/demotools" "github.com/awsdocs/aws-doc-sdk-examples/gov2/rds/actions" "github.com/google/uuid" ) // GetStartedInstances is an interactive example that shows you how to use the AWS SDK for Go // with Amazon Relation Database Service (Amazon RDS) to do the following: // // 1. Create a custom DB parameter group and set parameter values. // 2. Create a DB instance that is configured to use the parameter group. The DB instance // also contains a database. // 3. Take a snapshot of the DB instance. // 4. Delete the DB instance and parameter group. type GetStartedInstances struct { sdkConfig aws.Config instances actions.DbInstances questioner demotools.IQuestioner helper IScenarioHelper isTestRun bool } // NewGetStartedInstances constructs a GetStartedInstances instance from a configuration. // It uses the specified config to get an Amazon RDS // client and create wrappers for the actions used in the scenario. func NewGetStartedInstances(sdkConfig aws.Config, questioner demotools.IQuestioner, helper IScenarioHelper) GetStartedInstances { rdsClient := rds.NewFromConfig(sdkConfig) return GetStartedInstances{ sdkConfig: sdkConfig, instances: actions.DbInstances{RdsClient: rdsClient}, questioner: questioner, helper: helper, } } // Run runs the interactive scenario. func (scenario GetStartedInstances) Run(ctx context.Context, dbEngine string, parameterGroupName string, instanceName string, dbName string) { defer func() { if r := recover(); r != nil { log.Println("Something went wrong with the demo.") } }() log.Println(strings.Repeat("-", 88)) log.Println("Welcome to the Amazon Relational Database Service (Amazon RDS) DB Instance demo.") log.Println(strings.Repeat("-", 88)) parameterGroup := scenario.CreateParameterGroup(ctx, dbEngine, parameterGroupName) scenario.SetUserParameters(ctx, parameterGroupName) instance := scenario.CreateInstance(ctx, instanceName, dbEngine, dbName, parameterGroup) scenario.DisplayConnection(instance) scenario.CreateSnapshot(ctx, instance) scenario.Cleanup(ctx, instance, parameterGroup) log.Println(strings.Repeat("-", 88)) log.Println("Thanks for watching!") log.Println(strings.Repeat("-", 88)) } // CreateParameterGroup shows how to get available engine versions for a specified // database engine and create a DB parameter group that is compatible with a // selected engine family. func (scenario GetStartedInstances) CreateParameterGroup(ctx context.Context, dbEngine string, parameterGroupName string) *types.DBParameterGroup { log.Printf("Checking for an existing DB parameter group named %v.\n", parameterGroupName) parameterGroup, err := scenario.instances.GetParameterGroup(ctx, parameterGroupName) if err != nil { panic(err) } if parameterGroup == nil { log.Printf("Getting available database engine versions for %v.\n", dbEngine) engineVersions, err := scenario.instances.GetEngineVersions(ctx, dbEngine, "") if err != nil { panic(err) } familySet := map[string]struct{}{} for _, family := range engineVersions { familySet[*family.DBParameterGroupFamily] = struct{}{} } var families []string for family := range familySet { families = append(families, family) } sort.Strings(families) familyIndex := scenario.questioner.AskChoice("Which family do you want to use?\n", families) log.Println("Creating a DB parameter group.") _, err = scenario.instances.CreateParameterGroup( ctx, parameterGroupName, families[familyIndex], "Example parameter group.") if err != nil { panic(err) } parameterGroup, err = scenario.instances.GetParameterGroup(ctx, parameterGroupName) if err != nil { panic(err) } } log.Printf("Parameter group %v:\n", *parameterGroup.DBParameterGroupFamily) log.Printf("\tName: %v\n", *parameterGroup.DBParameterGroupName) log.Printf("\tARN: %v\n", *parameterGroup.DBParameterGroupArn) log.Printf("\tFamily: %v\n", *parameterGroup.DBParameterGroupFamily) log.Printf("\tDescription: %v\n", *parameterGroup.Description) log.Println(strings.Repeat("-", 88)) return parameterGroup } // SetUserParameters shows how to get the parameters contained in a custom parameter // group and update some of the parameter values in the group. func (scenario GetStartedInstances) SetUserParameters(ctx context.Context, parameterGroupName string) { log.Println("Let's set some parameter values in your parameter group.") dbParameters, err := scenario.instances.GetParameters(ctx, parameterGroupName, "") if err != nil { panic(err) } var updateParams []types.Parameter for _, dbParam := range dbParameters { if strings.HasPrefix(*dbParam.ParameterName, "auto_increment") && *dbParam.IsModifiable && *dbParam.DataType == "integer" { log.Printf("The %v parameter is described as:\n\t%v", *dbParam.ParameterName, *dbParam.Description) rangeSplit := strings.Split(*dbParam.AllowedValues, "-") lower, _ := strconv.Atoi(rangeSplit[0]) upper, _ := strconv.Atoi(rangeSplit[1]) newValue := scenario.questioner.AskInt( fmt.Sprintf("Enter a value between %v and %v:", lower, upper), demotools.InIntRange{Lower: lower, Upper: upper}) dbParam.ParameterValue = aws.String(strconv.Itoa(newValue)) updateParams = append(updateParams, dbParam) } } err = scenario.instances.UpdateParameters(ctx, parameterGroupName, updateParams) if err != nil { panic(err) } log.Println("To get a list of parameters that you set previously, specify a source of 'user'.") userParameters, err := scenario.instances.GetParameters(ctx, parameterGroupName, "user") if err != nil { panic(err) } log.Println("Here are the parameters you set:") for _, param := range userParameters { log.Printf("\t%v: %v\n", *param.ParameterName, *param.ParameterValue) } log.Println(strings.Repeat("-", 88)) } // CreateInstance shows how to create a DB instance that contains a database of a // specified type. The database is also configured to use a custom DB parameter group. func (scenario GetStartedInstances) CreateInstance(ctx context.Context, instanceName string, dbEngine string, dbName string, parameterGroup *types.DBParameterGroup) *types.DBInstance { log.Println("Checking for an existing DB instance.") instance, err := scenario.instances.GetInstance(ctx, instanceName) if err != nil { panic(err) } if instance == nil { adminUsername := scenario.questioner.Ask( "Enter an administrator username for the database: ", demotools.NotEmpty{}) adminPassword := scenario.questioner.AskPassword( "Enter a password for the administrator (at least 8 characters): ", 7) engineVersions, err := scenario.instances.GetEngineVersions(ctx, dbEngine, *parameterGroup.DBParameterGroupFamily) if err != nil { panic(err) } var engineChoices []string for _, engine := range engineVersions { engineChoices = append(engineChoices, *engine.EngineVersion) } engineIndex := scenario.questioner.AskChoice( "The available engines for your parameter group are:\n", engineChoices) engineSelection := engineVersions[engineIndex] instOpts, err := scenario.instances.GetOrderableInstances(ctx, *engineSelection.Engine, *engineSelection.EngineVersion) if err != nil { panic(err) } optSet := map[string]struct{}{} for _, opt := range instOpts { if strings.Contains(*opt.DBInstanceClass, "micro") { optSet[*opt.DBInstanceClass] = struct{}{} } } var optChoices []string for opt := range optSet { optChoices = append(optChoices, opt) } sort.Strings(optChoices) optIndex := scenario.questioner.AskChoice( "The available micro DB instance classes for your database engine are:\n", optChoices) storageType := "standard" allocatedStorage := int32(5) log.Printf("Creating a DB instance named %v and database %v.\n"+ "The DB instance is configured to use your custom parameter group %v,\n"+ "selected engine %v,\n"+ "selected DB instance class %v,"+ "and %v GiB of %v storage.\n"+ "This typically takes several minutes.", instanceName, dbName, *parameterGroup.DBParameterGroupName, *engineSelection.EngineVersion, optChoices[optIndex], allocatedStorage, storageType) instance, err = scenario.instances.CreateInstance( ctx, instanceName, dbName, *engineSelection.Engine, *engineSelection.EngineVersion, *parameterGroup.DBParameterGroupName, optChoices[optIndex], storageType, allocatedStorage, adminUsername, adminPassword) if err != nil { panic(err) } for *instance.DBInstanceStatus != "available" { scenario.helper.Pause(30) instance, err = scenario.instances.GetInstance(ctx, instanceName) if err != nil { panic(err) } } log.Println("Instance created and available.") } log.Println("Instance data:") log.Printf("\tDBInstanceIdentifier: %v\n", *instance.DBInstanceIdentifier) log.Printf("\tARN: %v\n", *instance.DBInstanceArn) log.Printf("\tStatus: %v\n", *instance.DBInstanceStatus) log.Printf("\tEngine: %v\n", *instance.Engine) log.Printf("\tEngine version: %v\n", *instance.EngineVersion) log.Println(strings.Repeat("-", 88)) return instance } // DisplayConnection displays connection information about a DB instance and tips // on how to connect to it. func (scenario GetStartedInstances) DisplayConnection(instance *types.DBInstance) { log.Println( "You can now connect to your database by using your favorite MySQL client.\n" + "One way to connect is by using the 'mysql' shell on an Amazon EC2 instance\n" + "that is running in the same VPC as your DB instance. Pass the endpoint,\n" + "port, and administrator username to 'mysql'. Then, enter your password\n" + "when prompted:") log.Printf("\n\tmysql -h %v -P %v -u %v -p\n", *instance.Endpoint.Address, instance.Endpoint.Port, *instance.MasterUsername) log.Println("For more information, see the User Guide for RDS:\n" + "\thttps://docs.aws.amazon.com/AmazonRDS/latest/UserGuide/CHAP_GettingStarted.CreatingConnecting.MySQL.html#CHAP_GettingStarted.Connecting.MySQL") log.Println(strings.Repeat("-", 88)) } // CreateSnapshot shows how to create a DB instance snapshot and wait until it's available. func (scenario GetStartedInstances) CreateSnapshot(ctx context.Context, instance *types.DBInstance) { if scenario.questioner.AskBool( "Do you want to create a snapshot of your DB instance (y/n)? ", "y") { snapshotId := fmt.Sprintf("%v-%v", *instance.DBInstanceIdentifier, scenario.helper.UniqueId()) log.Printf("Creating a snapshot named %v. This typically takes a few minutes.\n", snapshotId) snapshot, err := scenario.instances.CreateSnapshot(ctx, *instance.DBInstanceIdentifier, snapshotId) if err != nil { panic(err) } for *snapshot.Status != "available" { scenario.helper.Pause(30) snapshot, err = scenario.instances.GetSnapshot(ctx, snapshotId) if err != nil { panic(err) } } log.Println("Snapshot data:") log.Printf("\tDBSnapshotIdentifier: %v\n", *snapshot.DBSnapshotIdentifier) log.Printf("\tARN: %v\n", *snapshot.DBSnapshotArn) log.Printf("\tStatus: %v\n", *snapshot.Status) log.Printf("\tEngine: %v\n", *snapshot.Engine) log.Printf("\tEngine version: %v\n", *snapshot.EngineVersion) log.Printf("\tDBInstanceIdentifier: %v\n", *snapshot.DBInstanceIdentifier) log.Printf("\tSnapshotCreateTime: %v\n", *snapshot.SnapshotCreateTime) log.Println(strings.Repeat("-", 88)) } } // Cleanup shows how to clean up a DB instance and DB parameter group. // Before the DB parameter group can be deleted, all associated DB instances must first be deleted. func (scenario GetStartedInstances) Cleanup( ctx context.Context, instance *types.DBInstance, parameterGroup *types.DBParameterGroup) { if scenario.questioner.AskBool( "\nDo you want to delete the database instance and parameter group (y/n)? ", "y") { log.Printf("Deleting database instance %v.\n", *instance.DBInstanceIdentifier) err := scenario.instances.DeleteInstance(ctx, *instance.DBInstanceIdentifier) if err != nil { panic(err) } log.Println( "Waiting for the DB instance to delete. This typically takes several minutes.") for instance != nil { scenario.helper.Pause(30) instance, err = scenario.instances.GetInstance(ctx, *instance.DBInstanceIdentifier) if err != nil { panic(err) } } log.Printf("Deleting parameter group %v.", *parameterGroup.DBParameterGroupName) err = scenario.instances.DeleteParameterGroup(ctx, *parameterGroup.DBParameterGroupName) if err != nil { panic(err) } } } // IScenarioHelper abstracts the function from a scenario so that it // can be mocked for unit testing. type IScenarioHelper interface { Pause(secs int) UniqueId() string } type ScenarioHelper struct{} // Pause waits for the specified number of seconds. func (helper ScenarioHelper) Pause(secs int) { time.Sleep(time.Duration(secs) * time.Second) } // UniqueId returns a new UUID. func (helper ScenarioHelper) UniqueId() string { return uuid.New().String() }
시나리오에서 Amazon RDS 작업을 관리하기 위해 호출하는 함수를 정의합니다.
import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // GetParameterGroup gets a DB parameter group by name. func (instances *DbInstances) GetParameterGroup(ctx context.Context, parameterGroupName string) ( *types.DBParameterGroup, error) { output, err := instances.RdsClient.DescribeDBParameterGroups( ctx, &rds.DescribeDBParameterGroupsInput{ DBParameterGroupName: aws.String(parameterGroupName), }) if err != nil { var notFoundError *types.DBParameterGroupNotFoundFault if errors.As(err, ¬FoundError) { log.Printf("Parameter group %v does not exist.\n", parameterGroupName) err = nil } else { log.Printf("Error getting parameter group %v: %v\n", parameterGroupName, err) } return nil, err } else { return &output.DBParameterGroups[0], err } } // CreateParameterGroup creates a DB parameter group that is based on the specified // parameter group family. func (instances *DbInstances) CreateParameterGroup( ctx context.Context, parameterGroupName string, parameterGroupFamily string, description string) ( *types.DBParameterGroup, error) { output, err := instances.RdsClient.CreateDBParameterGroup(ctx, &rds.CreateDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), DBParameterGroupFamily: aws.String(parameterGroupFamily), Description: aws.String(description), }) if err != nil { log.Printf("Couldn't create parameter group %v: %v\n", parameterGroupName, err) return nil, err } else { return output.DBParameterGroup, err } } // DeleteParameterGroup deletes the named DB parameter group. func (instances *DbInstances) DeleteParameterGroup(ctx context.Context, parameterGroupName string) error { _, err := instances.RdsClient.DeleteDBParameterGroup(ctx, &rds.DeleteDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), }) if err != nil { log.Printf("Couldn't delete parameter group %v: %v\n", parameterGroupName, err) return err } else { return nil } } // GetParameters gets the parameters that are contained in a DB parameter group. func (instances *DbInstances) GetParameters(ctx context.Context, parameterGroupName string, source string) ( []types.Parameter, error) { var output *rds.DescribeDBParametersOutput var params []types.Parameter var err error parameterPaginator := rds.NewDescribeDBParametersPaginator(instances.RdsClient, &rds.DescribeDBParametersInput{ DBParameterGroupName: aws.String(parameterGroupName), Source: aws.String(source), }) for parameterPaginator.HasMorePages() { output, err = parameterPaginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get parameters for %v: %v\n", parameterGroupName, err) break } else { params = append(params, output.Parameters...) } } return params, err } // UpdateParameters updates parameters in a named DB parameter group. func (instances *DbInstances) UpdateParameters(ctx context.Context, parameterGroupName string, params []types.Parameter) error { _, err := instances.RdsClient.ModifyDBParameterGroup(ctx, &rds.ModifyDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), Parameters: params, }) if err != nil { log.Printf("Couldn't update parameters in %v: %v\n", parameterGroupName, err) return err } else { return nil } } // CreateSnapshot creates a snapshot of a DB instance. func (instances *DbInstances) CreateSnapshot(ctx context.Context, instanceName string, snapshotName string) ( *types.DBSnapshot, error) { output, err := instances.RdsClient.CreateDBSnapshot(ctx, &rds.CreateDBSnapshotInput{ DBInstanceIdentifier: aws.String(instanceName), DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't create snapshot %v: %v\n", snapshotName, err) return nil, err } else { return output.DBSnapshot, nil } } // GetSnapshot gets a DB instance snapshot. func (instances *DbInstances) GetSnapshot(ctx context.Context, snapshotName string) (*types.DBSnapshot, error) { output, err := instances.RdsClient.DescribeDBSnapshots(ctx, &rds.DescribeDBSnapshotsInput{ DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't get snapshot %v: %v\n", snapshotName, err) return nil, err } else { return &output.DBSnapshots[0], nil } } // CreateInstance creates a DB instance. func (instances *DbInstances) CreateInstance(ctx context.Context, instanceName string, dbName string, dbEngine string, dbEngineVersion string, parameterGroupName string, dbInstanceClass string, storageType string, allocatedStorage int32, adminName string, adminPassword string) ( *types.DBInstance, error) { output, err := instances.RdsClient.CreateDBInstance(ctx, &rds.CreateDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), DBName: aws.String(dbName), DBParameterGroupName: aws.String(parameterGroupName), Engine: aws.String(dbEngine), EngineVersion: aws.String(dbEngineVersion), DBInstanceClass: aws.String(dbInstanceClass), StorageType: aws.String(storageType), AllocatedStorage: aws.Int32(allocatedStorage), MasterUsername: aws.String(adminName), MasterUserPassword: aws.String(adminPassword), }) if err != nil { log.Printf("Couldn't create instance %v: %v\n", instanceName, err) return nil, err } else { return output.DBInstance, nil } } // GetInstance gets data about a DB instance. func (instances *DbInstances) GetInstance(ctx context.Context, instanceName string) ( *types.DBInstance, error) { output, err := instances.RdsClient.DescribeDBInstances(ctx, &rds.DescribeDBInstancesInput{ DBInstanceIdentifier: aws.String(instanceName), }) if err != nil { var notFoundError *types.DBInstanceNotFoundFault if errors.As(err, ¬FoundError) { log.Printf("DB instance %v does not exist.\n", instanceName) err = nil } else { log.Printf("Couldn't get instance %v: %v\n", instanceName, err) } return nil, err } else { return &output.DBInstances[0], nil } } // DeleteInstance deletes a DB instance. func (instances *DbInstances) DeleteInstance(ctx context.Context, instanceName string) error { _, err := instances.RdsClient.DeleteDBInstance(ctx, &rds.DeleteDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), SkipFinalSnapshot: aws.Bool(true), DeleteAutomatedBackups: aws.Bool(true), }) if err != nil { log.Printf("Couldn't delete instance %v: %v\n", instanceName, err) return err } else { return nil } } // GetEngineVersions gets database engine versions that are available for the specified engine // and parameter group family. func (instances *DbInstances) GetEngineVersions(ctx context.Context, engine string, parameterGroupFamily string) ( []types.DBEngineVersion, error) { output, err := instances.RdsClient.DescribeDBEngineVersions(ctx, &rds.DescribeDBEngineVersionsInput{ Engine: aws.String(engine), DBParameterGroupFamily: aws.String(parameterGroupFamily), }) if err != nil { log.Printf("Couldn't get engine versions for %v: %v\n", engine, err) return nil, err } else { return output.DBEngineVersions, nil } } // GetOrderableInstances uses a paginator to get DB instance options that can be used to create DB instances that are // compatible with a set of specifications. func (instances *DbInstances) GetOrderableInstances(ctx context.Context, engine string, engineVersion string) ( []types.OrderableDBInstanceOption, error) { var output *rds.DescribeOrderableDBInstanceOptionsOutput var instanceOptions []types.OrderableDBInstanceOption var err error orderablePaginator := rds.NewDescribeOrderableDBInstanceOptionsPaginator(instances.RdsClient, &rds.DescribeOrderableDBInstanceOptionsInput{ Engine: aws.String(engine), EngineVersion: aws.String(engineVersion), }) for orderablePaginator.HasMorePages() { output, err = orderablePaginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get orderable DB instance options: %v\n", err) break } else { instanceOptions = append(instanceOptions, output.OrderableDBInstanceOptions...) } } return instanceOptions, err }
작업
다음 코드 예시에서는 CreateDBInstance
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // CreateInstance creates a DB instance. func (instances *DbInstances) CreateInstance(ctx context.Context, instanceName string, dbName string, dbEngine string, dbEngineVersion string, parameterGroupName string, dbInstanceClass string, storageType string, allocatedStorage int32, adminName string, adminPassword string) ( *types.DBInstance, error) { output, err := instances.RdsClient.CreateDBInstance(ctx, &rds.CreateDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), DBName: aws.String(dbName), DBParameterGroupName: aws.String(parameterGroupName), Engine: aws.String(dbEngine), EngineVersion: aws.String(dbEngineVersion), DBInstanceClass: aws.String(dbInstanceClass), StorageType: aws.String(storageType), AllocatedStorage: aws.Int32(allocatedStorage), MasterUsername: aws.String(adminName), MasterUserPassword: aws.String(adminPassword), }) if err != nil { log.Printf("Couldn't create instance %v: %v\n", instanceName, err) return nil, err } else { return output.DBInstance, nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 CreateDBInstance
를 참조하세요.
-
다음 코드 예시에서는 CreateDBParameterGroup
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // CreateParameterGroup creates a DB parameter group that is based on the specified // parameter group family. func (instances *DbInstances) CreateParameterGroup( ctx context.Context, parameterGroupName string, parameterGroupFamily string, description string) ( *types.DBParameterGroup, error) { output, err := instances.RdsClient.CreateDBParameterGroup(ctx, &rds.CreateDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), DBParameterGroupFamily: aws.String(parameterGroupFamily), Description: aws.String(description), }) if err != nil { log.Printf("Couldn't create parameter group %v: %v\n", parameterGroupName, err) return nil, err } else { return output.DBParameterGroup, err } }
-
API 세부 정보는 AWS SDK for Go API 참조의 CreateDBParameter 그룹을
참조하세요.
-
다음 코드 예시에서는 CreateDBSnapshot
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // CreateSnapshot creates a snapshot of a DB instance. func (instances *DbInstances) CreateSnapshot(ctx context.Context, instanceName string, snapshotName string) ( *types.DBSnapshot, error) { output, err := instances.RdsClient.CreateDBSnapshot(ctx, &rds.CreateDBSnapshotInput{ DBInstanceIdentifier: aws.String(instanceName), DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't create snapshot %v: %v\n", snapshotName, err) return nil, err } else { return output.DBSnapshot, nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 CreateDBSnapshot
를 참조하세요.
-
다음 코드 예시에서는 DeleteDBInstance
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // DeleteInstance deletes a DB instance. func (instances *DbInstances) DeleteInstance(ctx context.Context, instanceName string) error { _, err := instances.RdsClient.DeleteDBInstance(ctx, &rds.DeleteDBInstanceInput{ DBInstanceIdentifier: aws.String(instanceName), SkipFinalSnapshot: aws.Bool(true), DeleteAutomatedBackups: aws.Bool(true), }) if err != nil { log.Printf("Couldn't delete instance %v: %v\n", instanceName, err) return err } else { return nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 DeleteDBInstance
를 참조하세요.
-
다음 코드 예시에서는 DeleteDBParameterGroup
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // DeleteParameterGroup deletes the named DB parameter group. func (instances *DbInstances) DeleteParameterGroup(ctx context.Context, parameterGroupName string) error { _, err := instances.RdsClient.DeleteDBParameterGroup(ctx, &rds.DeleteDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), }) if err != nil { log.Printf("Couldn't delete parameter group %v: %v\n", parameterGroupName, err) return err } else { return nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 DeleteDBParameter 그룹을
참조하세요.
-
다음 코드 예시에서는 DescribeDBEngineVersions
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // GetEngineVersions gets database engine versions that are available for the specified engine // and parameter group family. func (instances *DbInstances) GetEngineVersions(ctx context.Context, engine string, parameterGroupFamily string) ( []types.DBEngineVersion, error) { output, err := instances.RdsClient.DescribeDBEngineVersions(ctx, &rds.DescribeDBEngineVersionsInput{ Engine: aws.String(engine), DBParameterGroupFamily: aws.String(parameterGroupFamily), }) if err != nil { log.Printf("Couldn't get engine versions for %v: %v\n", engine, err) return nil, err } else { return output.DBEngineVersions, nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 DescribeDBEngine 버전을 참조하세요
.
-
다음 코드 예시에서는 DescribeDBInstances
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // GetInstance gets data about a DB instance. func (instances *DbInstances) GetInstance(ctx context.Context, instanceName string) ( *types.DBInstance, error) { output, err := instances.RdsClient.DescribeDBInstances(ctx, &rds.DescribeDBInstancesInput{ DBInstanceIdentifier: aws.String(instanceName), }) if err != nil { var notFoundError *types.DBInstanceNotFoundFault if errors.As(err, ¬FoundError) { log.Printf("DB instance %v does not exist.\n", instanceName) err = nil } else { log.Printf("Couldn't get instance %v: %v\n", instanceName, err) } return nil, err } else { return &output.DBInstances[0], nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 DescribeDBInstances
를 참조하세요.
-
다음 코드 예시에서는 DescribeDBParameterGroups
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // GetParameterGroup gets a DB parameter group by name. func (instances *DbInstances) GetParameterGroup(ctx context.Context, parameterGroupName string) ( *types.DBParameterGroup, error) { output, err := instances.RdsClient.DescribeDBParameterGroups( ctx, &rds.DescribeDBParameterGroupsInput{ DBParameterGroupName: aws.String(parameterGroupName), }) if err != nil { var notFoundError *types.DBParameterGroupNotFoundFault if errors.As(err, ¬FoundError) { log.Printf("Parameter group %v does not exist.\n", parameterGroupName) err = nil } else { log.Printf("Error getting parameter group %v: %v\n", parameterGroupName, err) } return nil, err } else { return &output.DBParameterGroups[0], err } }
-
API 세부 정보는 AWS SDK for Go API 참조의 DescribeDBParameter 그룹을 참조하세요
.
-
다음 코드 예시에서는 DescribeDBParameters
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // GetParameters gets the parameters that are contained in a DB parameter group. func (instances *DbInstances) GetParameters(ctx context.Context, parameterGroupName string, source string) ( []types.Parameter, error) { var output *rds.DescribeDBParametersOutput var params []types.Parameter var err error parameterPaginator := rds.NewDescribeDBParametersPaginator(instances.RdsClient, &rds.DescribeDBParametersInput{ DBParameterGroupName: aws.String(parameterGroupName), Source: aws.String(source), }) for parameterPaginator.HasMorePages() { output, err = parameterPaginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get parameters for %v: %v\n", parameterGroupName, err) break } else { params = append(params, output.Parameters...) } } return params, err }
-
API 세부 정보는 AWS SDK for Go API 참조의 DescribeDBParameters
를 참조하세요.
-
다음 코드 예시에서는 DescribeDBSnapshots
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // GetSnapshot gets a DB instance snapshot. func (instances *DbInstances) GetSnapshot(ctx context.Context, snapshotName string) (*types.DBSnapshot, error) { output, err := instances.RdsClient.DescribeDBSnapshots(ctx, &rds.DescribeDBSnapshotsInput{ DBSnapshotIdentifier: aws.String(snapshotName), }) if err != nil { log.Printf("Couldn't get snapshot %v: %v\n", snapshotName, err) return nil, err } else { return &output.DBSnapshots[0], nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 DescribeDBSnapshots
를 참조하세요.
-
다음 코드 예시에서는 DescribeOrderableDBInstanceOptions
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // GetOrderableInstances uses a paginator to get DB instance options that can be used to create DB instances that are // compatible with a set of specifications. func (instances *DbInstances) GetOrderableInstances(ctx context.Context, engine string, engineVersion string) ( []types.OrderableDBInstanceOption, error) { var output *rds.DescribeOrderableDBInstanceOptionsOutput var instanceOptions []types.OrderableDBInstanceOption var err error orderablePaginator := rds.NewDescribeOrderableDBInstanceOptionsPaginator(instances.RdsClient, &rds.DescribeOrderableDBInstanceOptionsInput{ Engine: aws.String(engine), EngineVersion: aws.String(engineVersion), }) for orderablePaginator.HasMorePages() { output, err = orderablePaginator.NextPage(ctx) if err != nil { log.Printf("Couldn't get orderable DB instance options: %v\n", err) break } else { instanceOptions = append(instanceOptions, output.OrderableDBInstanceOptions...) } } return instanceOptions, err }
-
API 세부 정보는 DescribeOrderable 참조의 WordDBInstanceOptions
를 참조하세요. AWS SDK for Go API
-
다음 코드 예시에서는 ModifyDBParameterGroup
을 사용하는 방법을 보여 줍니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. AWS 코드 예시 리포지토리
에서 전체 예시를 찾고 설정 및 실행하는 방법을 배워보세요. import ( "context" "errors" "log" "github.com/aws/aws-sdk-go-v2/aws" "github.com/aws/aws-sdk-go-v2/service/rds" "github.com/aws/aws-sdk-go-v2/service/rds/types" ) type DbInstances struct { RdsClient *rds.Client } // UpdateParameters updates parameters in a named DB parameter group. func (instances *DbInstances) UpdateParameters(ctx context.Context, parameterGroupName string, params []types.Parameter) error { _, err := instances.RdsClient.ModifyDBParameterGroup(ctx, &rds.ModifyDBParameterGroupInput{ DBParameterGroupName: aws.String(parameterGroupName), Parameters: params, }) if err != nil { log.Printf("Couldn't update parameters in %v: %v\n", parameterGroupName, err) return err } else { return nil } }
-
API 세부 정보는 AWS SDK for Go API 참조의 ModifyDBParameter 그룹을
참조하세요.
-
서버리스 예제
다음 코드 예제는 RDS 데이터베이스에 연결하는 Lambda 함수를 구현하는 방법을 보여줍니다. 이 함수는 간단한 데이터베이스 요청을 하고 결과를 반환합니다.
- SDK for Go V2
-
참고
더 많은 on GitHub가 있습니다. 서버리스 예제
리포지토리에서 전체 예제를 찾아보고 설정 및 실행 방법을 알아봅니다. Go를 사용하여 Lambda 함수의 Amazon RDS 데이터베이스에 연결합니다.
/* Golang v2 code here. */ package main import ( "context" "database/sql" "encoding/json" "fmt" "os" "github.com/aws/aws-lambda-go/lambda" "github.com/aws/aws-sdk-go-v2/config" "github.com/aws/aws-sdk-go-v2/feature/rds/auth" _ "github.com/go-sql-driver/mysql" ) type MyEvent struct { Name string `json:"name"` } func HandleRequest(event *MyEvent) (map[string]interface{}, error) { var dbName string = os.Getenv("DatabaseName") var dbUser string = os.Getenv("DatabaseUser") var dbHost string = os.Getenv("DBHost") // Add hostname without https var dbPort int = os.Getenv("Port") // Add port number var dbEndpoint string = fmt.Sprintf("%s:%d", dbHost, dbPort) var region string = os.Getenv("AWS_REGION") cfg, err := config.LoadDefaultConfig(context.TODO()) if err != nil { panic("configuration error: " + err.Error()) } authenticationToken, err := auth.BuildAuthToken( context.TODO(), dbEndpoint, region, dbUser, cfg.Credentials) if err != nil { panic("failed to create authentication token: " + err.Error()) } dsn := fmt.Sprintf("%s:%s@tcp(%s)/%s?tls=true&allowCleartextPasswords=true", dbUser, authenticationToken, dbEndpoint, dbName, ) db, err := sql.Open("mysql", dsn) if err != nil { panic(err) } defer db.Close() var sum int err = db.QueryRow("SELECT ?+? AS sum", 3, 2).Scan(&sum) if err != nil { panic(err) } s := fmt.Sprint(sum) message := fmt.Sprintf("The selected sum is: %s", s) messageBytes, err := json.Marshal(message) if err != nil { return nil, err } messageString := string(messageBytes) return map[string]interface{}{ "statusCode": 200, "headers": map[string]string{"Content-Type": "application/json"}, "body": messageString, }, nil } func main() { lambda.Start(HandleRequest) }