MCPcopy Index your code
hub / github.com/aws/amazon-ecs-agent

github.com/aws/amazon-ecs-agent @v1.105.0

Chat with this repo
repository ↗ · DeepWiki ↗ · release v1.105.0 ↗ · + Follow
14,881 symbols 68,166 edges 1,299 files 7,339 documented · 49%
What it actually does AI analysis from the code graph — generated when you open this
loading…
README

Amazon ECS Container Agent

Amazon ECS logo

The Amazon ECS Container Agent is a component of Amazon Elastic Container Service (Amazon ECS) and is responsible for managing containers on behalf of Amazon ECS.

This repository comes with ECS-Init, which is a systemd based service to support the Amazon ECS Container Agent and keep it running. It is used for systems that utilize systemd as init systems and is packaged as deb or rpm. The source for ECS-Init is available in this repository at ./ecs-init while the packaging is available at ./packaging.

Usage

The best source of information on running this software is the Amazon ECS documentation.

On the Amazon Linux AMI

On the Amazon Linux AMI, we provide an installable RPM which can be used via sudo yum install ecs-init && sudo start ecs. This is the recommended way to run it in this environment.

On Other Linux AMIs

Amazon ECS docs provides deb and rpm packages and instructions to install ECS Container Agent on non-Amazon Linux instances.

The Amazon ECS Container Agent may also be run in a Docker container on an EC2 instance with a recent Docker version installed. Docker images are available in Docker Hub Repository and ECR Public Gallery.

$ # Set up directories the agent uses
$ mkdir -p /var/log/ecs /etc/ecs /var/lib/ecs/data
$ touch /etc/ecs/ecs.config
$ # Set up necessary rules to enable IAM roles for tasks
$ sysctl -w net.ipv4.conf.all.route_localnet=1
$ iptables -t nat -A PREROUTING -p tcp -d 169.254.170.2 --dport 80 -j DNAT --to-destination 127.0.0.1:51679
$ iptables -t nat -A OUTPUT -d 169.254.170.2 -p tcp -m tcp --dport 80 -j REDIRECT --to-ports 51679
$ # Run the agent
$ docker run --name ecs-agent \
    --detach=true \
    --restart=on-failure:10 \
    --volume=/var/run/docker.sock:/var/run/docker.sock \
    --volume=/var/log/ecs:/log \
    --volume=/var/lib/ecs/data:/data \
    --net=host \
    --env-file=/etc/ecs/ecs.config \
    --env=ECS_LOGFILE=/log/ecs-agent.log \
    --env=ECS_DATADIR=/data/ \
    --env=ECS_ENABLE_TASK_IAM_ROLE=true \
    --env=ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=true \
    amazon/amazon-ecs-agent:latest

On Other Linux AMIs when awsvpc networking mode is enabled

For the AWS VPC networking mode, ECS agent requires CNI plugin and dhclient to be available. ECS also needs the ecs-init to run as part of its startup. The following is an example of docker run configuration for running ecs-agent with Task ENI enabled. Note that ECS agent currently only supports cgroupfs for cgroup driver.

$ # Run the agent
$ /usr/bin/docker run --name ecs-agent \
--init \
--restart=on-failure:10 \
--volume=/var/run:/var/run \
--volume=/var/log/ecs/:/log:Z \
--volume=/var/lib/ecs/data:/data:Z \
--volume=/etc/ecs:/etc/ecs \
--volume=/sbin:/host/sbin \
--volume=/lib:/lib \
--volume=/lib64:/lib64 \
--volume=/usr/lib:/usr/lib \
--volume=/usr/lib64:/usr/lib64 \
--volume=/proc:/host/proc \
--volume=/sys/fs/cgroup:/sys/fs/cgroup \
--net=host \
--env-file=/etc/ecs/ecs.config \
--cap-add=sys_admin \
--cap-add=net_admin \
--env ECS_ENABLE_TASK_ENI=true \
--env ECS_UPDATES_ENABLED=true \
--env ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION=1h \
--env ECS_DATADIR=/data \
--env ECS_ENABLE_TASK_IAM_ROLE=true \
--env ECS_ENABLE_TASK_IAM_ROLE_NETWORK_HOST=true \
--env ECS_LOGFILE=/log/ecs-agent.log \
--env ECS_AVAILABLE_LOGGING_DRIVERS='["json-file","awslogs","syslog","none"]' \
--env ECS_LOGLEVEL=info \
--detach \
amazon/amazon-ecs-agent:latest

