-
Notifications
You must be signed in to change notification settings - Fork 2
/
iam.tf
52 lines (44 loc) · 1.53 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
data "aws_iam_policy_document" "ec2_assume_role" {
statement {
actions = [
"sts:AssumeRole"
]
principals {
type = "Service"
identifiers = ["ec2.amazonaws.com"]
}
}
}
data "aws_iam_policy_document" "wireguard_policy_doc" {
statement {
actions = [
"ec2:AssociateAddress",
"ssm:GetParameter"
]
resources = ["*"]
}
}
data "aws_caller_identity" "current" {}
resource "aws_iam_policy" "wireguard_policy" {
name = "${var.env}-${var.region}-tf-wireguard"
description = "Terraform Managed. Allows Wireguard instance to attach EIP."
policy = data.aws_iam_policy_document.wireguard_policy_doc.json
count = (var.use_eip ? 1 : 0) # only used for EIP mode
}
resource "aws_iam_role" "wireguard_role" {
name = "${var.env}-${var.region}-tf-wireguard"
description = "Terraform Managed. Role to allow Wireguard instance to attach EIP."
path = "/"
assume_role_policy = data.aws_iam_policy_document.ec2_assume_role.json
count = (var.use_eip ? 1 : 0) # only used for EIP mode
}
resource "aws_iam_role_policy_attachment" "wireguard_roleattach" {
role = aws_iam_role.wireguard_role[0].name
policy_arn = aws_iam_policy.wireguard_policy[0].arn
count = (var.use_eip ? 1 : 0) # only used for EIP mode
}
resource "aws_iam_instance_profile" "wireguard_profile" {
name = "${var.env}-${var.region}-tf-wireguard"
role = aws_iam_role.wireguard_role[0].name
count = (var.use_eip ? 1 : 0) # only used for EIP mode
}