ecstaskports

package module
v0.0.0-...-095ab07 Latest Latest
Warning

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

Go to latest
Published: Apr 2, 2018 License: MIT Imports: 14 Imported by: 0

README

ECS Task Ports

This is a Go library for use by microservices running in an AWS Elastic Container Service environment that wish to know their port mappings to update service discovery systems.

For example a microservice might bind to the port 8080 (the container port) but when run as a container using a Docker bridge network then the port is remapped to port 32481 (the host port).

This library supports two methods for determining the container's ports:

The simplest and fastest solution (which also doesn't require any IAM permissions) is to use container metadata files. To enable this your EC2 instance must be running at least ECS container agent 1.15.0 or greater.

Either add ECS_ENABLE_CONTAINER_METADATA=true to the /etc/ecs/ecs.config file or ensure the container agent start with ECS_ENABLE_CONTAINER_METADATA=true in it's environment.

The second option using agent introspection takes a bit longer and requires three API calls - two to the ECS container agent, and another to the ECS API. Your task will also need the ecs:DescribeTasks IAM permission.

API

package main
import "github.com/alexbilbie/ecs-task-ports"

func main() {
    addresses, err := ecstaskports.Discover()
    if err != nil {
        // handle err...
    }

    for _, addr := range addresses {
        _ := os.Setenv(
            fmt.Sprintf("PORT_%d", addr.ContainerPort),
            fmt.Sprintf("%d", addr.HostPort),
        )
    }

    // serviceDiscovery.UpdateAdvertiseAddr(os.Getenv("PORT_8080"))
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetEC2InstanceAvailabilityZone

func GetEC2InstanceAvailabilityZone() ([]byte, error)

GetEC2InstanceAvailabilityZone returns the availability zone that an EC2 instance is running in

func GetEC2InstancePrivateIPAddress

func GetEC2InstancePrivateIPAddress() ([]byte, error)

GetEC2InstancePrivateIPAddress returns the private IP address of an EC2 instance

func GetEC2InstanceRegion

func GetEC2InstanceRegion() string

GetEC2InstanceRegion automatically discovers the AWS region from the runtime environment

Types

type PortMapping

type PortMapping struct {
	ContainerPort int
	HostPort      int
}

PortMapping struct contains the container port (which is the original EXPOSEd port in a container) and the host port which is the port mapped to the container port

func Discover

func Discover() ([]PortMapping, error)

Discover will attempt to return an array ofPortMapping, or an error

Jump to

Keyboard shortcuts

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