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]¶
- Parameters
key (
str
) – API key or username to used (required)secret (
str
) – Secret password to be used (required)secure (
bool
) – Whether to use HTTPS or HTTP. Note: Some providers only support HTTPS, and it is on by default.host (
str
) – Override hostname used for connections.port (
int
) – Override port used for connections.
- Returns
None
- connectionCls¶
alias of
libcloud.container.drivers.ecs.ECSJsonConnection
- create_cluster(name, location=None)[source]¶
Create a container cluster
- Parameters
name (
str
) – The name of the clusterlocation (
libcloud.container.base.ClusterLocation
) – The location to create the cluster in
- Return type
- 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 containerimage (
libcloud.container.base.ContainerImage
) – The container image to deploycluster (
libcloud.container.base.ContainerCluster
) – The cluster to deploy to, None is defaultparameters (
str
) – Container Image parametersstart (
bool
) – Start the container on deployment
- Return type
- 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 destroy- Return type
- ecrConnectionClass¶
alias of
libcloud.container.drivers.ecs.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 servicecluster (
libcloud.container.base.ContainerCluster
) – The cluster to run the service ontask_definition (
str
) – The task definition name or ARN for the servicedesired_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
cluster (
libcloud.container.base.ContainerCluster
) – The hosting clusterservice_arn (
str
) – The service ARN to describe
- Returns
The service object
- Return type
object
- ex_destroy_service(service_arn)[source]¶
Deletes a service
- Parameters
cluster (
libcloud.container.base.ContainerCluster
) – The target clusterservice_arn (
str
) – The service ARN to destroy
- 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
- 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
ofstr
) – The list of ARNs- Return type
- ex_list_service_arns(cluster=None)[source]¶
List the services
- Parameters
cluster (
libcloud.container.base.ContainerCluster
) – The cluster hosting the services- Return 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 Runcount (
int
) – The number of containers to start
- Return type
- get_cluster(id)¶
Get a cluster by ID
- Parameters
id (
str
) – The ID of the cluster to get- Return type
- get_container(id)[source]¶
Get a container by ID
- Parameters
id (
str
) – The ID of the container to get- Return type
- install_image(path)¶
Install a container image from a remote path.
- Parameters
path (
str
) – Path to the container image- Return type
- 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_containers(image=None, cluster=None)[source]¶
List the deployed container images
- Parameters
image (
libcloud.container.base.ContainerImage
) – Filter to containers with a certain imagecluster (
libcloud.container.base.ContainerCluster
) – Filter to containers in a cluster
- Return type
- 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_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 restart- Return type
- start_container(container, count=1)[source]¶
Start a deployed task
- Parameters
container (
libcloud.container.base.Container
) – The container to startcount (
int
) – Number of containers to start
- Return type
- stop_container(container)[source]¶
Stop a deployed container
- Parameters
container (
libcloud.container.base.Container
) – The container to stop- Return type