Deploy a Simple Jenkins on AWS Using Terraform

Continuous Integration, Continuous Delivery, and Continuous Deployment, concepts and terms which work together to automate the lifecycle of apps, in this regard if you want to implement a CI / CD, Jenkins could be the main tool to establish a server to automate this process.

This post is a simple task to deploy a Jenkins taking AWS amazon as IaaS. For getting a big picture we are going to install Jenkins and its plugins, register a user, set up a basic security rule, and finally insert a job as an example so, let’s elaborate further this idea.

Prerequisite:

.- EC2 instance in AWS amazon to test this tech instruction.

.- Install terraform as Infrastructure As Code tool.

Install Jenkins and its job

First of all, we need to watch this section coz it is the key where we can find how Jenkins is being installed by a script bash which was made as a generic script to be useful on different CentOS Linux (well!!! that’s the idea if you don’t choose other SO). This installation will be in 5 parts, firstly we are going to install and starts up Jenkins, then we are going to register a default user and install basic plugins, and finally, register our best friend HelloWorld job which will be a litter example for getting a good start.

Notice the plugins function presents on the script bash is necessary to install all plugins suggested at the moment when we log in on Jenkins the first time, that is part of customizing this tool and is necessary to register our default user.

Additional loops were placed to check the Jenkins server is running, you can watch this on startup and plugins functions.

Do you remember this announcement starting the Jenkins server? If you don’t want to see it anymore you should implement plugins function.

The next job was made as a simple task to introduce how we can add a job in Jenkins by Jenkins CLI, this is a Pipeline job with a string parameter.

Deploy Jenkins as server

General variables are presented in this file, please pay attention to some properties which need to be replaced by your configure AWS CLI variables. See more information in AWS CLI Command Reference.

There are several properties which are need setups:

access_key and secret_key: Access keys that are used as part of the credentials to authenticate the command request.

jenkins_key_name: Name of key pair file that will be useful to connect via ssh to our instance. If you need to make a key pair please handler by the section My security credentials getting in your AWS account. Notice in this example we used key-pair.pem which is located in the Template folder in this project, replace this file and his name in the project with your credentials.

amis: This property can be a list and has the AMI instance id which is easy to find in Amazon Machine Instances, if you want to change this AMI instance please watch out if your AMIhas systemctl installed because Amazon Linux doesn’t support systemclt or service command according to its version based on CentOS / RHEL, so to avoid spending a lot of time searching an AMI instance with his systemctl already configured, try to use the AMI instance which is configured in this project.

Once all properties have been configured, execute command line terraform init to initialize the working folder, terraform plan to watch all deploy plan, and once you have check it please execute command line terraform apply to deploy your infrastructure.

# terraform init

# terraform plan

# terraform apply

Get in Jenkins

One can locate in AWS Management Console, click in instances section and a new instance should be created as below.

Copy the Public DNS and type this value on your browser followed by port 8080 such as is shown in the image, remember all available ports are in the security group section of main.tf file. The user and password are configured as properties in the variables.tf file, so in this case our user and password are “Jenkins”.

Conclusion

Simple and practical, if you want to explore this code please just make a click in this repository. Perhaps, you have watched many installing about Jenkins but this bash and kinds of installing I have used in many installing and it very eases make those basic activities: register a user, register basic job and install plugins. I hope this contribution was helpful to you.

--

--

Marvin Ortiz Balliachi
Deploy a simple Jenkins on AWS amazon

Software engineer … looking to share and contribute with my knowledge