aws

package
v0.0.0-...-492d7f2 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2018 License: Apache-2.0 Imports: 17 Imported by: 0

Documentation

Overview

Package aws provides a standard way to create a virtual machine on AWS.

Index

Constants

View Source
const (
	// PublicIP is the index of the public IP address that GetIPs returns.
	PublicIP = 0
	// PrivateIP is the index of the private IP address that GetIPs returns.
	PrivateIP = 1

	// StateStarted is the state AWS reports when the VM is started.
	StateStarted = "running"
	// StateHalted is the state AWS reports when the VM is halted.
	StateHalted = "stopped"
	// StateDestroyed is the state AWS reports when the VM is destroyed.
	StateDestroyed = "terminated"
	// StatePending is the state AWS reports when the VM is pending.
	StatePending = "pending"
)
View Source
const (
	// Timeout for VM operations viz. Halt, Start & Terminate in seconds
	VmOpsTimeout = 900 // 15 mins

	// Retry interval for VM operations in seconds
	VmOpsInterval = 15

	// Timeout for Volume operations viz. Create, Detach in seconds
	VolTimeout = 600 // 10 mins

	// Retry interval for Volume operations in seconds
	VolInterval = 5
)
View Source
const (
	HttpClientTimeout = 30
)
View Source
const (

	// RegionEnv is the env var for the AWS region.
	RegionEnv = "AWS_DEFAULT_REGION"
)

Variables

View Source
var (
	// ErrNoCreds is returned when no credentials are found in environment or
	// home directory.
	ErrNoCreds = errors.New("Missing AWS credentials")
	// ErrNoRegion is returned when a request was sent without a region.
	ErrNoRegion = errors.New("Missing AWS region")
	// ErrNoInstance is returned querying an instance, but none is found.
	ErrNoInstance = errors.New("Missing VM instance")
	// ErrNoInstanceID is returned when attempting to perform an operation on
	// an instance, but the ID is missing.
	ErrNoInstanceID = errors.New("Missing instance ID")
	// ErrProvisionTimeout is returned when the EC2 instance takes too long to
	// enter "running" state.
	ErrProvisionTimeout = errors.New("AWS provision timeout")
	// ErrNoIPs is returned when no IP addresses are found for an instance.
	ErrNoIPs = errors.New("Missing IPs for instance")
	// ErrNoSupportSuspend is returned when vm.Suspend() is called.
	ErrNoSupportSuspend = errors.New("Suspend action not supported by AWS")
	// ErrNoSupportResume is returned when vm.Resume() is called.
	ErrNoSupportResume = errors.New("Resume action not supported by AWS")
)
View Source
var SSHTimeout = 5 * time.Minute

SSHTimeout is the maximum time to wait before failing to GetSSH. This is not thread-safe.

Functions

func DeleteKeyPair

func DeleteKeyPair(name string, region string) error

DeleteKeyPair deletes the given key pair from the given region.

func UploadKeyPair

func UploadKeyPair(publicKey []byte, name string, region string) error

UploadKeyPair uploads the public key to AWS with a given name. If the public key already exists, then no error is returned.

func ValidCredentials

func ValidCredentials(region string) error

ValidCredentials sends a dummy request to AWS to check if credentials are valid. An error is returned if credentials are missing or region is missing.

Types

type EbsBlockVolume

type EbsBlockVolume struct {
	DeviceName       string `json:"device_name,omitempty"`
	VolumeSize       *int64 `json:"volume_size,omitempty"`
	VolumeType       string `json:"volume_type,omitempty"`
	AvailabilityZone string `json:"availability_zone,omitempty"`
	VolumeId         string `json:"volume_id,omitempty"`
	SnapshotId       string `json:"snapshot_id,omitempty"`
}

EbsBlockVolume represents a AWS EbsBlockDevice

type Image

type Image struct {
	Id                 *string           `json:"id,omitempty"`
	Name               *string           `json:"name,omitempty"`
	Description        *string           `json:"description,omitempty"`
	State              *string           `json:"state,omitempty"`
	OwnerId            *string           `json:"owner_id,omitempty"`
	OwnerAlias         *string           `json:"owner_alias,omitempty"`
	CreationDate       *string           `json:"creation_date,omitempty"`
	Architecture       *string           `json:"architecture,omitempty"`
	Platform           *string           `json:"platform,omitempty"`
	Hypervisor         *string           `json:"hypervisor,omitempty"`
	VirtualizationType *string           `json:"virtualization_type,omitempty"`
	ImageType          *string           `json:"image_type,omitempty"`
	KernelId           *string           `json:"kernel_id,omitemtpy"`
	RootDeviceName     *string           `json:"root_device_name,omitempty"`
	RootDeviceType     *string           `json:"root_device_type,omitempty"`
	Public             *bool             `json:"public,omitempty"`
	EbsVolumes         []*EbsBlockVolume `json:"ebs_volumes,omitempty"`
}

