Terminating EC2 Instances with Go and AWS SDK: A Step-by-Step Guide

Terminating EC2 Instances with Go and AWS SDK: A Step-by-Step Guide

Learn how to use the AWS SDK and Go programming language to terminate EC2 instances in a simple and efficient way.

ยท

4 min read

Intro ๐ŸŽฌ

Managing resources on the cloud is a crucial part of a developer's job. One of the essential resources on AWS is EC2 instances. EC2 instances are virtual machines on the cloud that provides scalable computing resources. These instances are billed by the hour, so it's essential to terminate instances when they are no longer needed to avoid unnecessary charges.

Today's work ๐Ÿ“–

Yesterday, I worked on waiting for the EC2 instance until it is running in Go using the AWS SDK for Go.

In this blog, I will be discussing the terminateEC2Instance() functionality in Go, which will allow us to terminate an EC2 instance programmatically.

Important Note ๐Ÿค”

This blog post does not cover the implementation of the createEC2Instance() and aitUntilInstanceRunning() functions. I have discussed all these things here:

In this blog, I am going to focus solely on how you can terminate an EC2 instance using the terminateEC2Instance() function.

Here is a video about this topic ๐Ÿš€

The terminateEC2Instance() function is part of a Go program that uses the AWS SDK to manage EC2 instances.

Now let's take a closer look at the terminateEC2Instance() function step by step:

Import Required Packages ๐Ÿ“ฆ

First, we need to import the required packages for working with the AWS SDK for Go and the EC2 service.

import (
    "fmt"
    "log"
    "os"
    "time"

    "github.com/aws/aws-sdk-go/aws"
    "github.com/aws/aws-sdk-go/aws/session"
    "github.com/aws/aws-sdk-go/service/ec2"
)

Define the terminate EC2 Instance function ๐Ÿ”‹

This function takes two arguments: svc of type \ec2.EC2, which represents an EC2 service client, and instanceID of the typed string*, which represents the ID of the EC2 instance that needs to be terminated.

func terminateEC2Instance(svc *ec2.EC2, instanceID string) error {
  // function code
}

Create a Terminate Instances Input ๐Ÿ“

Next, we create an input parameter of type \ec2.TerminateInstancesInput. This parameter contains the list of instance IDs that need to be terminated. Since we want to terminate only one instance, we pass an array with a single element, which is the instanceID* argument.

input := &ec2.TerminateInstancesInput{
    InstanceIds: []*string{aws.String(instanceID)},
}

Call the Terminate Instances function on the svc ๐Ÿ™‡

Then, we call the TerminateInstances() function on the svc object and pass in the input parameter. The TerminateInstances() function sends a request to the EC2 service to terminate the instances specified in the input parameter.

_, err := svc.TerminateInstances(input)
if err != nil {
    return err
}

Print a message that an Instance is terminated ๐Ÿ˜€

Finally, if the termination request is successful, we print a message to the console indicating that the EC2 instance with the specified ID has been terminated.

fmt.Printf("EC2 instance with an ID %s is terminated\n", instanceID)

return nil

Now that we understand the terminateEC2Instance function let's explore how it is used in the complete program.

Call the terminate EC2 instance function in the main function ๐Ÿงญ

time.Sleep(10 * time.Second)

err = terminateEC2Instance(serviceClient, instanceID)
if err != nil {
    log.Fatalf("failed to terminate an EC2 instance %v", err)
}

The program starts by creating an AWS session and an EC2 service client using the session.NewSession and ec2.New functions, respectively. It then creates an EC2 instance using the createEC2Instance() and waitUntilInstanceRunning() functions, which is not shown in this code snippet. After waiting for ten seconds, the program calls the terminateEC2Instance function to terminate the EC2 instance created earlier.

Repo ๐ŸŽ

The code that I have shown you in this blog is present in this repository.

Conclusion ๐Ÿ™‹โ€โ™€๏ธ

Managing EC2 instances on AWS programmatically is essential for optimizing costs and improving the overall efficiency of cloud resources.

The terminateEC2Instance function provides a straightforward way to terminate EC2 instances programmatically, reducing manual efforts and helping automate workflows. Understanding how this function works is the first step toward building more complex programs that interact with AWS services.

That's it for now. Did you like this blog? Please let me know.

You can Buy Me a Coffee if you want to and please don't forget to follow me on YouTube, Twitter, and LinkedIn also.

Happy Learning!

ย