@@ -87,3 +87,62 @@ resource "aws_iam_role_policy_attachment" "github_tf_opentofu_policy_attachment"
87
87
role = aws_iam_role. github_tf . name
88
88
policy_arn = aws_iam_policy. opentofu_policy . arn
89
89
}
90
+
91
+ data "aws_iam_policy_document" "ecr_policy_document" {
92
+ statement {
93
+ effect = " Allow"
94
+ actions = [
95
+ " ecr:GetAuthorizationToken" ,
96
+ " ecr:BatchCheckLayerAvailability" ,
97
+ " ecr:GetDownloadUrlForLayer" ,
98
+ " ecr:GetRepositoryPolicy" ,
99
+ " ecr:DescribeRepositories" ,
100
+ " ecr:ListImages" ,
101
+ " ecr:DescribeImages" ,
102
+ " ecr:BatchGetImage" ,
103
+ " ecr:InitiateLayerUpload" ,
104
+ " ecr:UploadLayerPart" ,
105
+ " ecr:CompleteLayerUpload" ,
106
+ " ecr:PutImage"
107
+ ]
108
+ resources = [" *" ]
109
+ }
110
+ }
111
+
112
+ resource "aws_iam_policy" "ecr_policy" {
113
+ name = " ECRPushPolicy"
114
+ path = " /"
115
+ description = " Policy to allow push to ECR"
116
+
117
+ policy = data. aws_iam_policy_document . ecr_policy_document . json
118
+ }
119
+
120
+ resource "aws_iam_role" "github_ecr_push_role" {
121
+ name = " GithubActionsRoleECRPush"
122
+ description = " Allow GitHub actions to push to ECR"
123
+ assume_role_policy = jsonencode ({
124
+ Statement = [
125
+ {
126
+ Action = " sts:AssumeRoleWithWebIdentity"
127
+ Effect = " Allow"
128
+ Condition = {
129
+ StringEquals = {
130
+ " token.actions.githubusercontent.com:aud" = " sts.amazonaws.com"
131
+ }
132
+ StringLike = {
133
+ " token.actions.githubusercontent.com:sub" = " repo:Homebrew/ci-orchestrator:*"
134
+ }
135
+ }
136
+ Principal = {
137
+ Federated = aws_iam_openid_connect_provider.github_actions.arn
138
+ }
139
+ }
140
+ ]
141
+ Version = " 2012-10-17"
142
+ })
143
+ }
144
+
145
+ resource "aws_iam_role_policy_attachment" "github_ecr_policy_attachment" {
146
+ role = aws_iam_role. github_ecr_push_role . name
147
+ policy_arn = aws_iam_policy. ecr_policy . arn
148
+ }
0 commit comments