Amazon Elastic Container Service Documentation¶
Elastic Container Service is a container-as-a-service feature of AWS.
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]¶ -
connectionCls
¶ alias of
ECSJsonConnection
-
create_cluster
(name, location=None)[source]¶ Create a container cluster
Parameters: - name (
str
) – The name of the cluster - location (
libcloud.container.base.ClusterLocation
) – The location to create the cluster in
Return type: - name (
-
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: - name (
str
) – The name of the new container - image (
libcloud.container.base.ContainerImage
) – The container image to deploy - cluster (
libcloud.container.base.ContainerCluster
) – The cluster to deploy to, None is default - parameters (
str
) – Container Image parameters - start (
bool
) – Start the container on deployment
Return type: - name (
-
destroy_cluster
(cluster)[source]¶ Delete a cluster
Returns: True
if the destroy was successful, otherwiseFalse
.Return type: bool
-
destroy_container
(container)[source]¶ Destroy a deployed container
Parameters: container ( libcloud.container.base.Container
) – The container to destroyReturn type: libcloud.container.base.Container
-
ecrConnectionClass
¶ alias of
ECRJsonConnection
-
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- name (
-
ex_describe_service
(service_arn)[source]¶ Get the details of a service
Parameters: - cluster (
libcloud.container.base.ContainerCluster
) – The hosting cluster - service_arn (
str
) – The service ARN to describe
Returns: The service object
Return type: object
- cluster (
-
ex_destroy_service
(service_arn)[source]¶ Deletes a service
Parameters: - cluster (
libcloud.container.base.ContainerCluster
) – The target cluster - service_arn (
str
) – The service ARN to destroy
- cluster (
-
ex_get_registry_client
(repository_name)[source]¶ Get a client for an ECR repository
Parameters: repository_name ( str
) – The unique name of the repositoryReturns: 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 repositoryReturns: 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 repositoryReturns: 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
ofstr
) – The list of ARNsReturn type: list
oflibcloud.container.base.Container
-
ex_list_service_arns
(cluster=None)[source]¶ List the services
Parameters: cluster ( libcloud.container.base.ContainerCluster
) – The cluster hosting the servicesReturn type: list
ofstr
-
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: - task_arn (
-
get_cluster
(id)¶ Get a cluster by ID
Parameters: id ( str
) – The ID of the cluster to getReturn type: ContainerCluster
-
get_container
(id)[source]¶ Get a container by ID
Parameters: id ( str
) – The ID of the container to getReturn type: libcloud.container.base.Container
-
install_image
(path)¶ Install a container image from a remote path.
Parameters: path ( str
) – Path to the container imageReturn 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 inReturn type: list
oflibcloud.container.base.ContainerCluster
-
list_containers
(image=None, cluster=None)[source]¶ List the deployed container images
Parameters: - image (
libcloud.container.base.ContainerImage
) – Filter to containers with a certain image - cluster (
libcloud.container.base.ContainerCluster
) – Filter to containers in a cluster
Return type: - image (
-
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
oflibcloud.container.base.ContainerImage
-
list_locations
()¶ Get a list of potential locations to deploy clusters into
Return type: list
ofClusterLocation
-
restart_container
(container)[source]¶ Restart a deployed container
Parameters: container ( libcloud.container.base.Container
) – The container to restartReturn type: libcloud.container.base.Container
-
start_container
(container, count=1)[source]¶ Start a deployed task
Parameters: - container (
libcloud.container.base.Container
) – The container to start - count (
int
) – Number of containers to start
Return type: - container (
-
stop_container
(container)[source]¶ Stop a deployed container
Parameters: container ( libcloud.container.base.Container
) – The container to stopReturn type: libcloud.container.base.Container
-