Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
82 changes: 61 additions & 21 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,53 +1,93 @@
pipeline {
agent any
agent { label "vinod" }

triggers {
githubpush()
}

parameters {
booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?')
choice(name: 'action', choices: ['apply', 'destroy'], description: 'Select the action to perform')
}

environment {
AWS_ACCESS_KEY_ID = credentials('aws-access-key-id')
AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key')
AWS_DEFAULT_REGION = 'ap-south-1'
AWS_DEFAULT_REGION = 'us-east-1'
}

stages {
stage('Checkout') {
steps {
git branch: 'main', url: 'https://github.com/CodeSagarOfficial/jenkins-scripts.git'
git branch: 'main', url: 'https://github.com/ajaysingh3200/terraform-jenkins-pipeline.git', credentialsId: 'github-token'
}
}
stage('Terraform init') {
steps {
sh 'terraform init'
withCredentials([usernamePassword(
credentialsId: 'aws-credentials',
usernameVariable: 'AWS_ACCESS_KEY_ID',
passwordVariable: 'AWS_SECRET_ACCESS_KEY'
)]) {
sh 'terraform init'
}
}
}
stage('Terraform fmt') {
steps {
withCredentials([usernamePassword(
credentialsId: 'aws-credentials',
usernameVariable: 'AWS_ACCESS_KEY_ID',
passwordVariable: 'AWS_SECRET_ACCESS_KEY'
)]) {
sh 'terraform fmt'
}
}
}
stage('Terraform validate') {
steps {
withCredentials([usernamePassword(
credentialsId: 'aws-credentials',
usernameVariable: 'AWS_ACCESS_KEY_ID',
passwordVariable: 'AWS_SECRET_ACCESS_KEY'
)]) {
sh 'terraform validate'
}
}
}
stage('Plan') {
steps {
sh 'terraform plan -out tfplan'
sh 'terraform show -no-color tfplan > tfplan.txt'
withCredentials([usernamePassword(
credentialsId: 'aws-credentials',
usernameVariable: 'AWS_ACCESS_KEY_ID',
passwordVariable: 'AWS_SECRET_ACCESS_KEY'
)]) {
sh 'terraform plan -out tfplan'
sh 'terraform show -no-color tfplan > tfplan.txt'
}
}
}
stage('Apply / Destroy') {
steps {
script {
if (params.action == 'apply') {
if (!params.autoApprove) {
def plan = readFile 'tfplan.txt'
input message: "Do you want to apply the plan?",
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
withCredentials([usernamePassword(
credentialsId: 'aws-credentials',
usernameVariable: 'AWS_ACCESS_KEY_ID',
passwordVariable: 'AWS_SECRET_ACCESS_KEY'
)]) {
if (params.action == 'apply') {
if (!params.autoApprove) {
def plan = readFile 'tfplan.txt'
input message: "Do you want to apply the plan?",
parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)]
}
sh 'terraform apply -input=false tfplan'
} else if (params.action == 'destroy') {
sh 'terraform destroy -auto-approve'
} else {
error "Invalid action selected. Please choose either 'apply' or 'destroy'."
}

sh 'terraform ${action} -input=false tfplan'
} else if (params.action == 'destroy') {
sh 'terraform ${action} --auto-approve'
} else {
error "Invalid action selected. Please choose either 'apply' or 'destroy'."
}
}
}
}

}
}
}
9 changes: 9 additions & 0 deletions igw.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# Internet Gateway

resource "aws_internet_gateway" "main" {
vpc_id = aws_vpc.main.id

tags = {
Name = "main-internet-gateway"
}
}
24 changes: 24 additions & 0 deletions output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,28 @@ output "public_ip" {
output "instance_id" {
value = aws_instance.public_instance.id
description = "Instance ID"
}

output "public_subnet_1_id" {
value = aws_subnet.public-subnet-1.id
description = "ID of public subnet 1"
}

output "public_subnet_2_id" {
value = aws_subnet.public-subnet-2.id
description = "ID of public subnet 2"
}

output "public_subnet_3_id" {
value = aws_subnet.public-subnet-3.id
description = "ID of public subnet 3"
}

output "public_subnet_cidrs" {
value = [
aws_subnet.public-subnet-1.cidr_block,
aws_subnet.public-subnet-2.cidr_block,
aws_subnet.public-subnet-3.cidr_block
]
description = "CIDR blocks of all public subnets"
}
37 changes: 37 additions & 0 deletions subnet.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
resource "aws_subnet" "public-subnet-1" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.1.0/24"
availability_zone = "us-east-1a"
map_public_ip_on_launch = true

tags = {
Name = "public-subnet-1"
}

}

resource "aws_subnet" "public-subnet-2" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.2.0/24"
availability_zone = "us-east-1b"
map_public_ip_on_launch = true

tags = {
Name = "public-subnet-2"
}

}

resource "aws_subnet" "public-subnet-3" {
vpc_id = aws_vpc.main.id
cidr_block = "10.0.3.0/24"
availability_zone = "us-east-1c"
map_public_ip_on_launch = true

tags = {
Name = "public-subnet-2"
}

}


4 changes: 2 additions & 2 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ variable "aws_secret_key" {
variable "aws_region" {
description = "AWS region"
type = string
default = "ap-south-1"
default = "us-east-1"
}


variable "ami" {
type = string
description = "Ubuntu AMI ID"
default = "ami-0f5ee92e2d63afc18"
default = "ami-0866a3c8686eaeeba"
}

variable "instance_type" {
Expand Down
7 changes: 7 additions & 0 deletions vpc.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
resource "aws_vpc" "main" {
cidr_block = "10.0.0.0/16"

tags = {
name = "main"
}
}