1
+ // iam user
1
2
resource "aws_iam_user" "login_for_external_user" {
2
- name = var. iam_name_prefix
3
- tags = var. tags
3
+ count = var. iam_user_name_prefix != null ? 1 : 0
4
+ name = var. iam_user_name_prefix
5
+ tags = var. tags
4
6
}
5
7
8
+ resource "aws_iam_user_policy" "assume_nexus_role" {
9
+ count = var. iam_user_name_prefix != null ? 1 : 0
10
+ name = " assume_role_nexus"
11
+ user = aws_iam_user. login_for_external_user . 0 . name
12
+
13
+ policy = << EOF
14
+ {
15
+ "Version": "2012-10-17",
16
+ "Statement": [
17
+ {
18
+ "Action": "sts:AssumeRole",
19
+ "Effect": "Allow",
20
+ "Resource": "${ aws_iam_role . access_s3_bucket_nexus . arn } "
21
+ }
22
+ ]
23
+ }
24
+ EOF
25
+ }
6
26
27
+ // iam role
7
28
resource "aws_iam_role" "access_s3_bucket_nexus" {
8
- name_prefix = var. iam_name_prefix
29
+ name = var. iam_role_name
9
30
10
31
assume_role_policy = << EOF
11
32
{
@@ -14,18 +35,9 @@ resource "aws_iam_role" "access_s3_bucket_nexus" {
14
35
{
15
36
"Action": "sts:AssumeRole",
16
37
"Principal": {
17
- "AWS": " ${ aws_iam_user . login_for_external_user . arn } "
38
+ "AWS": ${ jsonencode ( aws_iam_user. login_for_external_user . * . arn ) }
18
39
},
19
- "Effect": "Allow",
20
- "Sid": "iam_user"
21
- },
22
- {
23
- "Action": "sts:AssumeRole",
24
- "Principal": {
25
- "Service": "ec2.amazonaws.com"
26
- },
27
- "Effect": "Allow",
28
- "Sid": "ec2_instance"
40
+ "Effect": "Allow"
29
41
}
30
42
]
31
43
}
@@ -60,8 +72,8 @@ data "aws_iam_policy_document" "bucket_policy" {
60
72
}
61
73
62
74
resource "aws_iam_policy" "policy_s3_bucket" {
63
- name_prefix = var. iam_name_prefix
64
- description = " A test policy "
75
+ name_prefix = var. iam_role_name
76
+ description = " Nexus Access to S3 bucket "
65
77
66
78
policy = data. aws_iam_policy_document . bucket_policy . json
67
79
}
@@ -71,6 +83,7 @@ resource "aws_iam_role_policy_attachment" "attach-s3-policy" {
71
83
policy_arn = aws_iam_policy. policy_s3_bucket . arn
72
84
}
73
85
86
+ // s3 bucket
74
87
module "s3_bucket" {
75
88
source = " terraform-aws-modules/s3-bucket/aws"
76
89
0 commit comments