Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

have jenkins start an EC2 instance and Terminating it

I have a full deployment job that takes an ip of a running instance and deploys my system on it.

I currently hold an EC2 instance for automation tests that run every night, but the instance is expensive and im looking for a way to initiate it before the tests and terminate it after the test.

I looked for EC2 plugins that can help and the closest one was this but this is for making slaves and thats not what I want.

I want to be able to launch an EC2 instance, and pass its IP address to the automation tests job, then terminate that instance once done.

I started making a command line bash file for this, but this seems like too much work, and I thought maybe there is something im missing.

like image 581
NotSoShabby Avatar asked Jan 22 '19 14:01

NotSoShabby


1 Answers

Your requirement is valid and amazon knows:

When you stop an instance, we shut it down. We don't charge usage for a stopped instance, or data transfer fees, but we do charge for the storage for any Amazon EBS volumes.

Reference :
- https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html

Here some approaches to start/stop your instaces

Amazon EC2 HTTP API

This is an api rest and you can perform a simple http request to start or stop your instance:

  • Amazon EC2 API Reference
  • Start Instance endpoint

https://ec2.amazonaws.com/?Action=StartInstances&...
  • Stop Instance endpoint

https://ec2.amazonaws.com/?Action=StopInstances&...

You can invoke this api from Jenkins in many ways : simple shell execution,groovy and scripted/declarative, pipelines.


AWS CLI

  • start instance
  • stop instance

Here more about how suspend instances using aws cli:

  • https://docs.aws.amazon.com/autoscaling/ec2/userguide/as-suspend-resume-processes.html
  • Also with powershell: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/Stop_Start.html

You can invoke this api from Jenkins in many ways : simple shell execution,groovy and scripted/declarative, pipelines.


AWS Instance Scheduler

In 2018, AWS launched the AWS Instance Scheduler, a new and improved scheduling solution that enables customers to schedule Amazon EC2 instances.

With this tool you can automatically start and stop the Amazon EC2 and Amazon RDS instances.

Reference :

  • https://aws.amazon.com/answers/infrastructure-management/instance-scheduler/

With this approach you don't need Jenkins :b

like image 172
JRichardsz Avatar answered Oct 31 '22 15:10

JRichardsz