ec2

package
v1.1.2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jul 8, 2023 License: MIT Imports: 8 Imported by: 0

README

awsutils/ec2

The ec2 package is a collection of utility functions designed to simplify common ec2 tasks.

Table of contents:


Functions

CheckInstanceExists(*ec2.EC2, string)
CheckInstanceExists(*ec2.EC2, string) error

CheckInstanceExists checks if an EC2 instance with the given instance ID exists.


CreateConnection()
CreateConnection() Connection

CreateConnection creates a connection with EC2 and returns a Connection.


CreateInstance(*ec2.EC2, Params)
CreateInstance(*ec2.EC2, Params) *ec2.Reservation, error

CreateInstance returns an ec2 reservation for an instance that is created with the input ec2Params.


DestroyInstance(*ec2.EC2, string)
DestroyInstance(*ec2.EC2, string) error

DestroyInstance terminates the ec2 instance associated with the input instanceID.


GetInstanceID(*ec2.Instance)
GetInstanceID(*ec2.Instance) string

GetInstanceID returns the instance ID from an input instanceReservation.


GetInstancePublicIP(*ec2.EC2, string)
GetInstancePublicIP(*ec2.EC2, string) string, error

GetInstancePublicIP returns the public IP address of the input instanceID.


GetInstanceState(*ec2.EC2, string)
GetInstanceState(*ec2.EC2, string) string, error

GetInstanceState returns the state of the ec2 instance associated with the input instanceID.


GetInstances(*ec2.EC2, []*ec2.Filter)
GetInstances(*ec2.EC2, []*ec2.Filter) []*ec2.Instance, error

GetInstances returns ec2 instances that the input client has access to. If no filters are provided, all ec2 instances will be returned by default.


GetInstancesRunningForMoreThan24Hours(*ec2.EC2)
GetInstancesRunningForMoreThan24Hours(*ec2.EC2) []*ec2.Instance, error

GetInstancesRunningForMoreThan24Hours returns a list of all EC2 instances running for more than 24 hours.


GetLatestAMI(AMIInfo)
GetLatestAMI(AMIInfo) string, error

GetLatestAMI retrieves the latest Amazon Machine Image (AMI) for a specified distribution, version and architecture. It utilizes AWS SDK to query AWS EC2 for the AMIs matching the provided pattern and returns the latest one based on the creation date.

Parameters:

info: An AMIInfo struct containing necessary details like Distro, Version, Architecture, and Region for which the AMI needs to be retrieved.

Returns:

string: The ID of the latest AMI found based on the provided information.

error: An error if any issue occurs while trying to get the latest AMI.


GetRegion(*ec2.EC2)
GetRegion(*ec2.EC2) string, error

GetRegion returns the region associated with the input ec2 client.


GetRunningInstances(*ec2.EC2)
GetRunningInstances(*ec2.EC2) *ec2.DescribeInstancesOutput, error

GetRunningInstances returns all ec2 instances with a state of running.


IsEC2Instance()
IsEC2Instance() bool

IsEC2Instance checks whether the code is running on an AWS EC2 instance by checking the existence of the file /sys/devices/virtual/dmi/id/product_uuid. If the file exists, the code is running on an EC2 instance, and the function returns true. If the file does not exist, the function returns false, indicating that the code is not running on an EC2 instance.

Returns:

bool: A boolean value that indicates whether the code is running on an EC2 instance.


TagInstance(*ec2.EC2, string, string, string)
TagInstance(*ec2.EC2, string, string, string) error

TagInstance tags the instance tied to the input ID with the specified tag.


WaitForInstance(*ec2.EC2, string)
WaitForInstance(*ec2.EC2, string) error

WaitForInstance waits for the input instanceID to get to a running state.


Installation

To use the awsutils/ec2 package, you first need to install it. Follow the steps below to install via go get.

go get github.com/awsutils/l50/ec2

Usage

After installation, you can import the package in your Go project using the following import statement:

import "github.com/awsutils/l50/ec2"

Tests

To ensure the package is working correctly, run the following command to execute the tests for ec2/awsutils:

go test -v

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.


License

This project is licensed under the MIT License - see the LICENSE file for details.

Documentation

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

func CheckInstanceExists added in v1.1.1

func CheckInstanceExists(client *ec2.EC2, instanceID string) error

CheckInstanceExists checks if an EC2 instance with the given instance ID exists.

func CreateInstance

func CreateInstance(client *ec2.EC2, ec2Params Params) (*ec2.Reservation, error)

CreateInstance returns an ec2 reservation for an instance that is created with the input ec2Params.

func DestroyInstance

func DestroyInstance(client *ec2.EC2, instanceID string) error

DestroyInstance terminates the ec2 instance associated with the input instanceID.

func GetInstanceID

func GetInstanceID(instanceReservation *ec2.Instance) string

