mirror of https://github.com/grafana/loki
Provide Terraform script for setting up S3 and document role based access. (#7656)
**What this PR does / why we need it**: Role based access to S3 on an EKS cluster via service account annotation was not documented well and let to some confusion. This change documents how a role based accessis achieved with providing an AWS secret access key and access key id. Thus there is no change of accidentally leaking credentials. A Terraform module is provided under `production/terraform/modules/s3` that provisions and S3 bucket and IAM role and policy. **Which issue(s) this PR fixes**: Fixes #7279 **Special notes for your reviewer**: **Checklist** - [ ] Reviewed the `CONTRIBUTING.md` guide - [x] Documentation added - [ ] Tests updated - [ ] `CHANGELOG.md` updated - [ ] Changes that require user attention or interaction to upgrade are documented in `docs/sources/upgrading/_index.md` Co-authored-by: Pablo Angulo <pablo.angulo@grafana.com>pull/7500/head
parent
6d05ade6a8
commit
691b8be6c7
@ -0,0 +1,106 @@ |
||||
provider "aws" { |
||||
region = var.region |
||||
} |
||||
|
||||
data "aws_caller_identity" "current" {} |
||||
|
||||
data "aws_eks_cluster" "current" { |
||||
name = var.cluster_name |
||||
} |
||||
|
||||
locals { |
||||
oidc_id = replace(data.aws_eks_cluster.current.identity[0].oidc[0].issuer, "https://", "") |
||||
} |
||||
|
||||
data "aws_iam_policy_document" "oidc" { |
||||
statement { |
||||
actions = ["sts:AssumeRoleWithWebIdentity"] |
||||
effect = "Allow" |
||||
|
||||
condition { |
||||
test = "StringEquals" |
||||
variable = "${local.oidc_id}:sub" |
||||
values = ["system:serviceaccount:${var.namespace}:${var.serviceaccount}"] |
||||
} |
||||
|
||||
condition { |
||||
test = "StringEquals" |
||||
variable = "${local.oidc_id}:aud" |
||||
values = ["sts.amazonaws.com"] |
||||
} |
||||
|
||||
principals { |
||||
identifiers = [ |
||||
"arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/${local.oidc_id}" |
||||
] |
||||
type = "Federated" |
||||
} |
||||
} |
||||
} |
||||
|
||||
resource "aws_s3_bucket" "loki-data" { |
||||
bucket = "${var.bucket_name}" |
||||
} |
||||
|
||||
resource "aws_s3_bucket_policy" "grant-access" { |
||||
bucket = aws_s3_bucket.loki-data.id |
||||
policy = jsonencode({ |
||||
Version: "2012-10-17", |
||||
Statement: [ |
||||
{ |
||||
Sid: "Statement1", |
||||
Effect: "Allow", |
||||
Principal: { |
||||
AWS: aws_iam_role.loki.arn |
||||
}, |
||||
Action: [ |
||||
"s3:PutObject", |
||||
"s3:GetObject", |
||||
"s3:DeleteObject", |
||||
"s3:ListBucket" |
||||
], |
||||
Resource: [ |
||||
aws_s3_bucket.loki-data.arn, |
||||
"${aws_s3_bucket.loki-data.arn}/*" |
||||
] |
||||
} |
||||
] |
||||
}) |
||||
} |
||||
|
||||
resource "aws_iam_role" "loki" { |
||||
name = "LokiStorage-${var.cluster_name}" |
||||
assume_role_policy = data.aws_iam_policy_document.oidc.json |
||||
|
||||
inline_policy {} |
||||
} |
||||
|
||||
resource "aws_iam_policy" "loki" { |
||||
name = "LokiStorageAccessPolicy-${var.bucket_name}" |
||||
path = "/" |
||||
description = "Allows Loki to access bucket" |
||||
|
||||
policy = jsonencode({ |
||||
Version: "2012-10-17", |
||||
Statement: [ |
||||
{ |
||||
Effect: "Allow", |
||||
Action: [ |
||||
"s3:ListBucket", |
||||
"s3:PutObject", |
||||
"s3:GetObject", |
||||
"s3:DeleteObject" |
||||
], |
||||
Resource: [ |
||||
aws_s3_bucket.loki-data.arn, |
||||
"${aws_s3_bucket.loki-data.arn}/*" |
||||
] |
||||
} |
||||
] |
||||
}) |
||||
} |
||||
|
||||
resource "aws_iam_role_policy_attachment" "loki-attach" { |
||||
role = aws_iam_role.loki.name |
||||
policy_arn = aws_iam_policy.loki.arn |
||||
} |
||||
@ -0,0 +1,4 @@ |
||||
output "annotation" { |
||||
description = "Service account annotation" |
||||
value = "eks.amazonaws.com/role-arn=${aws_iam_role.loki.arn}" |
||||
} |
||||
@ -0,0 +1,26 @@ |
||||
variable "region" { |
||||
description = "AWS region" |
||||
type = string |
||||
default = "us-east-2" |
||||
} |
||||
|
||||
variable "bucket_name" { |
||||
description = "Bucket name for Loki storage" |
||||
type = string |
||||
} |
||||
|
||||
variable "cluster_name" { |
||||
description = "Name of EKS cluster" |
||||
type = string |
||||
} |
||||
|
||||
variable "namespace" { |
||||
description = "Namespace of Loki installation" |
||||
type = string |
||||
} |
||||
|
||||
variable "serviceaccount" { |
||||
description = "Service account of Loki installation" |
||||
type = string |
||||
default = "loki" |
||||
} |
||||
@ -0,0 +1,15 @@ |
||||
terraform { |
||||
required_providers { |
||||
aws = { |
||||
source = "hashicorp/aws" |
||||
version = "~> 4.15.0" |
||||
} |
||||
|
||||
random = { |
||||
source = "hashicorp/random" |
||||
version = "3.1.0" |
||||
} |
||||
} |
||||
|
||||
required_version = "> 1.2.0" |
||||
} |
||||
Loading…
Reference in new issue