Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to deploy Jenkins on AWS using Terraform

2025-01-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly introduces "how to use Terraform to deploy Jenkins on AWS". In daily operation, I believe many people have doubts about how to use Terraform to deploy Jenkins on AWS. The editor consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubt of "how to use Terraform to deploy Jenkins on AWS". Next, please follow the editor to study!

precondition

EC2 instance in AWS Amazon to test this technical specification.

Install terraform as the "infrastructure is code" tool.

Install Jenkins and its work

First, we need to take a look at this section, because this is the process by which we can figure out how to install Jenkins through the script bash, which is made as a generic script and can be used on different CentOS Linux. This installation is divided into five parts, first we will install and start Jenkins, then we will register a default user and install the basic plug-in, and finally, register our best friend HelloWorld job, which will be a good example and a good start.

Jenkins_user=$1 jenkins_password=$2 jenkins_address= http://localhost:8080 set-x function installing () {# Installing some necessary dependencies sudo yum-y update sudo yum-y install wget java-1.8.0 nano nc # Installing jenkins Instructions located in http://pkg.jenkins-ci.org/redhat/ sudo wget-O / etc/yum.repos.d/jenkins.repo http://pkg.jenkins-ci.org/redhat/jenkins.repo sudo rpm-- import https://pkg.jenkins.io/redhat/jenkins.io.key sudo yum install-y jenkins sleep 1 echo "[INFO] Jenkins was installed"}

Note that the plugins function in the bash script is required for all plug-ins recommended when you install login Jenkins for the first time, as part of customizing this tool and for registering the default user. Loop to check if the Jenkins server is running, which you can watch when starting and plug-in functionality.

Function plugins () {# Installing jenkins plugins java-jar jenkins-cli.jar-s "$jenkins_address"-auth $jenkins_user:$jenkins_password install-plugin trilead-api java-jar jenkins-cli.jar-s "$jenkins_address"-auth $jenkins_user:$jenkins_password install-plugin cloudbees-folder. Many more plugins... Java-jar jenkins-cli.jar-s "$jenkins_address"-auth $jenkins_user:$jenkins_password install-plugin pam-auth java-jar jenkins-cli.jar-s "$jenkins_address"-auth $jenkins_user:$jenkins_password install-plugin ldap java-jar jenkins-cli.jar-s "$jenkins_address"-auth $jenkins_user:$jenkins_password install-plugin email-ext # Restart sudo systemctl restart jenkins & while ((1)) Do echo "[INFO] waiting for restart Jenkins on port [8080]..." Java-jar jenkins-cli.jar-s "$jenkins_address"-auth $jenkins_user:$jenkins_password list-jobs if (($? = = 0)); then break fi sleep 20 done echo "[INFO] Jenkins was restarted"

Do you remember the announcement of starting the Jenkins server? If you don't want to see it again, you should implement the plug-in functionality.

The following is a simple task that describes how to add a job to Jenkins through Jenkins CLI, which is a Pipeline job with string parameters.

False par_name HelloWorld false pipeline {agent {label 'master'} stages {stage (' build') {steps {echo "HelloWorld!"} true false

Deploy Jenkins as a server

General variables are provided in this file, so note some properties that need to be replaced by the AWS CLI variable that you configure. See the AWS CLI Command reference for more information.

Variable "region" {default = "us-east-1" description = "AWS region"} variable "access_key" {default = "HEREYOURACCESSKEY" description = "AWS credentials file path"} variable "secret_key" {default = "HEREYOURSECRETKEY" description = "AWS credentials file path"} variable "jenkins_user_name" {description = "jenkins" default = "jenkins"} variable "jenkins_user_password" {description = "jenkins" Default = "jenkins"} variable "jenkins_name" {description = "Jenkins name" default = "jenkins"} variable "jenkins_instance_type" {default = "t2.micro"} variable "jenkins_key_name" {default = "key-pair" description = "SSH key located in tyour AWS account."} variable "amis" {description = "ami to spawn." Default = {us-east-1 = "ami-0c94855ba95c71c99"}}

There are several properties that need to be set:

Access_key and secret_key: used as access keys to validate command requests.

Jenkins_key_name: the name of the key pair file, which will help you connect to our instance through ssh. If you need to create a key pair, please enter your AWS account through the handler in my security credentials section. Note that in this example, we use the key-pair.pem located in the Template folder of the project, replacing this file and its name in the project with your credentials.

Amis: this property can be a list and has an AMI instance ID that can be easily found in Amazon Machine Instances. If you want to change this AMI instance, please note whether AMI has installed systemctl, because Amazon Linux does not support systemclt or the service command is based on its CentOS / RHEL-based version, so to avoid spending a lot of time searching for AMI instances for which his systemctl has been configured, try using the AMI instance configured in this project.

Resource "aws_instance"jenkins" {instance_type = "${var.jenkins_instance_type}" security_groups = ["${aws_security_group.security_group_jenkins.name}"] ami = "${lookup (var.amis) Var.region)} "key_name =" ${var.jenkins_key_name} "# Add jenkins server startup provisioner" file "{connection {user =" ec2-user "host =" ${aws_instance.jenkins.public_ip} "timeout =" 1m "private_key =" ${file ("templates/$ {var.jenkins_key_name} .pem")} "} source =" templates/ " Jenkins_startup.sh "destination =" / home/ec2-user/jenkins_startup.sh "} # Add jenkins job provisioner" file "{connection {user =" ec2-user "host =" ${aws_instance.jenkins.public_ip} "timeout =" 1m "private_key =" ${file ("templates/$ {var.jenkins_key_name} .pem")} "source = "templates/jobmaster.xml" destination = "/ home/ec2-user/jobmaster.xml"} provisioner "remote-exec" {connection {user = "ec2-user" host = "${aws_instance.jenkins.public_ip}" timeout = "1m" private_key = "${file (" templates/$ {var.jenkins_key_name} .pem ")}" inline = [ "chmod + x / home/ec2-user/jenkins*.sh" "/ home/ec2-user/jenkins_startup.sh ${var.jenkins_user_name} ${var.jenkins_user_password}"]}}

Once all the properties are configured, execute the command line terraform init to initialize the working folder, execute the terraform plan to monitor all deployment plans, and once the check is complete, execute the command line terraform to apply for the deployment infrastructure.

Enter Jenkins

You can find it in the AWS management console, click the instances section, and then create a new instance as follows.

Copy the public DNS, type this value on the browser, and then type port 8080 (as shown in the mirror), keeping in mind that all available ports are in the security groups section of the main.tf file. The user and password are configured as attributes in the variables.tf file, so in this case, our user and password are "Jenkins".

At this point, the study on "how to use Terraform to deploy Jenkins on AWS" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report