Deploying Jenkins using AWS CDK and Typescript

Aditya Nama
3 min readJul 20, 2023

In today’s fast-paced software development world, continuous integration and continuous deployment (CI/CD) have become essential practices to ensure the efficiency and reliability of software delivery.

Jenkins, an open-source automation server, has been a popular choice for implementing CI/CD pipelines. In this blog, we will explore how to deploy Jenkins using AWS CDK and Typescript on EC2 instance.

You can utilise the AWS CDK’s official getting started guide.

The code.

The security group SecurityGroupJenkins allows inbound traffic on port 8080 from any IPv4 address (0.0.0.0/0). This makes the Jenkins server accessible to the public internet, which is generally not recommended. It’s better to restrict the inbound access to only necessary IP addresses.

The user data script below installs and sets up ssm-agent & Jenkins along with its dependencies, including Java, using the package manager.

#!/bin/bash

sudo apt update -y
sudo apt install snapd -y
sudo snap install amazon-ssm-agent --classic
sudo snap list amazon-ssm-agent
sudo snap start amazon-ssm-agent
sudo apt install openjdk-17-jre -y

curl -fsSL https://pkg.jenkins.io/debian-stable/jenkins.io-2023.key | sudo tee \
/usr/share/keyrings/jenkins-keyring.asc > /dev/null
echo deb [signed-by=/usr/share/keyrings/jenkins-keyring.asc] \
https://pkg.jenkins.io/debian-stable binary/ | sudo tee \
/etc/apt/sources.list.d/jenkins.list > /dev/null
sudo apt-get update -y
sudo apt-get install jenkins -y

sudo systemctl start jenkins

Now that we’ve defined our Jenkins stack, it’s time to deploy it using:

cdk deploy

Once the deployment is complete, you can go to the EC2 Dashboard and select the instance running Jenkins.

Type http://<Public IPv4 address>:8080 in the browser.

To find the admin password, go to instance connect and use session manager.

Enter the following command:

sudo cat /var/lib/jenkins/secrets/initialAdminPassword

Remember that this is just the starting point, and there are many customizations and improvements you can make to your Jenkins deployment depending on your specific use cases and requirements. Happy coding and automating your software delivery with Jenkins on AWS CDK!

Please share your thoughts about this blog post in the comments box below. Reach out to me on Twitter at @adiintify if you have any questions or require access to the entire repository.

--

--