How to Setup AWS ECS Cluster as Build Slave for Jenkins

Selvakumar - Apr 12 '23 - - Dev Community

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

  • Choose the "Networking only" and then click "Next step"

choose fargate

  • 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

Step#2: (Setup ECS task execution IAM role)

  • Goto IAM, select the "Roles". Then click the "create role" button.

Image description

  • 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

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

Image description

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

Image description

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
  • 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

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

Step#7: (Install ECS plugin on Jenkins)

  • Goto "Manage Jenkins", then click the "Plugins".
  • Install the "Amazon Elastic Container Service(ECS)/Fargate".

Image description

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

  • 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

  • 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
  • Network mode - > Select "awsvpc"
  • Soft Memory Reservation and CPU units give base on the doc. 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

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

Image description

_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

  • 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
  • On the build step just put some shell command. Then save and run the job

Image description

  • The slave container is PROVISIONING Image description
  • Console output

Image description

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

. . . . . . . . . . . .