Using Amazon EC2 Container Service with Jenkins
Jenkins Amazon EC2 Container Service Plugin
Amazon EC2 Container Service (ECS) is AWS' service for Docker container orchestration to let you deploy Docker based applications on a cluster. The Jenkins Amazon EC2 Container Service plugin let you use Amazon ECS Container service to manage Jenkins cloud agents. Jenkins delegates to Amazon ECS the orchestrations the execution builds on Docker based agents. Each Jenkins build is executed on a dedicated Docker container that is wiped-out at the end of the build. The ECS cluster is composed of Amazon EC2 virtual machines instantiated within the boundaries the user's account (typically in an Amazon VPC). These virtual machines can be declared statically or can be managed dynamically by AWS ECS thanks to AWS Auto Scaling and AWS CloudFormation. The later Jenkins agents are connected to the Jenkins controller using the JNLP protocol.
Installing Jenkins Amazon EC2 Container Service Plugin
Navigate to the "Plugin Manager" screen, install the "Amazon EC2 Container Service" plugin and restart Jenkins.
Configuring Jenkins and AWS
Amazon ECS cluster
As a pre-requisite, you must have created an Amazon ECS cluster with associated ECS instances. These instances can be statically associated with the ECS cluster or can be dynamically created with Amazon Auto Scaling. The Jenkins Amazon EC2 Container Service plugin will use this ECS cluster and will create automatically the required Task Definition.
Jenkins System Configuration
Navigate to the "Configure System" screen. In the "Jenkins Location" section, ensure that the "Jenkins URL" is reachable from the the container instances of the Amazon ECS cluster. See the section "Network and firewalls" for more details. At the bottom of the screen, click on "Add a new Cloud" and select "Amazon EC2 Container Service Cloud".
Amazon EC2 Container Service Cloud
Then enter the configuration details of the Amazon EC2 Container Service Cloud:
Name: name for your ECS cloud (e.g. ecs-cloud)
Amazon ECS Credentials: Amazon IAM Access Key with privileges to create Task Definitions and Tasks on the desired ECS cluster
ECS Cluster: desired ECS cluster on which Jenkins will send builds as ECS tasks
ECS Template: click on "Add" to create the desired ECS template or templates
Tunnel connection through: tunnelling options (when Jenkins runs behind a load balancer...).
ECS agent Templates
One or several ECS agent template can be defined for the Amazon EC2 Container Service Cloud. The main reason to create more than one ECS agent template is to use several Docker image to perform build (e.g. java-build-tools, php-build-tools...)
Label: agent labels used in conjunction with the job level configuration "Restrict where the project can be run / Label expression". ECS agent label could identify the Docker image used for the agent (e.g. docker for the jenkinsci/jnlp-agent ).
Docker image: identifier of the Docker image to use to create the agents
Filesystem root: working directory used by Jenkins (e.g. /home/jenkins/).
Memory: number of MiB of memory reserved for the container. If your container attempts to exceed the memory allocated here, the container is killed.
The number of cpu units to reserve for the container. A container instance has 1,024 cpu units for every CPU core.
Override entrypoint: overwritten Docker image entrypoint. Container command can't be overriden as it is used to pass jenkins agent connection parameters.
JVM arguments: additional arguments for the JVM, such as -XX:MaxPermSize or GC options.
Network and firewalls
Running the Jenkins controller and the ECS container instances in the same Amazon VPC and in the same subnet is the simplest setup and default settings will work out-of-the-box.
If you enable network restrictions between the Jenkins controller and the ECS cluster container instances,
Fix the TCP listen port for JNLP agents of the Jenkins controller (e.g. 5000) navigating in the "Manage Jenkins / Configure Global Security" screen
Allow TCP traffic from the ECS cluster container instances to the Jenkins controller on the listen port for JNLP agents (see above) and the HTTP(S) port.
Network Address Translation and Reverse Proxies
In case of Network Address Translation rules between the ECS cluster container instances and the Jenkins controller, ensure that the JNLP agents will use the proper hostname to connect to the Jenkins controller doing on of the following:
Define the proper hostname of the Jenkins controller defining the system property hudson.TcpagentAgentListener.hostName in the launch command
Use the advanced configuration option "Tunnel connection through" in the configuration of the Jenkins Amazon EC2 Container Service Cloud (see above).
Using ECS Agents
The ECS agents can be used for any job and any type of job (Freestyle job, Maven job, Workflow job...), you just have to restrict the execution of the jobs on one of the labels used in the ECS agent Template configuration. Sample with a label named docker: In the console output of the executed builds, you can verify that the build was performed on the ECS cluster checking the agent name that is composed of the ECS cloud name and of a random identifier.
Docker Images for ECS Agents
The Jenkins Amazon EC2 Container Service Cloud can use for the agents all the Docker image designed to act as a Jenkins JNLP agent. Here is a list of compatible Docker images:
jenkinsci/jnlp-agent : Jenkins CI official image to run Docker based JNLP agents
cloudbees/jnlp-agent-with-java-build-tools : a Docker image designed by CloudBees with convenient tools to build java applications (openjdk8, maven, selenium, firefox, aws-cli...)
You can easily extend one of these images to add tools or you can create your own Docker image.
Cyrille Le Clerc
Director, Product Management
Stay up to date
We'll never share your email address and you can opt out at any time, we promise.