-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathiam.tf
67 lines (57 loc) · 1.83 KB
/
iam.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
data "aws_iam_policy_document" "bastion_assume_role" {
statement {
actions = ["sts:AssumeRole"]
principals {
identifiers = ["ec2.amazonaws.com"]
type = "Service"
}
effect = "Allow"
}
}
resource "aws_iam_role" "bastion" {
name_prefix = "${var.name_prefix}bastion"
assume_role_policy = data.aws_iam_policy_document.bastion_assume_role.json
}
data "aws_iam_policy_document" "bastion_policy" {
# Allow downloading of user SSH public keys
statement {
actions = ["s3:GetObject"]
resources = ["${aws_s3_bucket.ssh_keys.arn}/*"]
effect = "Allow"
}
# Allow listing SSH public keys
statement {
actions = ["s3:ListBucket"]
resources = [aws_s3_bucket.ssh_keys.arn]
}
# Allow reading the host key secret
statement {
actions = ["secretsmanager:GetSecretValue"]
resources = [aws_secretsmanager_secret.bastion_host_key.arn]
}
# Allow use of the KMS key used to encrypt the host key secret
statement {
actions = ["kms:Decrypt"]
resources = [aws_kms_key.bastion_host_key_encryption_key.arn]
}
}
resource "aws_iam_policy" "bastion" {
name_prefix = "${var.name_prefix}bastion"
policy = data.aws_iam_policy_document.bastion_policy.json
}
resource "aws_iam_role_policy_attachment" "bastion_policy" {
role = aws_iam_role.bastion.name
policy_arn = aws_iam_policy.bastion.arn
}
data "aws_iam_policy" "cloudwatch_agent" {
arn = "arn:aws:iam::aws:policy/CloudWatchAgentServerPolicy"
}
resource "aws_iam_role_policy_attachment" "cloudwatch_agent" {
count = var.log_group_name == null ? 0 : 1
role = aws_iam_role.bastion.name
policy_arn = data.aws_iam_policy.cloudwatch_agent.arn
}
resource "aws_iam_instance_profile" "bastion_host_profile" {
name_prefix = "${var.name_prefix}bastion-profile"
role = aws_iam_role.bastion.name
}