-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.tf
111 lines (103 loc) · 3.3 KB
/
main.tf
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
terraform {
required_providers {
aws = {
source = "hashicorp/aws"
configuration_aliases = [aws]
}
}
}
resource "aws_iam_role" "cross_account_role" {
name = "infracost-readonly"
assume_role_policy = jsonencode({
Version : "2012-10-17",
Statement = [
{
Effect : "Allow", Principal : { AWS : "arn:aws:iam::${var.infracost_account}:root" }, Action : "sts:AssumeRole",
Condition : { StringEquals : { "sts:ExternalId" : var.infracost_external_id } }
}
]
})
}
resource "aws_iam_policy" "management_account_readonly_policy" {
count = var.is_management_account ? 1 : 0
name = "infracost-management-account-readonly"
path = "/"
description = "Infracost management account read-only policy"
policy = jsonencode({
Version : "2012-10-17",
Statement : [
{
Action : [
// For getting the account names and tags
"organizations:ListAccounts",
"organizations:ListTagsForResource",
// For getting recommendations
"compute-optimizer:Get*",
"cost-optimization-hub:List*",
"cost-optimization-hub:Get*",
"trustedadvisor:Describe*",
"trustedadvisor:Get*",
"trustedadvisor:List*",
// For getting cost, usage and pricing data
"ce:Get*",
"ce:Describe*",
"ce:List*",
"pricing:Get*",
"pricing:List*",
"pricing:Describe*",
"bcm-pricing-calculator:*"
],
Resource : "*",
Effect : "Allow",
Sid : "InfracostManagementAccountReadOnly"
}
]
})
}
resource "aws_iam_role_policy_attachment" "management_account_readonly_policy_attachment" {
count = var.is_management_account ? 1 : 0
policy_arn = aws_iam_policy.management_account_readonly_policy[count.index].arn
role = aws_iam_role.cross_account_role.name
}
resource "aws_iam_policy" "member_account_readonly_policy" {
count = var.is_management_account ? 0 : 1
name = "infracost-member-account-readonly"
path = "/"
description = "Infracost member account read-only policy"
policy = jsonencode({
Version : "2012-10-17",
Statement : [
{
Action : [
"ec2:DescribeInstances",
"ec2:DescribeLaunchTemplates",
"ec2:DescribeVolumes",
"autoscaling:DescribeLaunchConfigurations",
"autoscaling:DescribeAutoScalingGroups",
"rds:DescribeDBInstances",
"rds:DescribeDBClusters",
"eks:ListClusters",
"eks:DescribeCluster",
"eks:ListNodegroups",
"eks:DescribeNodegroup",
"ecs:ListClusters",
"ecs:DescribeClusters",
"ecs:ListServices",
"ecs:DescribeServices",
"ecs:ListTaskDefinitions",
"ecs:DescribeTaskDefinition",
"lambda:ListFunctions"
],
Resource : "*",
Effect : "Allow",
Sid : "InfracostMemberAccountReadOnly"
}
]
})
}
# Attach policies to the role
resource "aws_iam_role_policy_attachment" "member_account_readonly_policy_attachment" {
count = var.is_management_account ? 0 : 1
policy_arn = aws_iam_policy.member_account_readonly_policy[count.index].arn
role = aws_iam_role.cross_account_role.name
}