-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathiam.tf
More file actions
73 lines (65 loc) · 1.68 KB
/
Copy pathiam.tf
File metadata and controls
73 lines (65 loc) · 1.68 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
# Create IAM Role for EC2 Instance
resource "aws_iam_role" "ec2_instance_role" {
name = "${var.name}-ec2-role"
assume_role_policy = <<-EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
# Attach IAM Policy to role (Write file to S3)
resource "aws_iam_policy" "ec2_instance_role_policy" {
depends_on = [
aws_s3_bucket.k3sup_bucket
]
name = "${var.name}-ec2-policy"
description = "Policy for EC2 Instance"
policy = jsonencode(
{
Version = "2012-10-17",
Statement = [
{
Effect = "Allow",
Action = [
"s3:PutObject",
"s3:PutObjectAcl",
"s3:PutObjectTagging",
"s3:PutObjectVersionAcl",
"s3:PutObjectVersionTagging",
],
Resource = [
"arn:aws:s3:::${aws_s3_bucket.k3sup_bucket.id}/*",
"arn:aws:s3:::${aws_s3_bucket.k3sup_bucket.id}"
]
}
]
}
)
}
resource "aws_iam_policy_attachment" "role_policy_attachment" {
depends_on = [
aws_iam_policy.ec2_instance_role_policy,
aws_iam_role.ec2_instance_role
]
name = "${var.name}-ec2-policy-attachment"
roles = [aws_iam_role.ec2_instance_role.name]
policy_arn = aws_iam_policy.ec2_instance_role_policy.arn
}
# Instance profile
resource "aws_iam_instance_profile" "ec2_instance_profile" {
depends_on = [
aws_iam_role.ec2_instance_role
]
name = "${var.name}-ec2-profile"
role = aws_iam_role.ec2_instance_role.name
}