import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" ) sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := ec2.New(sess) input := &ec2.RunInstancesInput{ ImageId: aws.String("ami-0c94855ba95c71c99"), InstanceType: aws.String("t2.micro"), MinCount: aws.Int64(1), MaxCount: aws.Int64(1), } result, err := svc.RunInstances(input) if err != nil { fmt.Println("Error", err) return } fmt.Println(result)
import ( "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws/session" "github.com/aws/aws-sdk-go/service/ec2" ) sess := session.Must(session.NewSessionWithOptions(session.Options{ SharedConfigState: session.SharedConfigEnable, })) svc := ec2.New(sess) input := &ec2.StopInstancesInput{ InstanceIds: []*string{ aws.String("i-0123456789abcdef0"), }, } result, err := svc.StopInstances(input) if err != nil { fmt.Println("Error", err) return } fmt.Println(result)In summary, the github.com.aws.aws-sdk-go.service.ec2 package library provides Go developers with a powerful toolkit for interacting with AWS EC2 service. The example codes above demonstrate how to create a new EC2 instance and stop an existing one using the library's API.