Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Devops1 #33

Open
wants to merge 33 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
05bf684
added jenkins file
easyawslearn Jul 23, 2020
eb73de1
updated config
easyawslearn Jul 23, 2020
5b63fc0
cosmetic chnages
easyawslearn Jul 23, 2020
649b829
cosmetic chnages
easyawslearn Jul 23, 2020
fe40aa4
test
easyawslearn Jul 24, 2020
b7b3289
updated configuration
easyawslearn Jul 24, 2020
89d340e
updated configuration
easyawslearn Jul 24, 2020
832e9a8
updated configuration
easyawslearn Jul 24, 2020
4be5cb3
updated configuration
easyawslearn Jul 24, 2020
ceb390e
new project added
easyawslearn Jul 24, 2020
4b7c010
new project added
easyawslearn Jul 24, 2020
469c6d3
new project added
easyawslearn Jul 24, 2020
f808f54
new project added
easyawslearn Jul 24, 2020
5a343be
added region variable
easyawslearn Jul 24, 2020
ef8d423
added region variable
easyawslearn Jul 24, 2020
fe0965f
added region variable
easyawslearn Jul 24, 2020
a32a37a
added region variable
easyawslearn Jul 24, 2020
8ef55ca
added region variable
easyawslearn Jul 24, 2020
7289640
added region variable
easyawslearn Jul 24, 2020
8727318
added region variable
easyawslearn Jul 24, 2020
20f71c4
added region variable
easyawslearn Jul 24, 2020
8cf0040
added region variable
easyawslearn Jul 24, 2020
950870f
removed tf plan as its not require
easyawslearn Jul 24, 2020
6a0eb41
added destroy
easyawslearn Jul 24, 2020
8fe8980
added destroy
easyawslearn Jul 24, 2020
93b6c01
added destroy
easyawslearn Jul 24, 2020
3f27043
added destroy
easyawslearn Jul 24, 2020
b4c63ef
added destroy
easyawslearn Jul 24, 2020
30b70c1
added destroy
easyawslearn Jul 24, 2020
60c220e
Update user-data-file-input.tf
easyawslearn Jul 24, 2020
8305750
Update Jenkinsfile
Kerem-Tokgoz Mar 15, 2021
8c52d70
Update Jenkinsfile
Kerem-Tokgoz Mar 15, 2021
32bd00d
Update Jenkinsfile
Kerem-Tokgoz Mar 15, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 80 additions & 0 deletions Devops-project1/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

pipeline {

parameters {
string(name: 'environment', defaultValue: 'terraform', description: 'Workspace/environment file to use for deployment')
string(name: 'region', defaultValue: 'us-east-1', description: 'select region to deployment')
string(name: 'env', defaultValue: 'prod', description: 'select environment to deployment')
string(name: 'service', defaultValue: 'apache', description: 'please provide service name')
booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?')

}


environment {
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
}

agent any
options {
timestamps ()
ansiColor('xterm')
}
stages {
stage('checkout') {
steps {
git branch: "devops1", url: "https://github.com/Kerem-Tokgoz/Terraform-Tutorial.git"
}
}

stage('Plan') {
steps {
sh '''
cd Devops-project1 ;
TF_LOG=DEBUG terraform init \
-upgrade=true \
-get=true \
-input=true \
-force-copy \
-backend=true \
-backend-config "bucket=aws-terraform-devops-backend-kerem" \
-backend-config "key=terraform-${region}/${service}.tfstate" \
-backend-config "region=${region}" \
-backend-config "dynamodb_table=terraform" \
-lock=true
'''
sh """#!/bin/bash
cd Devops-project1 ; terraform workspace show | grep ${environment} ; if [ "\$?" == 0 ];then echo "workspace already exists ";else terraform workspace new ${environment}; fi;

echo "INFO: Terraform -> Working for ${environment}";
terraform plan -var region=${region} -out tfplan -lock=true;
terraform show -no-color tfplan > tfplan.txt;
"""
}
}
stage('Approval') {
when {
not {
equals expected: true, actual: params.autoApprove
}
}

steps {
script {
def plan = readFile 'Devops-project1/tfplan.txt'
input message: "Do you want to apply the plan?",
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
}
}
}

stage('Apply') {
steps {
sh "cd Devops-project1 ; terraform apply -input=false tfplan "
}
}

}

}
6 changes: 6 additions & 0 deletions Devops-project1/apache_config.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/bash
sudo yum update -y
sudo yum install -y httpd.x86_64
sudo service httpd start
sudo service httpd enable
echo "<h1>Welcome to apche server</h1>" | sudo tee /var/www/html/index.html
3 changes: 3 additions & 0 deletions Devops-project1/output.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
output "user_data_example_input_file" {
value = "${aws_instance.user_data_example_input_file.public_ip}"
}
18 changes: 18 additions & 0 deletions Devops-project1/provider.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
provider "aws" {
region = var.region
version = "~> 2.0"
}

# data "terraform_remote_state" "network" {
# backend = "s3"
# config = {
# bucket = "aws-terraform-devops-backend"
# key = "network/terraform.tfstate"
# region = "us-east-1"
# }
# }

