Amazon Elastic Container Service Documentation

Elastic Container Service is a container-as-a-service feature of AWS.

../../_images/aws.png

To provide API key access, you should apply one of the roles: * AmazonEC2ContainerServiceFullAccess * AmazonEC2ContainerServiceReadOnlyAccess

Instantiating the driver

from libcloud.container.types import Provider
from libcloud.container.providers import get_driver

cls = get_driver(Provider.ECS)

conn = cls(access_id='SDHFISJDIFJSIDFJ',
           secret='THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H',
           region='ap-southeast-2')

for container in conn.list_containers():
    print(container.name)

for cluster in conn.list_clusters():
    print(cluster.name)

Deploying a container

from libcloud.container.base import ContainerImage
from libcloud.container.types import Provider
from libcloud.container.providers import get_driver

cls = get_driver(Provider.ECS)

conn = cls(access_id='SDHFISJDIFJSIDFJ',
           secret='THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H',
           region='ap-southeast-2')

for cluster in conn.list_clusters():
    print(cluster.name)
    if cluster.name == 'default':
        container = conn.deploy_container(
            cluster=cluster,
            name='my-simple-app',
            image=ContainerImage(
                id=None,
                name='simple-app',
                path='simple-app',
                version=None,
                driver=conn
            )
        )

Deploying a container from Docker Hub

Docker Hub Client HubClient is a shared utility class for interfacing to the public Docker Hub Service.

You can use this class for fetching images to deploy to services like ECS

from libcloud.container.types import Provider
from libcloud.container.providers import get_driver
from libcloud.container.utils.docker import HubClient

cls = get_driver(Provider.ECS)

conn = cls(access_id='SDHFISJDIFJSIDFJ',
           secret='THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H',
           region='ap-southeast-2')
hub = HubClient()

image = hub.get_image('ubuntu', 'latest')

for cluster in conn.list_clusters():
    print(cluster.name)
    if cluster.name == 'default':
        container = conn.deploy_container(
            cluster=cluster,
            name='my-simple-app',
            image=image)

Deploying a container from Amazon Elastic Container Registry (ECR)

Amazon ECR is a combination of the Docker Registry V2 API and a proprietary API. The ECS driver includes methods for talking to both APIs.

Docker Registry API Client RegistryClient is a shared utility class for interfacing to the public Docker Hub Service.

You can use a factory method to generate an instance of RegsitryClient from the ECS driver. This will request a 12 hour token from the Amazon API and instantiate a RegistryClient object with those credentials.

from libcloud.container.types import Provider
from libcloud.container.providers import get_driver

cls = get_driver(Provider.ECS)

# Connect to AWS
conn = cls(access_id='SDHFISJDIFJSIDFJ',
           secret='THIS_IS)+_MY_SECRET_KEY+I6TVkv68o4H',
           region='ap-southeast-2')

# Get a Registry API client for an existing repository
client = conn.ex_get_registry_client('my-image')

# List all the images
for image in client.list_images('my-image'):
    print(image.name)

# Get a specific image
image = client.get_image('my-image', '14.04')

print(image.path)
# >> 647433528374.dkr.ecr.region.amazonaws.com/my-image:14.04

# Deploy that image
cluster = conn.list_clusters()[0]
container = conn.deploy_container(
    cluster=cluster,
    name='my-simple-app',
    image=image
)

API Docs

class libcloud.container.drivers.ecs.ElasticContainerDriver(access_id, secret, region)[source]
create_cluster(name, location=None)[source]

Create a container cluster

Parameters:
Return type:

libcloud.container.base.ContainerCluster

deploy_container(name, image, cluster=None, parameters=None, start=True, ex_cpu=10, ex_memory=500, ex_container_port=None, ex_host_port=None)[source]

Creates a task definition from a container image that can be run in a cluster.

Parameters:
Return type:

libcloud.container.base.Container

destroy_cluster(cluster)[source]

Delete a cluster

Returns:True if the destroy was successful, otherwise False.
Return type:bool
destroy_container(container)[source]

Destroy a deployed container

Parameters:container (libcloud.container.base.Container) – The container to destroy
Return type:libcloud.container.base.Container
ex_create_service(name, cluster, task_definition, desired_count=1)[source]

Runs and maintains a desired number of tasks from a specified task definition. If the number of tasks running in a service drops below desired_count, Amazon ECS spawns another instantiation of the task in the specified cluster.

Parameters:
  • name (str) – the name of the service
  • cluster (libcloud.container.base.ContainerCluster) – The cluster to run the service on
  • task_definition (str) – The task definition name or ARN for the service
  • desired_count (int) – The desired number of tasks to be running at any one time
Return type:

object The service object

ex_describe_service(service_arn)[source]

Get the details of a service

Parameters:
Returns:

The service object

Return type:

object

ex_destroy_service(service_arn)[source]

Deletes a service

Parameters:
ex_get_registry_client(repository_name)[source]

Get a client for an ECR repository

Parameters:repository_name (str) – The unique name of the repository
Returns:a docker registry API client
Return type:libcloud.container.utils.docker.RegistryClient
ex_get_repository_id(repository_name)[source]

Get the ID of a repository

Parameters:repository_name (str) – The unique name of the repository
Returns:The repository ID
Return type:str
ex_get_repository_token(repository_id)[source]

Get the authorization token (12 hour expiry) for a repository

Parameters:repository_id (str) – The ID of the repository
Returns:A token for login
Return type:str
ex_list_containers_for_task(task_arns)[source]

Get a list of containers by ID collection (ARN)

Parameters:task_arns (list of str) – The list of ARNs
Return type:list of libcloud.container.base.Container
ex_list_service_arns(cluster=None)[source]

List the services

Parameters:cluster (libcloud.container.base.ContainerCluster) – The cluster hosting the services
Return type:list of str
ex_start_task(task_arn, count=1)[source]

Run a task definition and get the containers

Parameters:
  • task_arn (str) – The task ARN to Run
  • count (int) – The number of containers to start
Return type:

list of libcloud.container.base.Container

get_cluster(id)

Get a cluster by ID

Parameters:id (str) – The ID of the cluster to get
Return type:ContainerCluster
get_container(id)[source]

Get a container by ID

Parameters:id (str) – The ID of the container to get
Return type:libcloud.container.base.Container
install_image(path)

Install a container image from a remote path.

Parameters:path (str) – Path to the container image
Return type:ContainerImage
list_clusters()[source]

Get a list of potential locations to deploy clusters into

Parameters:location (libcloud.container.base.ClusterLocation) – The location to search in
Return type:list of libcloud.container.base.ContainerCluster
list_containers(image=None, cluster=None)[source]

List the deployed container images

Parameters:
Return type:

list of libcloud.container.base.Container

list_images(ex_repository_name)[source]

List the images in an ECR repository

Parameters:ex_repository_name (str) – The name of the repository to check defaults to the default repository.
Returns:a list of images
Return type:list of libcloud.container.base.ContainerImage
list_locations()

Get a list of potential locations to deploy clusters into

Return type:list of ClusterLocation
restart_container(container)[source]

Restart a deployed container

Parameters:container (libcloud.container.base.Container) – The container to restart
Return type:libcloud.container.base.Container
start_container(container, count=1)[source]

Start a deployed task

Parameters:
Return type:

libcloud.container.base.Container

stop_container(container)[source]

Stop a deployed container

Parameters:container (libcloud.container.base.Container) – The container to stop
Return type:libcloud.container.base.Container