AppGambit
Published in

AppGambit

Provisioning a Jenkins Server on AWS With Terraform

In this article, we are talking about How we can deploy the Jenkins server on AWS EC2 instance using Terraform script.

Prerequisites

  • We require AWS IAM API keys (access key and secret key) with EC2 permissions.
  • Terraform should be installed on the machine. If Terraform does not exist you can download and install it from here.

Resources Created Using Terraform

  • AWS VPC
  • Public Subnet
  • EC2 Key pair
  • EC2 Instance
  • Security Group
  • Elastic IP

Let’s Start!

1. Create a “provider.tf”

This is the provider file that tells Terraform, which provider you are using.

All infrastructure will be on the AWS because of provider “aws”. If you want to use another cloud provider such as GCP or Azure, you need to change this.

We already declared the region of AWS where we are creating a VPC network.

you can declare a profile also if you are working on multiple AWS accounts.

by default, it will take your default profile.

2. Create “variables.tf”

All variables will be in this file. Now, there is only one region but there will be more…

If you are using terraform.tfvars you just need to add a description only.

3. Create “terraform.tfvars

To persist variable values, create a file, and assign variables within this file. Create a file named terraform.tfvars with the following contents:

For all files which match terraform.tfvars or *.auto.tfvars present in the current directory, Terraform automatically loads them to populate variables. If the file is named something else, you can use the -var-file flag directly to specify a file.

Personally, I don’t recommend saving usernames and passwords to version control, but you can create a local secret variables file and use -var-file to load it.

4. Create “modules > VPC” Folder

A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.

5. Create “Main.tf” in the VPC folder.

Note: Here we are using basic VPC setup if you want to explore Full VPC setup with public private subnets with NAT please follow

6. Create “variables.tf” in the VPC folder.

This is the same as the above variable.tf file just declare all variables that we are using in main.tf a file so we can use get all variables value from main main.tf file.

7. Create “output.tf” in the VPC folder.

We can export any details from created resources and give that as an input of another module.

We can access output value in another submodule like

8. Create “modules > compute” Folder

A module is a container for multiple resources that are used together. Modules can be used to create lightweight abstractions, so that you can describe your infrastructure in terms of its architecture, rather than directly in terms of physical objects.

9. Create “Main.tf” in the compute folder.

10. Create “variables.tf” in the compute folder.

This is the same as the above variable.tf file just declare all variables that we are using in main.tf a file so we can use get all variables value from main main.tf file.

11. Create “output.tf” in the compute folder.

We can export any details from created resources and give that as an input of another module.

11. Create “install_jenkins.sh” in the compute folder.

Jenkins installation script that deploy Jenkins on our EC2 instance.

12. Create “main.tf”

main.tf files in your working directory when you run terraform plan or terraform apply together form the root module. That module may call other modules and connect them by passing output values from one to the input values of another. To learn how to use modules, see the Modules configuration section.

12. Create Key pair for EC2 key

Following command will generates Key pair files and move that to our compute module.

Now, We are ready to init!

Run `terraform init` that download all modules information and download terraform in your project file.

After that, you can see the .terraform folder in your project directory that contains terraform setup and modules information.

terraform plan

The terraform plan a command is used to create an execution plan. Terraform performs a refresh, unless explicitly disabled, and then determines what actions are necessary to achieve the desired state specified in the configuration files.

This command is a convenient way to check whether the execution plan for a set of changes matches your expectations without making any changes to real resources or the state. For example, terraform plan might be run before committing a change to version control, to create confidence that it will behave as expected.

terraform apply

The terraform apply a command is used to apply the changes required to reach the desired state of the configuration, or the pre-determined set of actions generated by a terraform plan execution plan.

🎊 🎉🤖🎊 🎉 Our VPC Setup is ready on AWS.

Just you need to follow above all steps or clone this repository to start terraforming.

After cloning the repo, just run the following commands.

generate Key pair with following command

ssh-keygen -t rsa -b 4096 -m pem -f aws_kp && mv aws_kp.pub modules/compute/aws_kp.pub && mv aws_kp aws_kp.pem && chmod 400 aws_kp.pem

change values in terraform.tfvars.

terraform init

terraform plan

terraform apply

Once it will done you can access Jenkins with EC2 Instance public IP address

Please follow this to configure Jenkins

Thank you for reading, if you have anything to add please send a response or add a note!

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Prashant Bhatasana

AWS Community Builder | Terraform Associate | Infra. Automation Engineer, Love to work with #AWS #Terraform #Jenkins #kubernetes #Docker #Ansible #Selenium