DevOps with serverless Jenkins and AWS Cloud Development Kit (AWS CDK)
Jenkins is a popular open-source automation server that provides hundreds of plugins to support building, testing, deploying, and automation. Jenkins uses a controller-agent architecture in which the controller is responsible for serving the web UI, stores the configurations and related data on disk, and delegates the jobs to the worker agents that run these jobs as their primary responsibility.
Amazon Elastic Container Service (Amazon ECS) using Fargate is a fully-managed container orchestration service that helps you easily deploy, manage, and scale containerized applications. It deeply integrates with the rest of the AWS platform to provide a secure and easy-to-use solution for running container workloads in the cloud and now on your infrastructure. Fargate is a serverless, pay-as-you-go compute engine that lets you focus on building applications without managing servers. Fargate is compatible with both Amazon ECS and Amazon Elastic Kubernetes Service (Amazon EKS).
The following diagram illustrates the solution architecture. The dashed lines indicate the AWS CDK deployment.
You’ll be using the following:
- The Jenkins controller URL backed by an Application Load Balancer (ALB).
- You’ll be using your default Amazon Virtual Private Cloud (Amazon VPC) for this example.
- The Jenkins controller runs as a service in Amazon ECS using Fargate as the launch type. You’ll use Amazon Elastic File System (Amazon EFS) as the persistent backing store for the Jenkins controller task. The Jenkins controller and Amazon EFS are launched in private subnets.
For this post, you’ll utilize AWS CDK using TypeScript.
Follow the guide on Getting Started for AWS CDK to:
- Get your local environment setup
- Bootstrap your development account
Let’s review the code used to define the Jenkins environment in AWS using the AWS CDK.
Setup your imports
Setup your Amazon ECS, which is a logical grouping of tasks or services and set vpc
Setup Amazon EFS to store the data
Setup Access Point, which are application-specific entry points into an Amazon EFS file system that makes it easier to manage application access to shared datasets
Setup Task Definition to run Docker containers in Amazon ECS
Setup a Volume mapping the Amazon EFS from above to the Task Definition
Setup the Container using the Task Definition and the Jenkins image from the registry
Setup Mount Points to bind ephemeral storage to the container
Setup Fargate Service to run the container serverless
Setup ALB and add listener to checks for connection requests, using the protocol and port that you configure.
Setup Target to route requests to Jenkins running on Amazon ECS using Fargate
Now that you have all the code, let’s deploy the AWS CDK definition:
- Make sure that you have done the Prerequisite steps from earlier.
- Install packages by running the following command in your IDE CLI:
- Now you’ll deploy your AWS CDK definition to your dev account:
Let’s now login to Jenkins
- In your browser, use the DNS Name from the deployed Load Balancer
- In Amazon CloudWatch, there will be a Log group that will be created that is associated to Cluster Service.
- Go into that log and you’ll see it output the Password to login to Jenkins
- In Jenkins, follow the wizard to continue the setup
To avoid incurring future charges, delete the resources.
Let’s destroy our deploy solution
- In your IDE CLI:
With this overview we were able to cover the following:
- Build an Elastic Load Balancer
- Use AWS Fargate with a Jenkins AMI
- All resources running serverlessly
- All build using the AWS CDK
About the author: