Skip to content

Commit 0b54755

Browse files
authored
feat: first pass (#2)
* feat: first pass * chore: add pre-commit fixes, address checkov * chore: cleanup more pre-commit
1 parent 34caef6 commit 0b54755

File tree

8 files changed

+195
-26
lines changed

8 files changed

+195
-26
lines changed

README.md

Lines changed: 60 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,57 @@
1-
# AWS {{THING}} Terraform module
1+
# AWS Cloudtrail Terraform module
22

3-
Terraform module which creates AWS {{THING}} resources.
3+
Terraform module which creates AWS Cloudtrail resources.
44

5-
## Usage
5+
This is an opinionated tool for creating a fairly boring Cloudtrail setup.
6+
7+
Features:
8+
* Multi Region Trail
9+
* Includes Global Events
10+
* Includes Management Events
11+
* Include Insights events
12+
* No Data events
13+
* If you need data events, you should write another trail with specific event selectors to manage scale and cost.
614

7-
See [`examples`](examples) directory for working examples to reference:
15+
## Usage
816

917
```hcl
10-
module "<THING>" {
11-
source = "platformod/<THING>"
18+
# To prevent a dependency loop and pass AWS runtime validations, create
19+
# the storage first, providing the computed arn of the trail to the
20+
# cloudtrail_s3 module
21+
22+
data "aws_caller_identity" "current" {}
23+
data "aws_partition" "current" {}
24+
data "aws_region" "current" {}
1225
13-
tags = {
14-
Terraform = "true"
15-
Environment = "dev"
16-
}
26+
locals {
27+
name = "zombocom-main"
28+
arn = "arn:${data.aws_partition.current}:cloudtrail:${data.aws_region.current}:${data.aws_caller_identity.account_id}:trail/${local.name}"
1729
}
18-
```
1930
20-
## Examples
31+
module "storage" {
32+
source = "platformod/cloudtrail-s3"
33+
version = 0.CHANGE_ME
34+
35+
# Creates a "${local.name}-cloudtrail" bucket
36+
name = local.name
37+
38+
account_trails = [
39+
{
40+
account = data.aws_caller_identity.current.account_id ,
41+
arn = local.arn
42+
},
43+
]
44+
}
45+
46+
module "trail" {
47+
source = "platformod/cloudtrail"
48+
version = 0.CHANGEME
2149
22-
Examples codified under the [`examples`](examples) are intended to give users references for how to use the module(s) as well as testing/validating changes to the source code of the module. If contributing to the project, please be sure to make any appropriate updates to the relevant examples to allow maintainers to test your changes and to keep the examples up to date for users. Thank you!
50+
name = local.name
51+
s3_bucket = "${local.name}-cloudtrail"
52+
}
2353
24-
- [Complete](complete)
54+
```
2555

2656
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
2757
## Requirements
@@ -33,32 +63,38 @@ Examples codified under the [`examples`](examples) are intended to give users re
3363

3464
## Providers
3565

36-
No providers.
66+
| Name | Version |
67+
|------|---------|
68+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 5.34.0 |
3769

3870
## Modules
3971

4072
No modules.
4173

4274
## Resources
4375

44-
No resources.
76+
| Name | Type |
77+
|------|------|
78+
| [aws_cloudtrail.trail](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudtrail) | resource |
4579

4680
## Inputs
4781

48-
No inputs.
82+
| Name | Description | Type | Default | Required |
83+
|------|-------------|------|---------|:--------:|
84+
| <a name="input_name"></a> [name](#input\_name) | A name for the trail, ideally the same value you used for the bucket name prefix | `string` | n/a | yes |
85+
| <a name="input_s3_bucket"></a> [s3\_bucket](#input\_s3\_bucket) | The name od the S3 bucket you created to store the logs | `string` | n/a | yes |
4986

5087
## Outputs
5188

52-
No outputs.
89+
| Name | Description |
90+
|------|-------------|
91+
| <a name="output_arn"></a> [arn](#output\_arn) | ARN of the trail |
92+
| <a name="output_home_region"></a> [home\_region](#output\_home\_region) | Region in which the trail was created |
5393
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
5494

95+
## Tests
5596

56-
## Thanks
57-
58-
Heavily inspired from the following template repos
59-
* https://github.com/clowdhaus/terraform-aws-module-template
60-
* https://github.com/trussworks/terraform-module-template
61-
* https://github.com/thesis/terraform-module-template-repo
97+
The tests in this repo will create and destroy real resources at AWS and incur cost. Please be careful when running them.
6298

6399
## License
64100

main.tf

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,32 @@
1-
# Implementation is left as an excercise for the reader ...
2-
locals {}
1+
# An opnionated cloudtrail.
2+
# Single trail, all management events, no data events
3+
4+
resource "aws_cloudtrail" "trail" {
5+
#checkov:skip=CKV_AWS_252:Prefer S3 notifications and EventBridge over SNS
6+
#checkov:skip=CKV_AWS_35:These are encrypted at rest in S3
7+
#checkov:skip=CKV2_AWS_10:We will offworld these to a seperte log provider, do not need CloudWatch here
8+
9+
name = var.name
10+
s3_bucket_name = var.s3_bucket
11+
12+
enable_log_file_validation = true
13+
14+
include_global_service_events = true
15+
is_multi_region_trail = true
16+
17+
insight_selector {
18+
insight_type = "ApiCallRateInsight"
19+
}
20+
21+
insight_selector {
22+
insight_type = "ApiErrorRateInsight"
23+
}
24+
25+
event_selector {
26+
include_management_events = true
27+
}
28+
29+
tags = {
30+
Name = var.name
31+
}
32+
}

outputs.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
output "arn" {
2+
description = "ARN of the trail"
3+
value = aws_cloudtrail.trail.arn
4+
}
5+
6+
output "home_region" {
7+
description = "Region in which the trail was created"
8+
value = aws_cloudtrail.trail.home_region
9+
}

tests/apply.tftest.hcl

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
provider "aws" {
2+
region = "us-east-2"
3+
default_tags {
4+
tags = {
5+
Environment = "Test"
6+
Repo = "platformod/terraform-aws-cloudtrail"
7+
CI = true
8+
}
9+
}
10+
}
11+
12+
variables {
13+
name = "test-8388737"
14+
}
15+
16+
run "gets" {
17+
module {
18+
source = "./tests/gets"
19+
}
20+
}
21+
22+
run "bucket" {
23+
variables {
24+
account_trails = [
25+
{
26+
account = run.gets.account_id,
27+
arn = "arn:aws:cloudtrail:us-east-2:${run.gets.account_id}:trail/${var.name}"
28+
}
29+
]
30+
}
31+
module {
32+
source = "platformod/cloudtrail-s3/aws"
33+
version = "1.0.1"
34+
}
35+
}
36+
37+
run "apply" {
38+
command = apply
39+
40+
variables {
41+
s3_bucket = "${var.name}-cloudtrail"
42+
}
43+
44+
assert {
45+
condition = aws_cloudtrail.trail.home_region == "us-east-2"
46+
error_message = "Trail not created in correct region"
47+
}
48+
49+
assert {
50+
condition = aws_cloudtrail.trail.arn == "arn:aws:cloudtrail:us-east-2:${run.gets.account_id}:trail/${var.name}"
51+
error_message = "Trail ARN. It ain't right"
52+
}
53+
}

tests/gets/.terraform.lock.hcl

Lines changed: 28 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/gets/main.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
data "aws_caller_identity" "current" {}

tests/gets/outputs.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
output "account_id" {
2+
value = data.aws_caller_identity.current.account_id
3+
}

variables.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
variable "name" {
2+
description = "A name for the trail, ideally the same value you used for the bucket name prefix"
3+
type = string
4+
}
5+
6+
variable "s3_bucket" {
7+
description = "The name od the S3 bucket you created to store the logs"
8+
type = string
9+
}

0 commit comments

Comments
 (0)