See also the Advanced Usage section below.

On the ECS Optimized Windows AMI

ECS Optimized Windows AMI ships with a pre-installed PowerShell module called ECSTools to install, configure, and run the ECS Agent as a Windows service. To install the service, you can run the following PowerShell commands on an EC2 instance. To launch into another cluster instead of windows, replace the 'windows' in the script below with the name of your cluster.

PS C:\> Import-Module ECSTools
PS C:\> # The -EnableTaskIAMRole option is required to enable IAM roles for tasks.
PS C:\> Initialize-ECSAgent -Cluster 'windows' -EnableTaskIAMRole

Downloading Different Version of ECS Agent

To download different version of ECS Agent, you can do the following:

PS C:\> # use agentVersion = "latest" for the latest available agent version
PS C:\> $agentVersion = "v1.20.4"
PS C:\> Initialize-ECSAgent -Cluster 'windows' -EnableTaskIAMRole -Version $agentVersion

Build ECS Agent from source

Build ECS Agent Image (Linux)

ECS Agent can also be built locally from source on a linux machine. Use the following steps to build ECS Agent * Get ECS Agent source

git clone https://github.com/aws/amazon-ecs-agent.git
  • Build Agent image using release-agent make target
make release-agent

This installs the required build dependencies, builds ECS Agent image and saves it at a path ecs-agent-v${AGENT_VERSION}.tar. Load this using

docker load < ecs-agent-v${AGENT_VERSION}.tar

Follow the instructions above to continue with the installation

Build and run standalone (Linux)

The Amazon ECS Container Agent may also be run outside of a Docker container as a Go binary. At this time, this is not recommended for production on Linux, but it can be useful for development or easier integration with your local Go tools.

The following commands run the agent outside of Docker:

make gobuild
./out/amazon-ecs-agent

Standalone (Windows)

The Amazon ECS Container Agent may be built by invoking scripts\build_agent.ps1

Scripts (Windows)

The following scripts are available to help develop the Amazon ECS Container Agent on Windows:

  • scripts\run-integ-tests.ps1 - Runs all integration tests in the engine and stats packages
  • misc\windows-deploy\Install-ECSAgent.ps1 - Install the ECS agent as a Windows service
  • misc\windows-deploy\amazon-ecs-agent.ps1 - Helper script to set up the host and run the agent as a process
  • misc\windows-deploy\user-data.ps1 - Sample user-data that can be used with the Windows Server 2016 with Containers AMI to run the agent as a process

Build ECS-Init Package (Linux)

ECS-Init package can also be built as a deb or rpm depending on the linux system you are running. Follow instructions at generic-deb-integrated or generic-rpm-integrated to build and install ECS Agent with Init using deb or rpm.

Advanced Usage

The Amazon ECS Container Agent supports a number of configuration options, most of which should be set through environment variables.

Environment Variables

The table below provides an overview of optional environment variables that can be used to configure the ECS agent. See the Amazon ECS developer guide for additional details about how to configure the agent.

