This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stage('Checkout') { | |
steps { | |
checkout scm | |
sh ' cd devops/terraform/modules/ec2/' | |
sh """ sed -i '/instance_type=.*/c instance_type='\""${instance_type}"\"'' terraform.tfvars | |
sed -i '/key_pair=.*/c key_pair='\""${key_pair}"\"'' terraform.tfvars | |
sed -i '/ami_name=.*/c ami_name='\""${ami_name}"\"'' terraform.tfvars | |
sed -i '/subnet=.*/c subnet='\""${subnet}"\"'' terraform.tfvars | |
sed -i '/root_vol_size=.*/c root_vol_size='\""${root_vol_size}"\"'' terraform.tfvars | |
sed -i '/root_vol_delete_on_termination=.*/c root_vol_delete_on_termination='\""${root_vol_delete_on_termination}"\"'' terraform.tfvars |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
curl -X POST \ | |
'https://api.spotinst.io/aws/ec2/statefulMigrationGroup?accountId={act-1234xyz}’ \ | |
-H 'Authorization: Bearer <authorization_token>’ \ | |
-H 'Content-Type: application/json' \ | |
-H 'Postman-Token: <postman_token>' \ | |
-H 'cache-control: no-cache' \ | |
-d '{ | |
"statefulMigrationGroup": { | |
"shouldKeepPrivateIp": true, | |
"originalInstanceId": "'"$instance_id"'", |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stage('Apply') { | |
steps { | |
sh ‘terraform apply -auto-approve tfplan.txt ' | |
sh ‘echo "`terraform output`" | mail -s "${Env^} EC2 created by ${BUILD_USER} for ${Team} Team in ${region} region" -r email@company-domain.com email@company-domain.com’ | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stage('Plan') { | |
steps { | |
sh 'terraform init [ -f terraform.tfstate ] && rm terraform.tfstate ' | |
sh 'terraform plan –out=tfplan.txt ' | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
stage('Approval') { | |
steps { | |
script { | |
def userInput = input(id: 'confirm', message: 'Apply Terraform?', parameters: [ [$class: 'BooleanParameterDefinition', defaultValue: false, description: 'Apply terraform', name: 'confirm'] ]) | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
resource "aws_instance" "ec2" { | |
ami = "${data.aws_ami.ami.id}" | |
instance_type = "${var.instance_type}" | |
key_name = "${var.key_pair}" | |
vpc_security_group_ids = ["${aws_security_group.ec2-sg.id}"] | |
subnet_id = "${element(random_shuffle.subnet.result,0)}" | |
user_data = "${data.template_file.user_data.rendered}" | |
iam_instance_profile = "${aws_iam_instance_profile.ec2-profile.id}" | |
user_data = "${data.template_file.user_data.rendered}" | |
associate_public_ip_address = "${var.associate_public_ip_address}" |