How to Setup AWS ECS Cluster as Build Slave for Jenkins
Tags: aws, ecs, jenkins
Published: 2023-04-12 12:30:46

This is a blog walk through how to set up the AWS ECS Fargate as a build slave for Jenkins.

###Step#1: (ECS cluster setup)
- Login to the AWS
- Goto ECS and click the "Create cluster"

![create cluster](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dldunhmvbheb7y0x2zaz.png)
- Choose the "Networking only" and then click "Next step"


![choose fargate](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cyk5dfebtpo9l0t7mkce.png)

- Fill the cluster name and if you want to create the new VPC just click the "Create VPC" tick box or just leave it. Finally click the "create" button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hwdeqgv1jzjm65hhm0id.png)



###Step#2: (Setup ECS task execution IAM role)
- Goto IAM, select the "Roles". Then click the "create role" button.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pow6v5g89ktlzcnzcjya.png)

- Choose "AWS service" on the trusted entry, then select "Elastic Container Service" on the drop down of use cases for other AWS services, then select the "Elastic Container Service Task". And click next.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/guhie90wspb2k9nzu1e2.png)

- On the Add permissions section select 'AmazonECSTaskExecutionRolePolicy", then click next.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bgf6wkanj58u3q6k9quo.png)

- Give the name of the role, then click "create role".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mlxkabp57eqwklv4ephm.png)


###Step#3: (Create the security Group to ECS Slave)
- Create the security group with JNLP port "50000".

###Step#4: (Install the Jenkins)
- Install the Jenkins on the EC2 if you not have it.[Installation link](https://www.jenkins.io/doc/book/installing/linux/)
- _Note:_ _Open the JNLP port "50000" on the Jenkins machine security group_

###Step#5: (AWS credential config at Jenkins)
- Configure the AWS programmatic access keys and secret key on Jenkins credential. This will help to run the agent on ECS.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fjk373um1r299onpa9fw.png)

###Step#6: (Setup JNLP port on Jenkins settings)
- Goto "Manage Jenkins" on the dashboard.
- Click the "Security" under the security section.
- On the "Agents" setting, Choose the "Fixed" and put the port "50000".
- Then "save" it.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r16u2pthslnu970wfq8w.png)


###Step#7: (Install ECS plugin on Jenkins)
- Goto "Manage Jenkins", then click the "Plugins". 
- Install the "Amazon Elastic Container Service(ECS)/Fargate".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/87hlfoo0x87biyj0ee6w.png)

###Step#8:(Setup the slave configuration on the Jenkins)
- Goto "Manage Jenkins", then choose the "Nodes and Clouds".
- Click "Clouds" on the left side top.
- Choose "Amazon EC2 Container Service Cloud" on the add a new cloud drop down.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dtbrf10se5699wvb9her.png)
- **Name** -> Give name to you cloud config. Then click "Show More".
- **Amazon ECS Credentials** -> Select an AWS credential which we were configured before on step-5.
- **Amazon ECS Region Name** -> choose the region where your cluster is running(we were created the on step 1)
- **ECS Cluster** -> Select the cluster which we were created the on step 1
- Click "Add" under the ECS agent templates.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/us2ps7b1nf7rk1oscrdn.png)
- **Label** -> Give the label name ( this name will use us to configure the agent with job)
- **Template Name** -> Give name to template
- **Type** -> Choose "Fargate"
- **Operating System Family** -> Choose "linux"
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s7j1fl1wagukp146siwr.png)
- **Network mode** - > Select "awsvpc"
- **Soft Memory Reservation and CPU units** give base on the [doc](https://docs.aws.amazon.com/AmazonECS/latest/developerguide/task_definition_parameters.html#task_size). For "i.e. Memory is 2048 means, CPU should be 1024" 
- **Subnets** -> Given the subnets to run agent on vpc network("," is a delimiter). i.e subnet-1,subnet-2
- **Security Groups** -> Give the security group ID which we were created on step 3.
- **Assign Public Ip** -> Tick the check box.
- Then click "Advanced".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/339olg4wai8we3kvtdjm.png)

- **Task Execution Role ARN** -> Give the role ARN name wich we were created on step 2.
- **ContainerUser** -> Given container user is "root".

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3y6k6ioaoaetdym50c6z.png)

_Note: the following log configuration step are "not required", but If you want to see agent logs then config these. 
- **Logging Driver** -> Give "awslogs"
- **Logging Configuration:**  
 _note: you must create the log group (/ecs/jenkins-slave) on cloud watch before you give here._
     
     - **Key:** `awslogs-group` **Value:** `/ecs/jenkins-slave`
     - **Key:** `awslogs-region` **Value:** `us-east-1`
     - **Key:**`awslogs-stream-prefix` **Value:**`ecs` 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e5kdndvjqk6y27x45fop.png)
- Finally, Save the configuration.

#Step#8:(Create the test job)
- On the "Dashboard", add new item.
- Select "Freestyle project".
- Give the label name which we configured on step 7 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pf56os2igtvsuoxwgwyh.png)
- On the build step just put some shell command. Then save and run the job

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9oqqruzecciepoznpmvg.png)
- The slave container is PROVISIONING
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o488j6rfw4ft000sd7lp.png)
- Console output

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/00bv2c4m5pt5wgi7w78z.png)

_Note: The Jenkins and the ecs salve should be on same network._