Commit 8565b064 authored by A-Gordon's avatar A-Gordon

created readme

parent 037ca895
tf_mod_aws_rds
==============
A Terraform module for creating a AWS Lambda function that will search for and terminate any stale packer instances.
An instance will be marked as stale when it has existed for over 2 hours.
The module will create a IAM policy allowing the lambda function to describe EC2, terminate EC2 and full access to cloudwatch logs. It will also create a cloudwatch event and schedule that will trigger the function, the rate for this defualts to every 2 hours and is customisable.
This module makes the following assumptions:
* You have already defined the provider already
* You have already configured packer and are tagging the instances with the tag 'Packer Builder'. To change the tag you will need to edit packer_janitor.py and create a new zip file.
* You are within the eu-west-1 region, to change this you will need to edit the python script and generate a new zip file.
Input Variables
---------------
- `cloudwatch_schedule` - The ID of the VPC.
Usage
-----
You can use this in your terraform template with the following steps.
1.) Adding a module resource to your template, e.g. `main.tf`
```
module "packer_janitor" {
source = "git::https://git.steamhaus.co.uk/steamhaus/tf_mod_packer_janitor"
}
```
The below example uses ssh as the source and will set the schedule to trigger every 4 hours
```
module "packer_janitor" {
source = "git::ssh://git@git.steamhaus.co.uk/steamhaus/tf_mod_packer_janitor"
cloudwatch_schedule = rate(4 hours)
}
```
\ No newline at end of file
variable "cloudwatch_schedule" {
description = "Schedule for cloudwatch event to invoke Lambda function"
default = "rate(2 hours)"
}
resource "aws_iam_role" "packer_janitor" {
name = "iam_packer_janitor"
assume_role_policy = "${file("${path.module}/assume-role.json")}"
......@@ -23,11 +28,6 @@ resource "aws_lambda_function" "packer_janitor" {
source_code_hash = "${base64sha256(file("${path.module}/packer_janitor.zip"))}"
}
variable "cloudwatch_schedule" {
description = "Schedule for cloudwatch event to invoke Lambda function"
default = "rate(2 hours)"
}
resource "aws_cloudwatch_event_rule" "packer_janitor" {
name = "packer_janitor"
description = "Run packer janitor to tidy up stale packer instances."
......
......@@ -57,7 +57,7 @@ def lambda_handler (event, context):
logger.info('[%s]: Instance has been running for %s days and %s mins. Marking for termination', instance.id, runtime.days, runtime_minutes)
instance.terminate(DryRun=DRYRUN)
logger.info('[%s]: Stopped Instance', instance.id)
logger.info('[%s]: Terminated Instance', instance.id)
return True
def time_diff(launch_time, current_time):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment