├── Jenkinsfile ├── main.tf ├── output.tf ├── provider.tf └── variables.tf /Jenkinsfile: -------------------------------------------------------------------------------- 1 | pipeline { 2 | agent any 3 | 4 | parameters { 5 | booleanParam(name: 'autoApprove', defaultValue: false, description: 'Automatically run apply after generating plan?') 6 | choice(name: 'action', choices: ['apply', 'destroy'], description: 'Select the action to perform') 7 | } 8 | 9 | environment { 10 | AWS_ACCESS_KEY_ID = credentials('aws-access-key-id') 11 | AWS_SECRET_ACCESS_KEY = credentials('aws-secret-access-key') 12 | AWS_DEFAULT_REGION = 'ap-south-1' 13 | } 14 | 15 | stages { 16 | stage('Checkout') { 17 | steps { 18 | git branch: 'main', url: 'https://github.com/CodeSagarOfficial/jenkins-scripts.git' 19 | } 20 | } 21 | stage('Terraform init') { 22 | steps { 23 | sh 'terraform init' 24 | } 25 | } 26 | stage('Plan') { 27 | steps { 28 | sh 'terraform plan -out tfplan' 29 | sh 'terraform show -no-color tfplan > tfplan.txt' 30 | } 31 | } 32 | stage('Apply / Destroy') { 33 | steps { 34 | script { 35 | if (params.action == 'apply') { 36 | if (!params.autoApprove) { 37 | def plan = readFile 'tfplan.txt' 38 | input message: "Do you want to apply the plan?", 39 | parameters: [text(name: 'Plan', description: 'Please review the plan', defaultValue: plan)] 40 | } 41 | 42 | sh 'terraform ${action} -input=false tfplan' 43 | } else if (params.action == 'destroy') { 44 | sh 'terraform ${action} --auto-approve' 45 | } else { 46 | error "Invalid action selected. Please choose either 'apply' or 'destroy'." 47 | } 48 | } 49 | } 50 | } 51 | 52 | } 53 | } -------------------------------------------------------------------------------- /main.tf: -------------------------------------------------------------------------------- 1 | resource "aws_instance" "public_instance" { 2 | ami = var.ami 3 | instance_type = var.instance_type 4 | 5 | tags = { 6 | Name = var.name_tag, 7 | } 8 | } -------------------------------------------------------------------------------- /output.tf: -------------------------------------------------------------------------------- 1 | output "public_ip" { 2 | value = aws_instance.public_instance.public_ip 3 | description = "Public IP Address of EC2 instance" 4 | } 5 | 6 | output "instance_id" { 7 | value = aws_instance.public_instance.id 8 | description = "Instance ID" 9 | } -------------------------------------------------------------------------------- /provider.tf: -------------------------------------------------------------------------------- 1 | terraform { 2 | required_providers { 3 | aws = { 4 | source = "hashicorp/aws" 5 | version = "~> 4.0" 6 | } 7 | } 8 | } 9 | 10 | provider "aws" { 11 | access_key = var.aws_access_key 12 | secret_key = var.aws_secret_key 13 | region = var.aws_region 14 | } -------------------------------------------------------------------------------- /variables.tf: -------------------------------------------------------------------------------- 1 | variable "aws_access_key" { 2 | description = "AWS access key" 3 | type = string 4 | default = "" 5 | } 6 | 7 | variable "aws_secret_key" { 8 | description = "AWS secret key" 9 | type = string 10 | default = "" 11 | } 12 | 13 | variable "aws_region" { 14 | description = "AWS region" 15 | type = string 16 | default = "ap-south-1" 17 | } 18 | 19 | 20 | variable "ami" { 21 | type = string 22 | description = "Ubuntu AMI ID" 23 | default = "ami-0f5ee92e2d63afc18" 24 | } 25 | 26 | variable "instance_type" { 27 | type = string 28 | description = "Instance type" 29 | default = "t2.micro" 30 | } 31 | 32 | variable "name_tag" { 33 | type = string 34 | description = "Name of the EC2 instance" 35 | default = "My EC2 Instance" 36 | } 37 | --------------------------------------------------------------------------------