Image represents a AWS Image

type InstanceStatus

type InstanceStatus struct {
	AvailabilityZone string `json:"availability_zone,omitempty"`
	InstanceId       string `json:"instance_id,omitempty"`
	State            string `json:"state,omitempty"`
}

InstanceStatus represents AWS InstanceStatus

func GetInstanceStatus

func GetInstanceStatus(svc *ec2.EC2, instID string) (*InstanceStatus, error)

GetInstanceStatus: returns status of given instances Status includes availabilityZone & state

type IpPermission

type IpPermission struct {
	FromPort   *int64   `json:"from_port,omitempty"`
	ToPort     *int64   `json:"to_port,omitempty"`
	IpProtocol string   `json:"ip_protocol,omitempty"`
	Ipv4Ranges []string `json:"ipv4_ranges,omitempty"`
	Ipv6Ranges []string `json:"ipv6_ranges,omitempty"`
}

IpPermission in AWS is used to represent inbound or outbound rules associated with SecurityGroup

type ReadyError

type ReadyError struct {
	Err error

	ImageID               string
	InstanceID            string
	InstanceType          string
	LaunchTime            time.Time
	PublicIPAddress       string
	State                 string
	StateReason           string
	StateTransitionReason string
	SubnetID              string
	VPCID                 string
}

ReadyError is an information error that tells you why an instance wasn't ready.

func (ReadyError) Error

func (e ReadyError) Error() string

Error returns a summarized string version of ReadyError. More details about the failed instance can be accessed through the struct.

type Region

type Region struct {
	Name           string `json:"name,omitempty"`
	RegionEndpoint string `json:"region_endpoint,omitempty"`
}

Region represents a AWS Region

type S3

type S3 struct {
	Name   string // required
	Region string // required
	Prefix string // for creating a bucket /obj1/obj2/obj3

}

func (*S3) BucketExist

func (s3Obj *S3) BucketExist() (bool, error)

func (*S3) CreateBucket

func (s3Obj *S3) CreateBucket() error

func (*S3) DeleteBucket

func (s3Obj *S3) DeleteBucket() error

func (*S3) GetS3BucketsList

func (s3Obj *S3) GetS3BucketsList() ([]string, error)

type SecurityGroup

type SecurityGroup struct {
	Id                  string         `json:"id,omitempty"`
	Name                string         `json:"name,omitempty"`
	Description         string         `json:"description,omitempty"`
	OwnerId             string         `json:"owner_id,omitempty"`
	VpcId               string         `json:"vpc_id,omitempty"`
	IpPermissionsEgress []IpPermission `json:"ip_permissions_egress,omitempty"`
	IpPermissions       []IpPermission `json:"ip_permissions,omitempty"`
}

SecurityGroup represents a AWS SecurityGroup

type Subnet

type Subnet struct {
	Id                    string   `json:"id,omitempty"`
	State                 string   `json:"state,omitempty"`
	VpcId                 string   `json:"vpc_id,omitempty"`
	IPv4Block             string   `json:"ipv4block,omitempty"`
	IPv6Blocks            []string `json:"ipv6blocks,omitempty"`
	AvailableAddressCount *int64   `json:"available_address_count,omitempty"`
	// Availability Zone of the subnet
	AvailabilityZone string `json:"availability_zone,omitempty"`
	// Indicates if this is default for Availability Zone
	DefaultForAz        bool `json:"default_for_az,omitempty"`
	MapPublicIpOnLaunch bool `json:"map_public_ip_on_launch,omitempty"`
}

Subnet represents a AWS Subnet

type VM

type VM struct {
	Name                   string
	Region                 string // required
	AMI                    string
	InstanceType           string
	InstanceID             string // required when adding volume
	KeyPair                string // required
	IamInstanceProfileName string
	PrivateIPAddress       string

	// required when addding or deleting volume
	Volumes                      []EbsBlockVolume
	KeepRootVolumeOnDestroy      bool
	DeleteNonRootVolumeOnDestroy bool

	VPC    string
	Subnet string
	// required when modifying security group rules
	// all other parameters except this one and Region
	// is ingnored while security group modification
	SecurityGroups []SecurityGroup

	SSHCreds            ssh.Credentials // required
	DeleteKeysOnDestroy bool

	// only relevant in GetSubnetList, GetSecurityGroupList & GetImageList
	// filters result with given key-values
	Filters map[string][]*string
}

VM represents an AWS EC2 virtual machine.

func (*VM) AttachVolume

func (vm *VM) AttachVolume() error

AttachVolume: Attaches given volume to given instance

func (*VM) AuthorizeSecurityGroup

func (vm *VM) AuthorizeSecurityGroup() error

AuthorizeSecurityGroup: Adds one or more rules to a security group

func (*VM) CreateVolume

func (vm *VM) CreateVolume() error

CreateVolume: Creates a volume with given parameter

func (*VM) DeleteVolume