terraform {
backend "s3" {
}
}
28 changes: 28 additions & 0 deletions Devops-project1/security_group.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
resource "aws_security_group" "allow_ssh" {
name = "apche-sg"
description = "Allow SSH inbound traffic"
#vpc_id = aws_vpc.vpc_demo.id

ingress {
# SSH Port 22 allowed from any IP
from_port = 22
to_port = 22
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

ingress {
# SSH Port 80 allowed from any IP
from_port = 80
to_port = 80
protocol = "tcp"
cidr_blocks = ["0.0.0.0/0"]
}

egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = ["0.0.0.0/0"]
}
}
17 changes: 17 additions & 0 deletions Devops-project1/user-data-file-input.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

resource "aws_instance" "user_data_example_input_file" {
ami = lookup(var.ami_id, var.region)
instance_type = var.instance_type
# subnet_id = aws_subnet.public_1.id

# Security group assign to instance
vpc_security_group_ids = [aws_security_group.allow_ssh.id]

# key name
key_name = var.key_name
user_data = "${file("apache_config.sh")}"

tags = {
Name = "Apache-Server1"
}
}
20 changes: 20 additions & 0 deletions Devops-project1/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
variable "region" {
type = "string"
default = "us-east-2"
}
variable "ami_id" {
type = "map"
default = {
us-east-1 = "ami-035b3c7efe6d061d5"
eu-west-2 = "ami-132b3c7efe6sdfdsfd"
eu-central-1 = "ami-9787h5h6nsn75gd33"
}
}
variable "instance_type" {
type = "string"
default = "t2.micro"
}
variable "key_name" {
type = "string"
default = "ec2-demo"
}
6 changes: 6 additions & 0 deletions EC2withJenkins/ec2_jenkins.tf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,12 @@ resource "aws_instance" "ec2_jenkins" {
curl --silent --location http://pkg.jenkins-ci.org/redhat-stable/jenkins.repo | sudo tee /etc/yum.repos.d/jenkins.repo
sudo rpm --import https://jenkins-ci.org/redhat/jenkins-ci.org.key
yum install -y jenkins
yum install -y git

wget https://releases.hashicorp.com/terraform/0.12.29/terraform_0.12.29_linux_amd64.zip
unzip terraform_0.12.29_linux_amd64.zip
mv terraform /usr/bin

systemctl start jenkins
systemctl status jenkins
systemctl enable jenkins
Expand Down
77 changes: 77 additions & 0 deletions terraform-aws-ec2-userdata/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@

pipeline {

parameters {
string(name: 'environment', defaultValue: 'terraform', description: 'Workspace/environment file to use for deployment')
string(name: 'service', defaultValue: 'apache', description: 'please provide service name')
booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?')

}


environment {
AWS_ACCESS_KEY_ID = credentials('AWS_ACCESS_KEY_ID')
AWS_SECRET_ACCESS_KEY = credentials('AWS_SECRET_ACCESS_KEY')
}

agent any
options {
timestamps ()
ansiColor('xterm')
}
stages {
stage('checkout') {
steps {
git branch: "devops1", url: "https://github.com/easyawslearn/Terraform-Tutorial.git"
}
}

stage('Plan') {
steps {
sh '''
cd terraform-aws-ec2-userdata ;
terraform init \
-upgrade=true \
-get=true \
-input=false \
-force-copy \
-backend=true \
-backend-config "bucket=aws-terraform-devops-backend" \
-backend-config "key=terraform/${service}.tfstate" \
-backend-config "region=us-east-1" \
-backend-config "dynamodb_table=terraform" \
-lock=true
'''
sh """#!/bin/bash
cd terraform-aws-ec2-userdata ; terraform workspace show | grep ${environment} ; if [ "\$?" == 0 ];then echo "workspace already exists ";else terraform workspace new ${environment}; fi;

cd terraform-aws-ec2-userdata ; echo "INFO: Terraform -> Working for ${environment}";
terraform plan -input=false -out tfplan -lock=true;
terraform show -no-color tfplan > tfplan.txt;
"""
}
}
stage('Approval') {
when {
not {
equals expected: true, actual: params.autoApprove
}
}

steps {
script {
def plan = readFile 'terraform-aws-ec2-userdata/tfplan.txt'
input message: "Do you want to apply the plan?",
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
}
}
}

stage('Apply') {
steps {
sh "cd terraform-aws-ec2-userdata ; terraform apply -input=false tfplan"
}
}
}

}
16 changes: 15 additions & 1 deletion terraform-aws-ec2-userdata/provider.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,18 @@
provider "aws" {
region = "${var.region}"
region = var.region
version = "~> 2.0"
}

# data "terraform_remote_state" "network" {
# backend = "s3"
# config = {
# bucket = "aws-terraform-devops-backend"
# key = "network/terraform.tfstate"
# region = "us-east-1"
# }
# }

terraform {
backend "s3" {
}
}
2 changes: 1 addition & 1 deletion terraform-aws-ec2-userdata/security_group.tf
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
resource "aws_security_group" "allow_ssh" {
name = "allow_SSH"
name = "apche-sg"
description = "Allow SSH inbound traffic"
#vpc_id = aws_vpc.vpc_demo.id

Expand Down