Commit 2aef5c52 authored by Chris Merrett's avatar Chris Merrett

Added support for delegated access to ops Cloudtrail bucket

parent eea43bff
Pipeline #772 passed with stage
in 5 seconds
...@@ -8,6 +8,8 @@ Input Variables ...@@ -8,6 +8,8 @@ Input Variables
- `customer_name` - The name of the custmer/enduser that you're using within Terraform. - `customer_name` - The name of the custmer/enduser that you're using within Terraform.
- `environment` - The environment name that you're assigning. As this module is designed for "ops" accounts, "ops" is the default. - `environment` - The environment name that you're assigning. As this module is designed for "ops" accounts, "ops" is the default.
- `child_accounts` - A list of AWS account ID's that will also be writing Cloudtrail information to the ops bucket.
Outputs Outputs
------- -------
...@@ -23,6 +25,12 @@ You can use these in your Terraform template with the following steps. ...@@ -23,6 +25,12 @@ You can use these in your Terraform template with the following steps.
1.) Adding a module resource to your template, e.g. `main.tf` 1.) Adding a module resource to your template, e.g. `main.tf`
``` ```
variable "name" {}
variable "environment" {}
variable "aws_child_accounts" {
type = "list"
}
############################################################################################################# #############################################################################################################
# CloudTrail (Ops Account) # CloudTrail (Ops Account)
############################################################################################################# #############################################################################################################
...@@ -31,5 +39,6 @@ module "cloudtrail_ops" { ...@@ -31,5 +39,6 @@ module "cloudtrail_ops" {
customer_name = "${var.name}" customer_name = "${var.name}"
environment = "${var.environment}" environment = "${var.environment}"
child_accounts = "${var.aws_child_accounts}"
} }
``` ```
...@@ -7,18 +7,65 @@ variable "environment" { ...@@ -7,18 +7,65 @@ variable "environment" {
default = "ops" default = "ops"
} }
data "template_file" "s3_policy" { variable "child_accounts" {
template = "${file("${path.module}/s3_policy.json.tpl")}" description = "List of child AWS account ID's"
vars { type = "list"
customer_name = "${var.customer_name}" }
environment = "${var.environment}"
data "aws_iam_policy_document" "main" {
statement {
sid = "AWSCloudTrailAclCheck"
effect = "Allow"
actions = ["s3:GetBucketAcl"]
resources = "arn:aws:s3:::${var.customer_name}-${var.environment}-cloudtrail"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
statement {
sid = "AWSCloudTrailWrite"
effect = "Allow"
actions = ["s3:PutObject"]
resources = "arn:aws:s3:::${var.customer_name}-${var.environment}-cloudtrail/*"
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
}
statement {
sid = "AWSCloudTrailWrite"
effect = "Allow"
actions = ["s3:PutObject"]
resources = ["${formatlist("arn:aws:s3:::$${var.customer_name}-$${var.environment}-cloudtrail/AWSLogs/%s/*", var.child_accounts)}"]
principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
condition {
test = "StringEquals"
variable = "s3:x-amz-acl"
values = ["bucket-owner-full-control"]
}
} }
} }
resource "aws_s3_bucket" "main" { resource "aws_s3_bucket" "main" {
bucket = "${var.customer_name}-${var.environment}-cloudtrail" bucket = "${var.customer_name}-${var.environment}-cloudtrail"
force_destroy = true force_destroy = true
policy = "${data.template_file.s3_policy.rendered}" policy = "${data.aws_iam_policy_document.main.json}"
} }
resource "aws_cloudtrail" "main" { resource "aws_cloudtrail" "main" {
......
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