-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathiam.tf
More file actions
134 lines (114 loc) · 3.28 KB
/
iam.tf
File metadata and controls
134 lines (114 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# defining aws roles and policies for a lambda function
data "aws_iam_policy_document" "lambda-assume-role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["lambda.amazonaws.com"]
}
}
}
resource "aws_iam_policy" "lambda_eks_policy" {
name = "lambda_eks_policy"
path = "/"
description = "Policy to provide permissions to lambda functions"
policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"sts:GetCallerIdentity",
"eks:DescribeCluster",
"autoscaling:*",
"ec2:TerminateInstances",
"ec2:StopInstances"
],
"Effect": "Allow",
"Resource": "*"
}
]
}
EOF
}
resource "aws_iam_role_policy_attachment" "lambda_eks_policy" {
role = aws_iam_role.lambda-exec.name
policy_arn = aws_iam_policy.lambda_eks_policy.arn
}
resource "aws_iam_role" "lambda-exec" {
name = "${var.name}-lambda-exec-role"
tags = var.tags
max_session_duration = 43200
assume_role_policy = data.aws_iam_policy_document.lambda-assume-role.json
}
resource "aws_iam_role_policy_attachment" "lambda-exec" {
role = aws_iam_role.lambda-exec.name
policy_arn = "arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole"
}
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSClusterPolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSClusterPolicy"
role = aws_iam_role.lambda-exec.name
}
resource "aws_iam_role_policy_attachment" "cluster_AmazonEKSServicePolicy" {
policy_arn = "arn:aws:iam::aws:policy/AmazonEKSServicePolicy"
role = aws_iam_role.lambda-exec.name
}
# defining aws roles and policies for a step functions state machine
data "aws_iam_policy_document" "sfn-assume-role" {
statement {
actions = ["sts:AssumeRole"]
principals {
type = "Service"
identifiers = ["states.${var.aws_region}.amazonaws.com"]
}
}
}
resource "aws_iam_role" "sfn-exec" {
name = "${var.name}-sfn-exec"
tags = var.tags
max_session_duration = 43200
assume_role_policy = data.aws_iam_policy_document.sfn-assume-role.json
}
data "aws_iam_policy_document" "lambda-invoke" {
statement {
actions = [
"lambda:InvokeFunction"
]
resources = [
"*",
]
}
}
resource "aws_iam_policy" "lambda-invoke" {
name = "${var.name}-lambda-invoke"
policy = data.aws_iam_policy_document.lambda-invoke.json
}
resource "aws_iam_role_policy_attachment" "lambda-invoke" {
role = aws_iam_role.sfn-exec.name
policy_arn = aws_iam_policy.lambda-invoke.arn
}
resource "kubernetes_cluster_role" "lambda-access" {
metadata {
name = "lambda-access"
}
rule {
api_groups = [""]
resources = ["nodes", "pods"]
verbs = ["get", "list", "watch", "patch"]
}
}
resource "kubernetes_cluster_role_binding" "lambda-user-role-binding" {
metadata {
name = "lambda-user-role-binding"
}
role_ref {
api_group = "rbac.authorization.k8s.io"
kind = "ClusterRole"
name = "lambda-access"
}
subject {
kind = "User"
name = "lambda"
api_group = "rbac.authorization.k8s.io"
}
}