Skip to content

Commit 945394d

Browse files
authored
[DEV-1385] - Add IAM Role to deploy strapi (#639)
1 parent 201c98d commit 945394d

File tree

1 file changed

+87
-26
lines changed

1 file changed

+87
-26
lines changed

.infrastructure/40_iam.tf

Lines changed: 87 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
11
data "aws_caller_identity" "current" {}
22

3+
data "aws_iam_policy_document" "deploy_github" {
4+
statement {
5+
effect = "Allow"
6+
actions = ["sts:AssumeRoleWithWebIdentity"]
7+
principals {
8+
type = "Federated"
9+
identifiers = ["arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com"]
10+
}
11+
12+
condition {
13+
test = "StringLike"
14+
variable = "token.actions.githubusercontent.com:sub"
15+
values = ["repo:${var.github_repository}:*"]
16+
}
17+
18+
condition {
19+
test = "ForAllValues:StringEquals"
20+
variable = "token.actions.githubusercontent.com:iss"
21+
values = ["https://token.actions.githubusercontent.com"]
22+
}
23+
24+
condition {
25+
test = "ForAllValues:StringEquals"
26+
variable = "token.actions.githubusercontent.com:aud"
27+
values = ["sts.amazonaws.com"]
28+
}
29+
}
30+
}
31+
332
###############################################################################
4-
# Define IAM Role to use on deploy #
33+
# Define IAM Role to use on website deploy #
534
###############################################################################
635
resource "aws_iam_role" "deploy_website" {
7-
name = "GitHubActionDeployWebsite"
8-
description = "Role to assume to deploy the website"
9-
10-
11-
assume_role_policy = jsonencode({
12-
Version = "2012-10-17"
13-
Statement = [
14-
{
15-
Effect = "Allow",
16-
Principal = {
17-
"Federated" : "arn:aws:iam::${data.aws_caller_identity.current.account_id}:oidc-provider/token.actions.githubusercontent.com"
18-
},
19-
Action = "sts:AssumeRoleWithWebIdentity",
20-
Condition = {
21-
StringLike = {
22-
"token.actions.githubusercontent.com:sub" : "repo:${var.github_repository}:*"
23-
},
24-
"ForAllValues:StringEquals" = {
25-
"token.actions.githubusercontent.com:iss" : "https://token.actions.githubusercontent.com",
26-
"token.actions.githubusercontent.com:aud" : "sts.amazonaws.com"
27-
}
28-
}
29-
}
30-
]
31-
})
36+
name = "GitHubActionDeployWebsite"
37+
description = "Role to assume to deploy the website"
38+
assume_role_policy = data.aws_iam_policy_document.deploy_github.json
3239
}
3340

3441
resource "aws_iam_policy" "deploy_website" {
@@ -77,6 +84,60 @@ resource "aws_iam_role_policy_attachment" "deploy_website" {
7784
policy_arn = aws_iam_policy.deploy_website.arn
7885
}
7986

87+
###############################################################################
88+
# Define IAM Role to use on strapi deploy #
89+
###############################################################################
90+
91+
resource "aws_iam_role" "deploy_cms" {
92+
name = "GitHubActionDeployCms"
93+
description = "Role to assume to deploy the cms"
94+
assume_role_policy = data.aws_iam_policy_document.deploy_github.json
95+
}
96+
97+
resource "aws_iam_policy" "deploy_cms" {
98+
name = "DeployCms"
99+
description = "Policy to allow to deploy the cms"
100+
101+
policy = jsonencode({
102+
Version = "2012-10-17"
103+
Statement = [
104+
{
105+
Action = [
106+
"ecs:DescribeTaskDefinition",
107+
"ecs:RegisterTaskDefinition",
108+
"ecs:DescribeServices",
109+
"ecs:UpdateService",
110+
"ecr:GetAuthorizationToken",
111+
"ecr:CompleteLayerUpload",
112+
"ecr:GetAuthorizationToken",
113+
"ecr:UploadLayerPart",
114+
"ecr:InitiateLayerUpload",
115+
"ecr:BatchCheckLayerAvailability",
116+
"ecr:PutImage",
117+
"ecr:BatchGetImage"
118+
]
119+
Effect = "Allow"
120+
Resource = "*"
121+
},
122+
{
123+
Action = [
124+
"iam:PassRole"
125+
]
126+
Effect = "Allow"
127+
Resource = [
128+
module.iam_role_ecs_task_execution.iam_role_arn,
129+
module.iam_role_task_role.iam_role_arn
130+
]
131+
}
132+
]
133+
})
134+
}
135+
136+
resource "aws_iam_role_policy_attachment" "deploy_cms" {
137+
role = aws_iam_role.deploy_cms.name
138+
policy_arn = aws_iam_policy.deploy_cms.arn
139+
}
140+
80141
## IAM Role and policy ECS for CMS Strapi
81142
data "aws_iam_policy_document" "ecs_task_execution" {
82143
statement {

0 commit comments

Comments
 (0)