func (vm *VM) DeleteVolume() error

DeleteVolume: Deletes volume with given Id Disk must not be in-use by any instance

func (*VM) Destroy

func (vm *VM) Destroy() error

Destroy terminates the VM on AWS. It returns an error if AWS credentials are missing or if there is no instance ID.

func (*VM) DetachVolume

func (vm *VM) DetachVolume() error

DetachVolume: Detaches volume with given Id from instance

func (*VM) GetAvailabilityZoneList

func (vm *VM) GetAvailabilityZoneList() ([]Zone, error)

GetAvailabilityZoneList: returns list of availability zones for a region

func (*VM) GetIPs

func (vm *VM) GetIPs() ([]net.IP, error)

GetIPs returns a slice of IP addresses assigned to the VM. The PublicIP or PrivateIP consts can be used to retrieve respective IP address type. It returns nil if there was an error obtaining the IPs.

func (*VM) GetImageList

func (vm *VM) GetImageList() ([]Image, error)

GetImageList: returns list of images available for given account Includes public,owned private images & private images with explicit permission

func (*VM) GetName

func (vm *VM) GetName() string

GetName returns the name of the virtual machine

func (*VM) GetRegionList

func (vm *VM) GetRegionList() ([]Region, error)

GetRegionList: returns list of regions

func (*VM) GetSSH

func (vm *VM) GetSSH(options ssh.Options) (ssh.Client, error)

GetSSH returns an SSH client that can be used to connect to a VM. An error is returned if the VM has no IPs.

func (*VM) GetSecurityGroupList

func (vm *VM) GetSecurityGroupList() ([]SecurityGroup, error)

GetSecurityGroupList : returns list of all securityGroup for given region most relevant filter(s) (map-keys): "vpc-id", "group-id" See all available filters at below link http://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#DescribeSecurityGroupsInput

func (*VM) GetState

func (vm *VM) GetState() (string, error)

GetState returns the state of the VM, such as "running". An error is returned if the instance ID is missing, if there was a problem querying AWS, or if there are no instances.

func (*VM) GetSubnetList

func (vm *VM) GetSubnetList() ([]Subnet, error)

GetSubnetList: returns list of all subnet for given region most relevant filter(s) (map-keys): "vpc-id", "subnet-id", "availabilityZone" See all available filters at below link http://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#DescribeSubnetsInput

func (*VM) GetVPCList

func (vm *VM) GetVPCList() ([]VPC, error)

GetVPCList: returns list of VPCs for given region

func (*VM) Halt

func (vm *VM) Halt() error

Halt shuts down the VM on AWS.

func (*VM) Provision

func (vm *VM) Provision() error

Provision creates a virtual machine on AWS. It returns an error if there was a problem during creation, if there was a problem adding a tag, or if the VM takes too long to enter "running" state.

func (*VM) ResetKeyPair

func (vm *VM) ResetKeyPair()

ResetKeyPair resets the key pair for this VM.

func (*VM) Resume

func (vm *VM) Resume() error

Resume always returns an error because this isn't supported by AWS.

func (*VM) RevokeSecurityGroup

func (vm *VM) RevokeSecurityGroup() error

RevokeSecurityGroup: Removes one or more rules from a security group

func (*VM) SetKeyPair

func (vm *VM) SetKeyPair(privateKey string, name string)

SetKeyPair sets the given private key and AWS key name for this vm

func (*VM) SetTag

func (vm *VM) SetTag(key, value string) error

SetTag adds a tag to the VM and its attached volumes.

func (*VM) SetTags

func (vm *VM) SetTags(tags map[string]string) error

SetTags takes in a map of tags to set to the provisioned instance. This is essentially a shorter way than calling SetTag many times.

func (*VM) Start

func (vm *VM) Start() error

Start boots a stopped VM.

func (*VM) Suspend

func (vm *VM) Suspend() error

Suspend always returns an error because this isn't supported by AWS.

func (*VM) ValidateAuth

func (vm *VM) ValidateAuth() error

ValidateAuth: returns error if credentials are incorrect

type VPC

type VPC struct {
	Id         string   `json:"id,omitempty"`
	State      string   `json:"state,omitempty"`
	IsDefault  *bool    `json:"is_default,omitempty"`
	IPv4Blocks []string `json:"ipv4_blocks,omitempty"`
	IPv6Blocks []string `json:"ipv6_blocks,omitempty"`
	// ID of DHCP options associated with VPC
	DhcpOptionsId string `json:"dhcp_options_id,omitempty"`
	// Allowed tenancy of instances launched into the VPC
	InstanceTenancy string `json:"instance_tenancy,omitempty"`
}

VPC represents a AWS VPC

type Zone

type Zone struct {
	Name   string `json:"name,omitempty"`
	State  string `json:"state,omitempty"`
	Region string `json:"region,omitempty"`
}

Zone represents a AWS availability zone

Jump to

Keyboard shortcuts

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