-
Notifications
You must be signed in to change notification settings - Fork 8
/
gitlab.tf
107 lines (102 loc) · 3.18 KB
/
gitlab.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
# Provides EKS focused policy documents for GitLab EKS management
# Reference: https://gitlab.com/help/user/project/clusters/add_eks_clusters.md#new-eks-cluster
data "aws_iam_policy_document" "gitlab" {
statement {
sid = "GitLabEKSPolicy"
actions = [
"autoscaling:CreateAutoScalingGroup",
"autoscaling:DescribeAutoScalingGroups",
"autoscaling:DescribeScalingActivities",
"autoscaling:UpdateAutoScalingGroup",
"autoscaling:CreateLaunchConfiguration",
"autoscaling:DescribeLaunchConfigurations",
"cloudformation:CreateStack",
"cloudformation:DescribeStacks",
"ec2:AuthorizeSecurityGroupEgress",
"ec2:AuthorizeSecurityGroupIngress",
"ec2:RevokeSecurityGroupEgress",
"ec2:RevokeSecurityGroupIngress",
"ec2:CreateSecurityGroup",
"ec2:createTags",
"ec2:DescribeImages",
"ec2:DescribeKeyPairs",
"ec2:DescribeRegions",
"ec2:DescribeSecurityGroups",
"ec2:DescribeSubnets",
"ec2:DescribeVpcs",
"eks:CreateCluster",
"eks:DescribeCluster",
"iam:AddRoleToInstanceProfile",
"iam:AttachRolePolicy",
"iam:CreateRole",
"iam:CreateInstanceProfile",
"iam:CreateServiceLinkedRole",
"iam:GetRole",
"iam:ListRoles",
"iam:ListAttachedRolePolicies", # Added per https://gitlab.com/gitlab-org/gitlab/-/issues/232960
"iam:PassRole",
"ssm:GetParameters"
]
resources = ["*"]
effect = "Allow"
}
}
# Provides IAM policy based on the EKS policy document
resource "aws_iam_policy" "gitlab" {
name = "gitlab-eks-policy"
description = "IAM Policy for GitLab EKS"
policy = data.aws_iam_policy_document.gitlab.json
}
# Provides a cross-account role for GitLab to authenticate and manage the EKS cluster
resource "aws_iam_role" "gitlab" {
name = "gitlab-eks-role"
description = "Provides a cross-account role for GitLab to authenticate and manage the EKS cluster"
assume_role_policy = <<POLICY
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"AWS": [
"arn:aws:iam::${var.gitlab_account_id}:root"
]
},
"Action": "sts:AssumeRole",
"Condition": {
"StringEquals": {
"sts:ExternalId": "${var.gitlab_external_id}"
}
}
}
]
}
POLICY
tags = {
environment = var.aws_tag_environment
source = "Terraform"
}
}
# Provides an attachment of the GitLabEKSPolicy policy to the new GitLab role
resource "aws_iam_role_policy_attachment" "gitlab" {
policy_arn = aws_iam_policy.gitlab.arn
role = aws_iam_role.gitlab.name
}
# Provides the VPC ID value
data "aws_vpc" "vpc" {
filter {
name = "tag:Name"
values = ["${var.aws_vpc_name}"]
}
}
# Provides an AWS security group for the GitLab EKS Cluster
resource "aws_security_group" "gitlab" {
name = "gitlab-eks-sg"
description = "GitLab EKS Cluster"
vpc_id = data.aws_vpc.vpc.id
tags = {
Name = "gitlab-eks-sg"
environment = var.aws_tag_environment
source = "Terraform"
}
}