GetInstanceID returns the instance ID from an input instanceReservation.

func GetInstancePublicIP

func GetInstancePublicIP(client *ec2.EC2, instanceID string) (string, error)

GetInstancePublicIP returns the public IP address of the input instanceID.

func GetInstanceState

func GetInstanceState(client *ec2.EC2, instanceID string) (string, error)

GetInstanceState returns the state of the ec2 instance associated with the input instanceID.

func GetInstances

func GetInstances(client *ec2.EC2, filters []*ec2.Filter) (
	[]*ec2.Instance, error)

GetInstances returns ec2 instances that the input client has access to. If no filters are provided, all ec2 instances will be returned by default.

func GetInstancesRunningForMoreThan24Hours added in v1.1.1

func GetInstancesRunningForMoreThan24Hours(client *ec2.EC2) ([]*ec2.Instance, error)

GetInstancesRunningForMoreThan24Hours returns a list of all EC2 instances running for more than 24 hours.

func GetLatestAMI added in v1.1.1

func GetLatestAMI(info AMIInfo) (string, error)

GetLatestAMI retrieves the latest Amazon Machine Image (AMI) for a specified distribution, version and architecture. It utilizes AWS SDK to query AWS EC2 for the AMIs matching the provided pattern and returns the latest one based on the creation date.

**Parameters:**

info: An AMIInfo struct containing necessary details like Distro, Version, Architecture, and Region for which the AMI needs to be retrieved.

**Returns:**

string: The ID of the latest AMI found based on the provided information.

error: An error if any issue occurs while trying to get the latest AMI.

Example
info := AMIInfo{
	Distro:       "ubuntu",
	Version:      "20.04",
	Architecture: "amd64",
	Region:       "us-west-2",
}

amiID, err := GetLatestAMI(info)
if err != nil {
	log.Fatalf("failed to get latest AMI: %v", err)
}

log.Println("Latest AMI ID:", amiID)

func GetRegion

func GetRegion(client *ec2.EC2) (string, error)

GetRegion returns the region associated with the input ec2 client.

func GetRunningInstances

func GetRunningInstances(client *ec2.EC2) (*ec2.DescribeInstancesOutput, error)

GetRunningInstances returns all ec2 instances with a state of running.

func IsEC2Instance

func IsEC2Instance() bool

IsEC2Instance checks whether the code is running on an AWS EC2 instance by checking the existence of the file /sys/devices/virtual/dmi/id/product_uuid. If the file exists, the code is running on an EC2 instance, and the function returns true. If the file does not exist, the function returns false, indicating that the code is not running on an EC2 instance.

**Returns:**

bool: A boolean value that indicates whether the code is running on an EC2 instance.

Example
isEC2 := IsEC2Instance()
if isEC2 {
	log.Println("Running on an EC2 instance")
} else {
	log.Println("Not running on an EC2 instance")
}

func TagInstance

func TagInstance(client *ec2.EC2, instanceID string, tagKey string, tagValue string) error

TagInstance tags the instance tied to the input ID with the specified tag.

func WaitForInstance

func WaitForInstance(client *ec2.EC2, instanceID string) error

WaitForInstance waits for the input instanceID to get to a running state.

Types

type AMIInfo added in v1.1.1

type AMIInfo struct {
	Distro       string
	Version      string
	Architecture string
	Region       string
}

AMIInfo provides information about an AMI.

**Attributes:**

Distro: the distro to use Version: the version to use Architecture: the architecture to use Region: the region to use

type Connection

type Connection struct {
	Client      *ec2.EC2
	Reservation *ec2.Reservation
	Params      Params
}

Connection contains all of the relevant information to maintain an EC2 connection.

**Attributes:**

Client: an EC2 session Reservation: an EC2 reservation Params: parameters for an EC2 instance

func CreateConnection

func CreateConnection() Connection

CreateConnection creates a connection with EC2 and returns a Connection.

type Params

type Params struct {
	AssociatePublicIPAddress bool
	ImageID                  string
	InstanceProfile          string
	InstanceType             string
	MinCount                 int
	MaxCount                 int
	SecurityGroupIDs         []string
	KeyName                  string
	SubnetID                 string
	VolumeSize               int64
	InstanceID               string
	InstanceName             string
	PublicIP                 string
}

Params provides parameter options for an EC2 instance.

**Attributes:**

AssociatePublicIPAddress: whether or not to associate a public IP address ImageID: the AMI ID to use InstanceProfile: the IAM instance profile to use InstanceType: the instance type to use MinCount: the minimum number of instances to launch MaxCount: the maximum number of instances to launch SecurityGroupIDs: the security group IDs to use KeyName: the key name to use SubnetID: the subnet ID to use VolumeSize: the volume size to use InstanceID: the instance ID to use InstanceName: the instance name to use PublicIP: the public IP to use

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL