@@ -37,17 +37,16 @@ resource "aws_dynamodb_table" "terraform_locks" {
3737# IAM OIDC Provider for GitHub
3838# ----------------------------
3939resource "aws_iam_openid_connect_provider" "github" {
40- url = " https://token.actions.githubusercontent.com"
41- client_id_list = [" sts.amazonaws.com" ]
42- thumbprint_list = [" 6938fd4d98bab03faadb97b34396831e3780aea1 " ]
40+ url = " https://token.actions.githubusercontent.com"
41+ client_id_list = [" sts.amazonaws.com" ]
42+ thumbprint_list = [" 1c58a3a8518e8759bf075b76b750d4f2df264fcd " ]
4343}
4444
4545# ----------------------------
4646# IAM Role for GitHub Actions (Scoped)
4747# ----------------------------
4848resource "aws_iam_role" "github_actions_infra_role" {
4949 name = " GitHubActionsInfraRole"
50-
5150 assume_role_policy = jsonencode ({
5251 Version = " 2012-10-17" ,
5352 Statement = [
@@ -59,9 +58,10 @@ resource "aws_iam_role" "github_actions_infra_role" {
5958 Action = " sts:AssumeRoleWithWebIdentity" ,
6059 Condition = {
6160 StringEquals = {
62- # Replace with your repo path
6361 " token.actions.githubusercontent.com:aud" = " sts.amazonaws.com"
64- " token.actions.githubusercontent.com:sub" = " repo:YOUR_GITHUB_USER/YOUR_REPO:ref:refs/heads/main"
62+ },
63+ StringLike = {
64+ " token.actions.githubusercontent.com:sub" = " repo:karimzakzouk/graduation-project-devops:*"
6565 }
6666 }
6767 }
@@ -70,73 +70,89 @@ resource "aws_iam_role" "github_actions_infra_role" {
7070}
7171
7272# ----------------------------
73- # Scoped Policy for Infra Role
73+ # Complete Policy for GitHub Actions
7474# ----------------------------
7575resource "aws_iam_role_policy" "github_actions_infra_policy" {
7676 name = " GitHubActionsInfraPolicy"
7777 role = aws_iam_role. github_actions_infra_role . id
78-
7978 policy = jsonencode ({
8079 Version = " 2012-10-17" ,
8180 Statement = [
8281 {
8382 Effect = " Allow" ,
8483 Action = [
85- # VPC
86- " ec2:CreateVpc" ,
87- " ec2:DescribeVpcs" ,
88- " ec2:DeleteVpc" ,
89- " ec2:CreateSubnet" ,
90- " ec2:DescribeSubnets" ,
91- " ec2:DeleteSubnet" ,
92- " ec2:CreateRouteTable" ,
93- " ec2:DescribeRouteTables" ,
94- " ec2:AssociateRouteTable" ,
95- " ec2:CreateInternetGateway" ,
96- " ec2:DescribeInternetGateways" ,
97- " ec2:AttachInternetGateway" ,
98- " ec2:CreateNatGateway" ,
99- " ec2:DescribeNatGateways" ,
100- " ec2:AllocateAddress" ,
101- " ec2:DescribeAddresses" ,
102- " ec2:CreateSecurityGroup" ,
103- " ec2:DescribeSecurityGroups" ,
104- " ec2:AuthorizeSecurityGroupIngress" ,
105- " ec2:AuthorizeSecurityGroupEgress" ,
106- " ec2:DeleteSecurityGroup" ,
107- " ec2:DeleteRouteTable"
108- ],
109- Resource = " *"
110- },
111- {
112- Effect = " Allow" ,
113- Action = [
114- # EKS
115- " eks:CreateCluster" ,
116- " eks:DescribeCluster" ,
117- " eks:UpdateClusterConfig" ,
118- " eks:DeleteCluster" ,
119- " eks:ListClusters" ,
120- " eks:CreateNodegroup" ,
121- " eks:DeleteNodegroup"
122- ],
123- Resource = " *"
124- },
125- {
126- Effect = " Allow" ,
127- Action = [
128- # IAM for node roles
129- " iam:PassRole" ,
130- " iam:GetRole" ,
84+ # S3 permissions for Terraform state
85+ " s3:GetObject" ,
86+ " s3:PutObject" ,
87+ " s3:DeleteObject" ,
88+ " s3:ListBucket" ,
89+ " s3:GetBucketVersioning" ,
90+ " s3:GetBucketAcl" ,
91+ " s3:GetBucketPolicy" ,
92+ " s3:PutBucketVersioning" ,
93+ " s3:PutBucketAcl" ,
94+ " s3:PutBucketPolicy" ,
95+ " s3:CreateBucket" ,
96+ " s3:DeleteBucket" ,
97+ " s3:GetBucketLocation" ,
98+ " s3:ListAllMyBuckets" ,
99+
100+ # DynamoDB for state locking
101+ " dynamodb:GetItem" ,
102+ " dynamodb:PutItem" ,
103+ " dynamodb:DeleteItem" ,
104+ " dynamodb:CreateTable" ,
105+ " dynamodb:DeleteTable" ,
106+ " dynamodb:DescribeTable" ,
107+ " dynamodb:TagResource" ,
108+ " dynamodb:UntagResource" ,
109+ " dynamodb:ListTagsOfResource" ,
110+
111+ # EC2/VPC permissions
112+ " ec2:*" ,
113+
114+ # EKS permissions
115+ " eks:*" ,
116+
117+ # IAM permissions (be more specific in production)
131118 " iam:CreateRole" ,
119+ " iam:DeleteRole" ,
120+ " iam:GetRole" ,
121+ " iam:ListRoles" ,
122+ " iam:PassRole" ,
132123 " iam:AttachRolePolicy" ,
133- " iam:PutRolePolicy"
124+ " iam:DetachRolePolicy" ,
125+ " iam:ListAttachedRolePolicies" ,
126+ " iam:CreatePolicy" ,
127+ " iam:DeletePolicy" ,
128+ " iam:GetPolicy" ,
129+ " iam:ListPolicies" ,
130+ " iam:GetPolicyVersion" ,
131+ " iam:ListPolicyVersions" ,
132+ " iam:CreatePolicyVersion" ,
133+ " iam:DeletePolicyVersion" ,
134+ " iam:SetDefaultPolicyVersion" ,
135+ " iam:PutRolePolicy" ,
136+ " iam:GetRolePolicy" ,
137+ " iam:DeleteRolePolicy" ,
138+ " iam:ListRolePolicies" ,
139+ " iam:CreateInstanceProfile" ,
140+ " iam:DeleteInstanceProfile" ,
141+ " iam:GetInstanceProfile" ,
142+ " iam:AddRoleToInstanceProfile" ,
143+ " iam:RemoveRoleFromInstanceProfile" ,
144+ " iam:ListInstanceProfiles" ,
145+ " iam:ListInstanceProfilesForRole" ,
146+ " iam:TagRole" ,
147+ " iam:UntagRole" ,
148+ " iam:ListRoleTags" ,
149+
150+ # Additional permissions for EKS
151+ " autoscaling:*" ,
152+ " logs:*" ,
153+ " application-autoscaling:*"
134154 ],
135- Resource = [
136- # Node roles your workflow will create
137- " arn:aws:iam::*:role/*-node-role" ,
138- " arn:aws:iam::*:role/*-cluster-role"
139- ]
155+ Resource = " *"
140156 }
141157 ]
142158 })
0 commit comments