{"id":1027,"date":"2017-04-10T05:56:32","date_gmt":"2017-04-10T05:56:32","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=1027"},"modified":"2017-04-10T05:56:32","modified_gmt":"2017-04-10T05:56:32","slug":"setup-ecs-cluster-as-build-slave-jenkins","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=1027","title":{"rendered":"How to Setup AWS ECS Cluster as Build Slave for Jenkins"},"content":{"rendered":"<p>In our<a href=\"https:\/\/devopscube.com\/docker-containers-as-build-slaves-jenkins\/\" rel=\"noopener noreferrer\"> last post<\/a>, we wrote about setting up docker containers as build slaves. Integrating docker into your build pipeline has lots of advantages. Especially when it comes to ECS cluster based build slave setup, the advantages are even more. Few of them are,<\/p>\n<p>1. In teams where continuous development happens, most the time the slave machines will be idle. By using ECS you can save cost by reducing the jenkins slave machines. The cluster capacity increases when there is a need for more resources.<\/p>\n<p>2. Manage build environments in containers with tagged versions as compared to installing several versions of an application on the same machine.<\/p>\n<p>3. Optimal usage of system resources by spinning up containers only when needed.<\/p>\n<h2 id=\"configuring-ecs-cluster-as-build-slave\">Configuring ECS Cluster As Build Slave<\/h2>\n<p>In this post, we will guide you to setup jenkins slaves on an ECS cluster. In this setup, we have the following.<\/p>\n<p>1. A working jenkins master(2.42-latest) running on a container, set up under an ELB.<\/p>\n<p><strong>Note<\/strong>: You can follow <a href=\"https:\/\/discuss.devopscube.com\/t\/how-to-setup-jenkins-using-docker-with-a-host-mount\/83?ref=devopscube.com\" rel=\"noopener noreferrer\">this article<\/a> for setting up the latest Jenkins server using a container.<\/p>\n<p>2. An ECS cluster with 2 Container instances. You can decide on the container instance capacity based on your project needs.<\/p>\n<p><strong>Note:<\/strong> It is better to have an ASG configured for the instances to scale the ECS cluster on demand.<\/p>\n<h3 id=\"prerequisites\">Prerequisites<\/h3>\n<p>1. In ec2 and ELB security groups, you should open the jnlp (50000) port for container slaves to connect to jenkins master.<\/p>\n<p>2. Go to <code>Manage Jenkns<\/code> &#8211;&gt; <code>Configure Global Security<\/code>, check,<code>Enable Security Option<\/code> check <code>fixed<\/code> and enter <code>50000<\/code> as shown in the image below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img1-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>Follow the steps given below for configuring jenkins with your existing ECS cluster.<\/p>\n<h3 id=\"install-amazon-ec2-container-service-cloud-plugin\">Install Amazon EC2 Container Service Cloud Plugin<\/h3>\n<p>1. Go to <code>Manage Jenkins<\/code> &#8211;&gt; <code>Manage Plugins<\/code> and search for <code>Amazon EC2 Container Service Plugin<\/code><\/p>\n<p>2. Install the plugin and restart it.<\/p>\n<h3 id=\"configure-ecs-settings-on-jenkins\">Configure ECS settings on Jenkins<\/h3>\n<p>To integrate ECS with the jekins master, jenkins should have AWS SDK access. You can enable this in two ways.<\/p>\n<p>1. You can add an IAM role with EC2 Container service Full Access to the instance where you have installed the jenkins server.<\/p>\n<p>2. You can add AWS access key and secret key to the Jenkins credentials and use it with the ECS configuration. If you are running jenkins in a container outside ECS, this will be the only available option. If you are running your jenkins server in ECS, then you can assign it a task role having privileges to ECS cluster.<\/p>\n<p>In this tutorial, I am using the access key and secret key stored in Jenkins AWS credentials.<\/p>\n<p>Follow the steps given below for configuring the ECS plugin to integrate the ECS cluster.<\/p>\n<p>1. Go to <code>Manage Jenkins<\/code> &#8211;&gt; <code>Configure System<\/code> and search for cloud option.<\/p>\n<p>2. Click <code>Add a new cloud<\/code> dropdown and select <code>Amazon EC2 Container Service Cloud<\/code><\/p>\n<p>3. You need to fill up the following details.<\/p>\n<p><strong>Name:<\/strong> User defined name.<\/p>\n<p><strong>Amazon ECS Credentials:<\/strong> If you are using AWS access keys, select the relevant credential. If you using AWS role, leave it empty. The cluster will get listed automatically.<\/p>\n<p><strong>Amazon ECS Region Name:<\/strong> AWS region where you have the cluster.<\/p>\n<p><strong>ECS Cluster:<\/strong> Select your ECS cluster from the drop-down.<\/p>\n<p>An example is shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img2-1-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>If you are running jenkins master under an ELB, you need to add the <code>tunnel configuration<\/code> in the advanced section.<\/p>\n<p>The <code>Tunnel connection through<\/code> option should have the elb URL followed by the JNLP port as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img4-1-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>4. Next, you need to add a <code>slave template<\/code> with a docker image which acts as a slave node.<\/p>\n<p>To do this, under <code>ECS slave templates<\/code> in the cloud configuration, click <code>add<\/code> and enter the details. In the following example, I have added a java JNLP slave image with label <code>java-ecs-salve<\/code>. Label is very important, because, we will use the label name in the job to restrict the job to run on containers.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img3-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>After filling out the details, save the configuration.<\/p>\n<h3 id=\"test-the-configuration\">Test The Configuration<\/h3>\n<p>To test the configuration, we will create a <code>Freestyle<\/code> project and try to run it on ECS.<\/p>\n<p>1. Go to <code>New item<\/code> and create a freestyle project.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img5-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>2. Under <code>Restrict where this project can be run<\/code> type the label name you have given in the slave template as shown in the image below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img6-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>3. Under <code>Build<\/code> select the <code>execute shell<\/code> option and type an echo statement as shown below.<\/p>\n<p>echo &#8220;This is a test for ecs slave&#8221;<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img7-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>4. Save and job and click <code>build now<\/code><\/p>\n<p>The build will go the pending start once it deploys the container. Once it executes the shell the container will be destroyed from ECS.<\/p>\n<p>If you click the <code>console output<\/code>, you should get the following output.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/img8-1.jpg\" class=\"kg-image\" alt loading=\"lazy\"><\/figure>\n<p>Once you tested your configuration, you can create actual jobs by integrating with git and the build steps that you want. You need to configure the docker image based on your requirement.<\/p>\n<p>For java based projects you can use the following cloudbees image as slave templates. It contains most of the java build dependencies.<\/p>\n<p>1. <a href=\"https:\/\/hub.docker.com\/r\/cloudbees\/jnlp-slave-with-java-build-tools\/~\/dockerfile\/?ref=devopscube.com\" rel=\"noopener noreferrer\">https:\/\/hub.docker.com\/r\/cloudbees\/jnlp-slave-with-java-build-tools\/~\/dockerfile\/<\/a><\/p>\n<p>For projects other than java, you can use the following cloudbees JNLP image we used in the demo as the base image.<\/p>\n<p>1. <a href=\"https:\/\/hub.docker.com\/r\/jenkinsci\/jnlp-slave\/?ref=devopscube.com\" rel=\"noopener noreferrer\">https:\/\/hub.docker.com\/r\/jenkinsci\/jnlp-slave\/<\/a><\/p>\n<p>ECS Cluster As Build Slave will save time and cost. If you have your applications of AWS, it is worth giving it a try. You can start with few applications and once you gain confidence, you can move the entire build fleet to ECS container based build pipelines.<\/p>\n<p>Contact us at contact@devopscube.com or leave a comment for any help with this implementation.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/setup-ecs-cluster-as-build-slave-jenkins\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Setup AWS ECS Cluster as Build Slave for Jenkins \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/setup-ecs-cluster-as-build-slave-jenkins\/<\/p>\n","protected":false},"author":1,"featured_media":1028,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1027","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1027","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1027"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/1027\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/1028"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1027"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1027"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1027"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}