This AWS lambda function will terminate AWS EC2 instance daily when the last commit of specific github repository:branch is older than 3 days.
- AWS Lambda
- AWS Cloudwatch
- AWS IAM
- AWS CloudFormation
- Serverless Framework
npm -g install serverlessCheck out Serverless guide to learn more about Serverless Framework
The following tags are required when launching AWS EC2 Instance for ec2Terminator to check the most recent commit time of specific github repository:branch
serverType: <ServerType>
repoOwner: <GithubRepositoryOwnerName>
repoName: <GithubRepositoryName>
featureBranch: <BranchName>To change AWS region, modify region in serverless.yml
provider:
region: us-west-2To change serverType tag filter, modify SERVER_TYPE in serverless.yml
provider:
environment:
SERVER_TYPE: devIn order to deploy ec2Terminator, simply run:
npm install
serverless deployThe expected result should be similar to:
Serverless: Packaging service...
Serverless: Excluding development dependencies...
Serverless: Creating Stack...
Serverless: Checking Stack create progress...
.....
Serverless: Stack create finished...
Serverless: Uploading CloudFormation file to S3...
Serverless: Uploading artifacts...
Serverless: Uploading service .zip file to S3 (1.7 KB)...
Serverless: Validating template...
Serverless: Updating Stack...
Serverless: Checking Stack update progress...
.....................
Serverless: Stack update finished...
Service Information
service: sls-ec2Terminator
stage: dev
region: us-west-2
stack: sls-ec2Terminator-dev
api keys:
None
endpoints:
None
functions:
ec2Terminator: sls-ec2Terminator-dev-ec2TerminatorYou can now invoke the Lambda directly and even see the resulting log via
serverless invoke --function ec2Terminator --logFrequently invoking ec2Terminator might be failed if exceeding github API rate limit, check out Github API rate limit for the detail.