Installing Jenkins Using Terraform and EC2
Let’s get started with the “what is”
What is Terraform? Terraform an infrastructure as code tool that lets you build, change, and version cloud and on-prem resources safely and efficiently.
What is Jenkins? Jenkins an open source automation server which enables developers around the world to reliably build, test, and deploy their software.
With this project I will be using the AWS Console and spin up my Cloud9. Also we are going to use the Terraform Registry https://registry.terraform.io/-
The Terraform Registry is an interactive resource for discovering a wide selection of integrations (providers), configuration packages (modules), and security rules (policies) for use with Terraform. The Registry includes solutions developed by HashiCorp, third-party vendors, and our Terraform community.
Commands that will be used to execute this project:
terraform init
command initializes a working directory containing Terraform configuration files. This is the first command that should be run after writing a new Terraform configuration or cloning an existing one from version control. It is safe to run this command multiple times.
terraform fmt
command is used to rewrite Terraform configuration files to a canonical format and style. This command applies a subset of the Terraform language style conventions, along with other minor adjustments for readability.
terraform validate
command validates the configuration files in a directory, referring only to the configuration and not accessing any remote services such as remote state, provider APIs, etc.
Validate runs checks that verify whether a configuration is syntactically valid and internally consistent, regardless of any provided variables or existing state. It is thus primarily useful for general verification of reusable modules, including correctness of attribute names and value types.
terraform plan
command creates an execution plan, which lets you preview the changes that Terraform plans to make to your infrastructure. By default, when Terraform creates a plan it:
- Reads the current state of any already-existing remote objects to make sure that the Terraform state is up-to-date.
- Compares the current configuration to the prior state and noting any differences.
- Proposes a set of change actions that should, if applied, make the remote objects match the configuration.
terraform apply
command executes the actions proposed in a Terraform plan.
terraform destroy
command is a convenient way to destroy all remote objects managed by a particular Terraform configuration.
While you will typically not want to destroy long-lived objects in a production environment, Terraform is sometimes used to manage ephemeral infrastructure for development purposes, in which case you can use terraform destroy
to conveniently clean up all of those temporary objects once you are finished with your work.
Objectives:
- Deploy 1 EC2 Instances in your Default VPC.
- Bootstrap the EC2 instance with a script that will install and start Jenkins. Review the official Jenkins Documentation for more information: https://www.jenkins.io/doc/book/installing/linux/
- Create and assign a Security Group to the Jenkins Security Group that allows traffic on port 22 from your ip and allows traffic from port 8080.
- Create a S3 bucket for your Jenkins Artifacts that is not open to the public.
- Verify that you can reach your Jenkins install via port 8080 in your browser. Be sure to include a screenshot of the Jenkins login screen in your documentation.
First we will need to create a new directory using the command line (I’m creating one call Jenkins) once you create your directory cd into it.
mkdir <your directory name>
Now we will create a main.tf file to build out my terraform code (Code in the Terraform language is stored in plain text files with the . tf file extension. There is also a JSON-based variant of the language that is named with the . tf.)
We will need to add to our .tf file the AWS provider required providers information and region we are working in (this information can be found in the Terraform Registry)
Now we are going to add in the resource blocks to set up our EC2 instance, this will include the ami, instance type, security groups and the script to install Jenkins.
Next we will create a resource block to generate our private keys
Next we will create our secruity groups to allow traffic inbound connections to ports 8080, 443 and 22 and allow outbound to all ip and ports.
Now let create the S3 bucket for the Jenkins Artifacts that’s should not have public access.
Now lets run our commands
terraform init
Run command terraform fmt- this will make sure your main.tf is formatted correctly.
terraform fmt
Next command:
terraform validate
terraform plan
There is alot of information but this is what you are looking for when running the terraform plan.
Now we are going to apply
terraform apply
Once you run the apply you will receive this message, here you can type in “yes” to perform the actions described above.
All has been applied. When running the apply command, this will show you that all of your configuration was set up.
Now we can verify everything was configured in the AWS Console.
EC2 created
Security Groups created
S3 Bucket (jenkinsartiproject) was created with public block acesss.
Now we can grab our EC2 instance public IP address to test this out
Here we go a completed project!!!!
Now we can tear it down to avoid charges in AWS.
terraform destroy
https://github.com/SharonRJohnson/Sharon-Gold-LUIT/blob/main/Deploying%20Jeknkins%20with%20Terraform