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"
- Choose the "Networking only" and then click "Next step"
- 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.
Step#2: (Setup ECS task execution IAM role)
- Goto IAM, select the "Roles". Then click the "create role" button.
- 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.
- On the Add permissions section select 'AmazonECSTaskExecutionRolePolicy", then click next.
- Give the name of the role, then click "create role".
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.
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.
Step#7: (Install ECS plugin on Jenkins)
- Goto "Manage Jenkins", then click the "Plugins".
- Install the "Amazon Elastic Container Service(ECS)/Fargate".
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.
- 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.
- 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"
- 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".
- Task Execution Role ARN -> Give the role ARN name wich we were created on step 2.
- ContainerUser -> Given container user is "root".
_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
-
Key:
- 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
- On the build step just put some shell command. Then save and run the job
- The slave container is PROVISIONING
- Console output
Note: The Jenkins and the ecs salve should be on same network.