This example starts an Amazon EC2 instance.

go run StartInstancesv2.go -i INSTANCE-ID

  • INSTANCE-ID is the ID of the instance to start.

The unit test accepts a similar value in config.json.

Source code

// Copyright, Inc. or its affiliates. All Rights Reserved.
// SPDX - License - Identifier: Apache - 2.0
package main

import (


// EC2StartInstancesAPI defines the interface for the StartInstances function.
// We use this interface to test the function using a mocked service.
type EC2StartInstancesAPI interface {
	StartInstances(ctx context.Context,
		params *ec2.StartInstancesInput,
		optFns ...func(*ec2.Options)) (*ec2.StartInstancesOutput, error)

// StartInstance starts an Amazon Elastic Compute Cloud (Amazon EC2) instance.
// Inputs:
//     c is the context of the method call, which includes the AWS Region.
//     api is the interface that defines the method call.
//     input defines the input arguments to the service call.
// Output:
//     If success, a StartInstancesOutput object containing the result of the service call and nil.
//     Otherwise, nil and an error from the call to StartInstances.
func StartInstance(c context.Context, api EC2StartInstancesAPI, input *ec2.StartInstancesInput) (*ec2.StartInstancesOutput, error) {
	resp, err := api.StartInstances(c, input)

    var apiErr smithy.APIError
    if errors.As(err, &apiErr) && apiErr.ErrorCode() == "DryRunOperation" {
        fmt.Println("User has permission to start an instance.")
        input.DryRun = false
        return api.StartInstances(c, input)

	return resp, err

func main() {
	instanceID := flag.String("i", "", "The ID of the instance to start")

	if *instanceID == "" {
		fmt.Println("You must supply an instance ID (-i INSTANCE-ID")

	cfg, err := config.LoadDefaultConfig(context.TODO())
	if err != nil {
		panic("configuration error, " + err.Error())

	client := ec2.NewFromConfig(cfg)

	input := &ec2.StartInstancesInput{
		InstanceIds: []string{
		DryRun: true,

	_, err = StartInstance(context.TODO(), client, input)
	if err != nil {
		fmt.Println("Got an error starting the instance")

	fmt.Println("Started instance with ID " + *instanceID)

See the complete example in GitHub.