Skip to content

Commit a1e76bf

Browse files
authored
Merge pull request #1 from rhythmictech/initial-project
initial commit
2 parents f61c5a9 + e63bad9 commit a1e76bf

File tree

7 files changed

+178
-0
lines changed

7 files changed

+178
-0
lines changed

.github/workflows/check.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: check
2+
on: [push, pull_request]
3+
4+
jobs:
5+
build:
6+
runs-on: macOS-latest
7+
steps:
8+
- uses: actions/checkout@v1
9+
10+
- name: Install prereq
11+
run: |
12+
brew install docker tfenv tflint
13+
tfenv install
14+
15+
- name: tf fmt
16+
run: |
17+
terraform fmt
18+
- name: tflint
19+
run: |
20+
tflint

.pre-commit-config.yaml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
---
2+
repos:
3+
- repo: git://github.com/antonbabenko/pre-commit-terraform
4+
rev: v1.19.0
5+
hooks:
6+
- id: terraform_fmt
7+
- id: terraform_docs
8+
- repo: https://github.com/pre-commit/pre-commit-hooks
9+
rev: v2.3.0
10+
hooks:
11+
- id: end-of-file-fixer
12+
- id: trailing-whitespace
13+
- id: no-commit-to-branch

.terraform-version

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0.12.13

README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# terraform-aws-inspector
2+
3+
4+
[![](https://github.com/rhythmictech/terraform-aws-inspector/workflows/check/badge.svg)](https://github.com/rhythmictech/terraform-aws-inspector/actions)
5+
6+
Configures AWS Inspector. Optionally configures a CloudWatch scheduled event to trigger assessments based on a specified schedule.
7+
8+
```
9+
module "inspector" {
10+
source = "git::ssh://git@github.com/rhythmictech/terraform-aws-inspector"
11+
match_tags = {
12+
"AWSInspector": "enabled"
13+
}
14+
}
15+
```
16+
17+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
18+
## Inputs
19+
20+
| Name | Description | Type | Default | Required |
21+
|------|-------------|:----:|:-----:|:-----:|
22+
| inspector\_cron\_schedule | Cron schedule to use \(see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html for formatting\) | string | `"cron(0 20 23 * ? *)"` | no |
23+
| match\_tags | Map of tags and corresponding values to match against for AWS Inspector | map(string) | n/a | yes |
24+
| name | Name of the assessment template/targets | string | `"Inspector"` | no |
25+
| schedule\_inspector | Indicate whether a cloudwatch rule should be created to trigger inspector automatically | bool | `"true"` | no |
26+
| tags | Tags to apply to resources that support tagging | map(string) | `{}` | no |
27+
28+
## Outputs
29+
30+
| Name | Description |
31+
|------|-------------|
32+
| inspector\_assessment\_target\_arn | |
33+
| inspector\_assessment\_template\_arn | |
34+
35+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->

main.tf

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
resource "aws_inspector_resource_group" "resource_group" {
2+
tags = var.match_tags
3+
}
4+
5+
resource "aws_inspector_assessment_target" "target" {
6+
name = var.name
7+
resource_group_arn = aws_inspector_resource_group.resource_group.arn
8+
}
9+
10+
resource "aws_inspector_assessment_template" "template" {
11+
name = var.name
12+
target_arn = aws_inspector_assessment_target.target.arn
13+
duration = 3600
14+
15+
# TODO don't hardcode this
16+
rules_package_arns = [
17+
"arn:aws:inspector:us-east-1:316112463485:rulespackage/0-gEjTy7T7",
18+
"arn:aws:inspector:us-east-1:316112463485:rulespackage/0-rExsr2X8",
19+
"arn:aws:inspector:us-east-1:316112463485:rulespackage/0-R01qwB5Q",
20+
"arn:aws:inspector:us-east-1:316112463485:rulespackage/0-gBONHN9h",
21+
]
22+
}
23+
24+
resource "aws_cloudwatch_event_rule" "inspector_trigger" {
25+
count = var.schedule_inspector ? 1 : 0
26+
name = "${var.name}-Scheduler"
27+
description = "Schedules AWS Inspector runs"
28+
schedule_expression = var.inspector_cron_schedule
29+
tags = var.tags
30+
}
31+
32+
data "aws_iam_policy_document" "cw_inspector_assume_role" {
33+
statement {
34+
actions = ["sts:AssumeRole"]
35+
36+
principals {
37+
type = "Service"
38+
identifiers = ["events.amazonaws.com"]
39+
}
40+
}
41+
}
42+
43+
resource "aws_iam_role" "cw_inspector_iam_role" {
44+
count = var.schedule_inspector ? 1 : 0
45+
assume_role_policy = data.aws_iam_policy_document.cw_inspector_assume_role.json
46+
name_prefix = "${var.name}-cw-role-"
47+
tags = var.tags
48+
49+
lifecycle {
50+
create_before_destroy = true
51+
}
52+
}
53+
54+
data "aws_iam_policy_document" "cw_inspector_policy_doc" {
55+
statement {
56+
actions = ["inspector:StartAssessmentRun"]
57+
resources = ["*"]
58+
}
59+
}
60+
61+
resource "aws_iam_role_policy" "cw_inspector_policy" {
62+
count = var.schedule_inspector ? 1 : 0
63+
name_prefix = "${var.name}-cwinspector-"
64+
role = aws_iam_role.cw_inspector_iam_role[0].id
65+
policy = data.aws_iam_policy_document.cw_inspector_policy_doc.json
66+
}
67+
68+
resource "aws_cloudwatch_event_target" "inspector_target" {
69+
count = var.schedule_inspector ? 1 : 0
70+
arn = aws_inspector_assessment_template.template.arn
71+
role_arn = aws_iam_role.cw_inspector_iam_role[0].arn
72+
rule = aws_cloudwatch_event_rule.inspector_trigger[0].name
73+
target_id = "${var.name}-Scheduler"
74+
}

outputs.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
output "inspector_assessment_target_arn" {
2+
value = aws_inspector_assessment_target.target.arn
3+
}
4+
5+
output "inspector_assessment_template_arn" {
6+
value = aws_inspector_assessment_template.template.arn
7+
}

variables.tf

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
variable "name" {
2+
default = "Inspector"
3+
description = "Name of the assessment template/targets"
4+
type = string
5+
}
6+
7+
variable "match_tags" {
8+
description = "Map of tags and corresponding values to match against for AWS Inspector"
9+
type = map(string)
10+
}
11+
12+
variable "schedule_inspector" {
13+
default = true
14+
description = "Indicate whether a cloudwatch rule should be created to trigger inspector automatically"
15+
type = bool
16+
}
17+
18+
variable "inspector_cron_schedule" {
19+
default = "cron(0 20 23 * ? *)"
20+
description = "Cron schedule to use (see https://docs.aws.amazon.com/AmazonCloudWatch/latest/events/ScheduledEvents.html for formatting)"
21+
type = string
22+
}
23+
24+
variable "tags" {
25+
default = {}
26+
description = "Tags to apply to resources that support tagging"
27+
type = map(string)
28+
}

0 commit comments

Comments
 (0)