Environment Key Example Value(s) Description Default value on Linux Default value on Windows
ECS_CLUSTER clusterName The cluster this agent should check into. default default
ECS_RESERVED_PORTS [22, 80, 5000, 8080] An array of ports that should be marked as unavailable for scheduling on this container instance. [22, 2375, 2376, 51678, 51679] [53, 135, 139, 445, 2375, 2376, 3389, 5985, 5986, 51678, 51679]
ECS_RESERVED_PORTS_UDP [53, 123] An array of UDP ports that should be marked as unavailable for scheduling on this container instance. [] []
ECS_ENGINE_AUTH_TYPE "docker" | "dockercfg" The type of auth data that is stored in the ECS_ENGINE_AUTH_DATA key.
ECS_ENGINE_AUTH_DATA See the dockerauth documentation Docker auth data formatted as defined by ECS_ENGINE_AUTH_TYPE.
AWS_DEFAULT_REGION <us-west-2>|<us-east-1>|… The region to be used in API requests as well as to infer the correct backend host. Taken from Amazon EC2 instance metadata. Taken from Amazon EC2 instance metadata.
DOCKER_HOST unix:///var/run/docker.sock Used to create a connection to the Docker daemon; behaves similarly to this environment variable as used by the Docker client. unix:///var/run/docker.sock npipe:////./pipe/docker_engine
ECS_LOGLEVEL <crit> | <error> | <warn> | <info> | <debug> The level of detail to be logged. info info
ECS_LOGLEVEL_ON_INSTANCE <none> | <crit> | <error> | <warn> | <info> | <debug> Can be used to override ECS_LOGLEVEL and set a level of detail that should be logged in the on-instance log file, separate from the level that is logged in the logging driver. If a logging driver is explicitly set, on-instance logs are turned off by default, but can be turned back on with this variable. none if ECS_LOG_DRIVER is explicitly set to a non-empty value; otherwise the same value as ECS_LOGLEVEL none if ECS_LOG_DRIVER is explicitly set to a non-empty value; otherwise the same value as ECS_LOGLEVEL
ECS_LOGFILE /ecs-agent.log The location where logs should be written. Log level is controlled by ECS_LOGLEVEL. blank blank
ECS_CHECKPOINT <true | false> Whether to checkpoint state to the DATADIR specified below. true if ECS_DATADIR is explicitly set to a non-empty value; false otherwise true if ECS_DATADIR is explicitly set to a non-empty value; false otherwise
ECS_DATADIR /data/ The container path where state is checkpointed for use across agent restarts. Note that on Linux, when you specify this, you will need to make sure that the Agent container has a bind mount of $ECS_HOST_DATA_DIR/data:$ECS_DATADIR with the corresponding values of ECS_HOST_DATA_DIR and ECS_DATADIR. /data/ C:\ProgramData\Amazon\ECS\data
ECS_UPDATES_ENABLED <true | false> Whether to exit for an updater to apply updates when requested. false false
ECS_DISABLE_METRICS <true | false> Whether to disable metrics gathering for tasks. false false
ECS_POLL_METRICS <true | false> Whether to poll or stream when gathering metrics for tasks. Setting this value to true can help reduce the CPU usage of dockerd and containerd on the ECS container instance. See also ECS_POLL_METRICS_WAIT_DURATION for setting the poll interval. false false
ECS_POLLING_METRICS_WAIT_DURATION 10s Time to wait between polling for metrics for a task. Not used when ECS_POLL_METRICS is false. Maximum value is 20s and minimum value is 5s. If user sets above maximum it will be set to max, and if below minimum it will be set to min. As the number of tasks/containers increase, a higher ECS_POLLING_METRICS_WAIT_DURATION value can potentially cause a problem where memory reservation value of ECS cluster reported in metrics becomes unstable due to missing metrics sample at metric collection time. It is recommended to keep this value smaller than 18s. This behavior is only observed on certain OS and platforms. 10s 10s
ECS_PULL_DEPENDENT_CONTAINERS_UPFRONT <true | false> Whether to pull images for containers with dependencies before the dependsOn condition has been satisfied. false false
ECS_RESERVED_MEMORY 32 Reduction, in MiB, of the memory capacity of the instance that is reported to Amazon ECS. Used by Amazon ECS when placing tasks on container instances. This doesn't reserve memory usage on the instance. 0 0
ECS_AVAILABLE_LOGGING_DRIVERS ["awslogs","fluentd","gelf","json-file","journald","logentries","splunk","syslog"] Which logging drivers are available on the container instance. ["json-file","none"] ["json-file","none"]
ECS_DISABLE_PRIVILEGED true Whether launching privileged containers is disabled on the container instance. false false
ECS_SELINUX_CAPABLE true Whether SELinux is available on the container instance. (Limited support; Z-mode mounts only.) false false
ECS_APPARMOR_CAPABLE true Whether AppArmor is available on the container instance. false false
ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION 10m Default time to wait to delete containers for a stopped task (see also ECS_ENGINE_TASK_CLEANUP_WAIT_DURATION_JITTER). If set to less than 1 second, the value is ignored. 3h 3h
`ECS_ENG

Extension points exported contracts — how you extend this code

Config (Interface)
Config is a general interface represents all kinds of plugin configs [191 implementers]
ecs-agent/netlib/model/ecscni/plugin.go
Idempotent (Interface)
Idempotent is the interface required to manage in flight requests. [191 implementers]
ecs-agent/daemonimages/csidriver/driver/internal/inflight.go
ClientFactory (Interface)
ClientFactory interface abstracts the method that creates new ClientServer objects. This is helpful when writing unit te [14 …
ecs-agent/wsclient/client_factory.go
NamedError (Interface)
NamedError defines an interface that wraps error and add additional 'ErrorName' method [38 implementers]
ecs-agent/api/errors/error_types.go
UUIDProvider (Interface)
UUIDProvider wraps 'uuid' methods for testing [14 implementers]
agent/utils/uuid.go
TaskResource (Interface)
TaskResource is a wrapper for task level resource methods we need [11 implementers]
agent/taskresource/interface.go
ECRFactory (Interface)
ECRFactory defines the interface to produce an ECR SDK client [6 implementers]
agent/ecr/factory.go
VolumeDriver (Interface)
go:generate mockgen.sh $GOPACKAGE_mock $GOFILE ./mock VolumeDriver contains the methods for volume drivers to implement [5 …
ecs-init/volumes/driver/driver.go

Core symbols most depended-on inside this repo

String
called by 2162
ecs-agent/netlib/model/ecscni/plugin.go
Run
called by 914
ecs-agent/utils/execwrapper/exec.go
Error
called by 898
agent/engine/dependencygraph/graph.go
Add
called by 731
ecs-agent/netlib/model/ecscni/plugin.go
EXPECT
called by 550
agent/engine/mocks/engine_mocks.go
New
called by 540
ecs-agent/metrics/metrics.go
Len
called by 507
agent/engine/execcmd/agent_version.go
Error
called by 492
ecs-agent/logger/custom_receiver.go

Shape

Method 6,954
Function 5,981
Struct 1,594
Interface 200
TypeAlias 121
FuncType 30
Class 1

Languages

Go100%
C1%

Modules by API surface

aws-sdk-go-v2/service/ecs/deserializers.go493 symbols
ecs-init/gpu/mocks/mock_gpu_device.go458 symbols
aws-sdk-go-v2/service/ecs/serializers.go357 symbols
ecs-agent/acs/model/ecsacs/api.go326 symbols
aws-sdk-go-v2/service/ecs/validators.go294 symbols
agent/api/task/task.go186 symbols
ecs-agent/tcs/model/ecstcs/api.go169 symbols
agent/api/task/task_test.go152 symbols
aws-sdk-go-v2/service/ecs/types/errors.go130 symbols
agent/api/container/container.go128 symbols
ecs-agent/gmsacredclient/credentialsfetcher/credentialsfetcher.pb.go126 symbols
aws-sdk-go-v2/service/ecs/types/enums.go124 symbols

For agents

$ claude mcp add amazon-ecs-agent \
  -- python -m otcore.mcp_server <graph>

⬇ download graph artifact

Ask about this repo answers extend the page