DevOps in Go — AWS Operation, Reserved Instances

Go code snippet for AWS operations

Tony
2 min readJan 22, 2023

--

A code snippet in Golang to query all AWS EC2 “Reserved Instances” and get information. This code can be used to extract the “Expires” date of AWS reserved instances programmably. You can easily extend the purpose and functionality of this code.

AWS EC2 Reserved Instances

Amazon EC2 Reserved Instances (RI) provide a significant discount (up to 72%) compared to On-Demand pricing and provide a capacity reservation when used in a specific Availability Zone.

Code Snippet

package main

import (
"encoding/json"
"flag"
"fmt"
"os"
"strconv"

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

func CreateAwsSession(profile string, region string) *session.Session {
// Create a new session
sess, err := session.NewSessionWithOptions(session.Options{
// Set AWS profile
Profile: profile,
// Set AWS region
Config: aws.Config{
Region: aws.String(region),
},
})

if err != nil {
fmt.Println(err.Error())
os.Exit(1)
}

return sess
}

// Function to call AWS EC2 service and parse result
func CallAwsGetReservedInstances(sess *session.Session) []string {
var revervedInstanceInventory []string
// Create AWS…

--

--