```
pipeline {
agent any
options {
timeout(time: 1, unit: 'HOURS')
buildDiscarder(logRotator(numToKeepStr: '50', artifactNumToKeepStr: '50'))
}
triggers {
githubPush()
}
parameters {
string(
name: 'AWS_AMI',
defaultValue: 'ami-096f43ef67d75e998',
description: 'Enter the AWS AMI'
)
string(
name: 'APP_URL',
description: 'Enter the Mitte APP URL'
)
string(
name: 'AWS_Region',
defaultValue:"eu-west-1",
description: "Enter the AWS Region")
string(
name: 'AWS_KEY',
description: 'Enter the AWS EC2 Key'
)
string(
name: 'AWS_VPCID',
defaultValue: 'vpc-b603dfcf',
description: 'Enter the AWS VPCID'
)
string(
name: 'AWS_SUBNET_ID',
defaultValue: 'subnet-c3020ba5',
description: 'Enter the AWS EC2 Subnet'
)
string(
name: 'AWS_INS_TYPE',
defaultValue: 't2.micro',
description: 'Enter the AWS EC2 INSTANCE TYPE'
)
string(
name: 'AWS_RDS_SG',
defaultValue:"MITTE-WEBSHOP-RDS",
description: 'Enter the AWS SG RDS'
)
string(
name: 'AWS_APP_SG',
defaultValue:"MITTE-WEBSHOP-APP",
description: 'Enter the AWS SG APP'
)
string(
name: 'AWS_ALB_SG',
defaultValue:"MITTE-WEBSHOP-ALB",
description: 'Enter the AWS SG ALB'
)
string(
name: 'AWS_RDS_CLASS',
defaultValue: 'db.t2.micro',
description: 'Enter the AWS RDS TYPE'
)
string(
name: 'AWS_RDS_USER',
description: 'Enter the AWS RDS USER NAME'
)
string(
name: 'AWS_RDS_PASS',
description: 'Enter the AWS RDS PASS'
)
string(
name: 'AWS_RDS_SUBNET',
defaultValue:"test_run",
description: 'Enter the AWS RDS SUBNET GROUP NAME'
)
string(
name: 'AWS_RDS_SNAP',
defaultValue:"webshop-golden-snap",
description: 'Enter the AWS RDS SNAPSHOT NAME'
)
string(
name: 'AWS_ALB_NAME',
defaultValue:"webshop-mitte-alb",
description: 'Enter the AWS ALB NAME'
)
string(
name: 'AWS_ACM',
defaultValue:"arn:aws:acm:eu-west-1:171983042750:certificate/fe04d20b-bebb-4538-870e-1ab7475b676c",
description: 'Enter the AWS ACM ARN'
)
}
environment {
def PARAM_STAGE = "${params.PARAM_STAGE}"
def AWS_Region = "${params.AWS_Region}"
def AWS_BucketName = "${params.AWS_BucketName}"
}
stages {
stage('Terraform Initialization') {
steps {
catchError(buildResult: 'SUCCESS', stageResult: 'FAILURE') {
withAWS(credentials: 'aws-testrun', region: AWS_Region) {
sh '/usr/local/bin/terraform init'
sh '/usr/local/bin/terraform plan'
}
}
}
post {
success {
mail to: 'sranjantripathi@gmail.com',
subject: "Jenkins Build ${currentBuild.currentResult}: Job ${env.JOB_NAME}",
body: "${currentBuild.currentResult}: Job ${env.JOB_NAME} build ${env.BUILD_NUMBER}\n Completed Successfully"
}
failure {
mail to: 'sranjantripathi@gmail.com',
subject: "Failed Pipeline: ${currentBuild.currentResult}: Job ${env.JOB_NAME}",
body: "Something is wrong with ${env.BUILD_URL}\n Need more info, kindly check : ${env.BUILD_URL}"
}
}
}
stage('Terraform Approval') {
steps {
script {
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ])
}
}
}
stage('AWS Resource Deployment') {
steps {
withAWS(credentials: 'aws-testrun', region: AWS_Region) {
sh '/usr/local/bin/terraform apply --auto-approve -var="ami=${AWS_AMI}" -var="type=${AWS_INS_TYPE}" -var="key=${AWS_KEY}" -var="subid=${AWS_SUBNET_ID}" -var="vpcid=${AWS_VPCID}" -var="sgrds-name=${AWS_RDS_SG}" -var="sgalb-name=${AWS_ALB_SG}" -var="sgapp-name=${AWS_APP_SG}" -var="rdsclass=${AWS_RDS_CLASS}" -var="rdsuser=${AWS_RDS_USER}" -var="rdspass=${AWS_RDS_PASS}" -var="rdssub=${AWS_RDS_SUBNET}" -var="snap=${AWS_RDS_SNAP}" -var="albn=${AWS_ALB_NAME}" -var="acm=${AWS_ACM}" -target=aws_db_instance.db -target=aws_security_group.rdssg -target=aws_security_group.appsg -target=local_file.rdsendpoint'
sh '/bin/sh update.sh'
sh '/usr/local/bin/terraform apply --auto-approve -var="ami=${AWS_AMI}" -var="type=${AWS_INS_TYPE}" -var="key=${AWS_KEY}" -var="subid=${AWS_SUBNET_ID}" -var="vpcid=${AWS_VPCID}" -var="sgrds-name=${AWS_RDS_SG}" -var="sgalb-name=${AWS_ALB_SG}" -var="sgapp-name=${AWS_APP_SG}" -var="rdsclass=${AWS_RDS_CLASS}" -var="rdsuser=${AWS_RDS_USER}" -var="rdspass=${AWS_RDS_PASS}" -var="rdssub=${AWS_RDS_SUBNET}" -var="snap=${AWS_RDS_SNAP}" -var="albn=${AWS_ALB_NAME}" -var="acm=${AWS_ACM}"'
}
}
}
}
}
```