Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S3: added variable and new resources #6

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions storage/s3/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Annotation
The S3 module is used to create s3 bucket on AWS.

## Used modules

- [iam-assumable-role](https://github.com/terraform-aws-modules/terraform-aws-iam/tree/v4.3.0/modules/iam-assumable-role)


## Feature

- IAM assumable role for accessing S3 bucket by other resources
- AWS cloudwatch group that collects logs from AWS Cloudtrail

## Example usage
```
module "s3" {
depends_on = [ module.kubernetes]
source = "./sak-incubator/storage/s3"
s3_bucket_name = "${module.kubernetes.cluster_name}-main"
cluster_name = module.kubernetes.cluster_name
trusted_role_arns = [ module.kubernetes.this.cluster_iam_role_arn ]
s3_cloudwatch_logging_enabled = true
}
```
119 changes: 114 additions & 5 deletions storage/s3/main.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,91 @@
resource "aws_s3_bucket" "kubeflow" {
resource "aws_cloudwatch_log_group" "s3_cloudtrail_logs" {
count = var.s3_cloudwatch_logging_enabled ? 1 : 0
name = "s3-cloudtrail-logs-${var.s3_bucket_name}"
}

data "aws_iam_policy_document" "cloudtrail-assume-role-policy" {
statement {
actions = ["sts:AssumeRole"]

principals {
type = "Service"
identifiers = ["cloudtrail.amazonaws.com"]
}
}
}



resource "aws_s3_bucket" "s3_cloudtrail_logs" {

count = var.s3_cloudwatch_logging_enabled ? 1 : 0
bucket = "s3-cloudtrail-logs-${var.s3_bucket_name}"
tags = var.tags

policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AWSCloudTrailAclCheck",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "s3:GetBucketAcl",
"Resource": "arn:aws:s3:::s3-cloudtrail-logs-${var.s3_bucket_name}"
},
{
"Sid": "AWSCloudTrailWrite",
"Effect": "Allow",
"Principal": {
"Service": "cloudtrail.amazonaws.com"
},
"Action": "s3:PutObject",
"Resource": "arn:aws:s3:::s3-cloudtrail-logs-${var.s3_bucket_name}/*",
"Condition": {
"StringEquals": {
"s3:x-amz-acl": "bucket-owner-full-control"
}
}
}
]
}
POLICY
}


resource "aws_iam_role" "cloudtrail_to_cloudwatch" {
count = var.s3_cloudwatch_logging_enabled ? 1 : 0
name = "CloudWatchWriteForCloudTrail"

# Terraform's "jsonencode" function converts a
# Terraform expression result to valid JSON syntax.
assume_role_policy = data.aws_iam_policy_document.cloudtrail-assume-role-policy.json
inline_policy {
name = "cloudwatch_write_permissions"

policy = jsonencode({
Version = "2012-10-17"
Statement = [
{
Action = [
"cloudwatch:PutMetricData",
"logs:PutLogEvents",
"logs:DescribeLogStreams",
"logs:DescribeLogGroups",
"logs:CreateLogStream",
"logs:CreateLogGroup"
]
Effect = "Allow"
Resource = "*"
},
]
})
}
}

resource "aws_s3_bucket" "main" {

bucket = var.s3_bucket_name
tags = var.tags
Expand All @@ -16,6 +103,28 @@ resource "aws_s3_bucket" "kubeflow" {
}
}

resource "aws_cloudtrail" "s3" {
count = var.s3_cloudwatch_logging_enabled ? 1 : 0
name = "s3-bucket-trail"
s3_bucket_name = aws_s3_bucket.s3_cloudtrail_logs[0].id
s3_key_prefix = "trail"
include_global_service_events = false


event_selector {
read_write_type = "All"
include_management_events = false

data_resource {
type = "AWS::S3::Object"
values = ["${aws_s3_bucket.main.arn}/"]
}
}

cloud_watch_logs_group_arn = "${aws_cloudwatch_log_group.s3_cloudtrail_logs[0].arn}:*" # CloudTrail requires the Log Stream wildcard
cloud_watch_logs_role_arn = aws_iam_role.cloudtrail_to_cloudwatch[0].arn
}


// create read-write user for S3 bucket
resource "aws_iam_user" "s3_user" {
Expand All @@ -39,13 +148,13 @@ resource "aws_iam_user_policy" "s3_user" {
"Sid": "ListObjectsInBucket",
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["${aws_s3_bucket.kubeflow.arn}"]
"Resource": ["${aws_s3_bucket.main.arn}"]
},
{
"Sid": "AllObjectActions",
"Effect": "Allow",
"Action": "s3:*Object",
"Resource": ["${aws_s3_bucket.kubeflow.arn}/*"]
"Resource": ["${aws_s3_bucket.main.arn}/*"]
}
]
}
Expand All @@ -69,13 +178,13 @@ resource "aws_iam_policy" "s3_role" {
"Sid": "ListObjectsInBucket",
"Effect": "Allow",
"Action": ["s3:ListBucket"],
"Resource": ["${aws_s3_bucket.kubeflow.arn}"]
"Resource": ["${aws_s3_bucket.main.arn}"]
},
{
"Sid": "AllObjectActions",
"Effect": "Allow",
"Action": "s3:*Object",
"Resource": ["${aws_s3_bucket.kubeflow.arn}/*"]
"Resource": ["${aws_s3_bucket.main.arn}/*"]
}
]
}
Expand Down
4 changes: 2 additions & 2 deletions storage/s3/output.tf
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ output "s3_user_access_key" {
}
}
output "s3_bucket_arn" {
value = aws_s3_bucket.kubeflow.arn
value = aws_s3_bucket.main.arn
}


output "s3_bucket_name" {
value = aws_s3_bucket.kubeflow.bucket
value = aws_s3_bucket.main.bucket
}
7 changes: 7 additions & 0 deletions storage/s3/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,10 @@ variable "trusted_role_arns" {
description = "ARNs of roles that are allowed to assume the role for read/write access to the S3 bucket"
default = []
}


variable "s3_cloudwatch_logging_enabled" {
type = bool
default = false
description = "By making it true, logs will be stored to cloudwatch with help of cloudtrail"
}