diff --git a/Jenkinsfile b/Jenkinsfile index f69a2db..b64e943 100644 --- a/Jenkinsfile +++ b/Jenkinsfile @@ -1,5 +1,9 @@ pipeline { - agent any + agent { label "vinod" } + + triggers { + githubpush() + } parameters { booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?') @@ -7,47 +11,83 @@ pipeline { } 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'." } } } } - } -} \ No newline at end of file +} diff --git a/igw.tf b/igw.tf new file mode 100644 index 0000000..906954a --- /dev/null +++ b/igw.tf @@ -0,0 +1,9 @@ +# Internet Gateway + +resource "aws_internet_gateway" "main" { + vpc_id = aws_vpc.main.id + + tags = { + Name = "main-internet-gateway" + } +} \ No newline at end of file diff --git a/output.tf b/output.tf index 5d746db..15de60a 100644 --- a/output.tf +++ b/output.tf @@ -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" } \ No newline at end of file diff --git a/subnet.tf b/subnet.tf new file mode 100644 index 0000000..b91fddf --- /dev/null +++ b/subnet.tf @@ -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" + } + +} + + diff --git a/variables.tf b/variables.tf index da7ee07..7e872b7 100644 --- a/variables.tf +++ b/variables.tf @@ -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" { diff --git a/vpc.tf b/vpc.tf new file mode 100644 index 0000000..c7afa11 --- /dev/null +++ b/vpc.tf @@ -0,0 +1,7 @@ +resource "aws_vpc" "main" { + cidr_block = "10.0.0.0/16" + + tags = { + name = "main" + } +} \ No newline at end of file