Skip to content
This repository has been archived by the owner on Jun 26, 2022. It is now read-only.
/ ecs-task-ports Public archive

A Golang library for helping microservices running in Elastic Container Service discovery the ECS instance IP address and exposed port

License

Notifications You must be signed in to change notification settings

alexbilbie/ecs-task-ports

Repository files navigation

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"))
}

About

A Golang library for helping microservices running in Elastic Container Service discovery the ECS instance IP address and exposed port

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages