Skip to content

Commit c9ebbf8

Browse files
committedAug 26, 2024··
fix: modify the assume role conditional for environment
1 parent bb4fb1b commit c9ebbf8

File tree

6 files changed

+51
-15
lines changed

6 files changed

+51
-15
lines changed
 

‎infra/aws-github-oidc/main.tf

+15-7
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,6 @@ data "tls_certificate" "oidc_thumbprint" {
22
url = "https://token.actions.githubusercontent.com"
33
}
44

5-
resource "aws_iam_openid_connect_provider" "github_actions" {
6-
url = "https://token.actions.githubusercontent.com"
7-
client_id_list = ["sts.amazonaws.com"]
8-
thumbprint_list = [data.tls_certificate.oidc_thumbprint.certificates[0].sha1_fingerprint]
9-
}
10-
115
data "aws_iam_policy_document" "assume_role" {
126
statement {
137
actions = [
@@ -23,7 +17,13 @@ data "aws_iam_policy_document" "assume_role" {
2317
condition {
2418
test = "StringEquals"
2519
variable = "token.actions.githubusercontent.com:sub"
26-
values = ["repo:developer-friendly/aws-lambda-opentofu-github-actions:ref:refs/heads/main"]
20+
values = ["repo:developer-friendly/aws-lambda-opentofu-github-actions:environment:${var.environment_name}"]
21+
}
22+
23+
condition {
24+
test = "StringEquals"
25+
variable = "token.actions.githubusercontent.com:aud"
26+
values = ["sts.amazonaws.com"]
2727
}
2828
}
2929
}
@@ -40,6 +40,14 @@ data "aws_iam_policy_document" "lambda_policy" {
4040
}
4141
}
4242

43+
resource "aws_iam_openid_connect_provider" "github_actions" {
44+
url = "https://token.actions.githubusercontent.com"
45+
client_id_list = ["sts.amazonaws.com"]
46+
thumbprint_list = [
47+
data.tls_certificate.oidc_thumbprint.certificates[0].sha1_fingerprint,
48+
]
49+
}
50+
4351
resource "aws_iam_role" "this" {
4452
name = "github-actions"
4553
assume_role_policy = data.aws_iam_policy_document.assume_role.json

‎infra/aws-github-oidc/outputs.tf

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
output "role_arn" {
22
value = aws_iam_role.this.arn
33
}
4+
5+
output "environment_name" {
6+
value = var.environment_name
7+
}

‎infra/aws-github-oidc/variables.tf

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
variable "environment_name" {
2+
type = string
3+
default = "prod"
4+
}

‎infra/repository/main.tf

+21-7
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
1-
data "github_user" "current" {
2-
username = ""
1+
data "aws_region" "current" {}
2+
3+
resource "github_branch_protection" "this" {
4+
repository_id = "aws-lambda-opentofu-github-actions"
5+
6+
pattern = "main"
7+
enforce_admins = true
8+
allows_deletions = false
9+
10+
force_push_bypassers = [
11+
"/meysam81",
12+
]
313
}
414

515
resource "github_repository_environment" "this" {
6-
environment = "prod"
16+
environment = var.environment_name
717
repository = "aws-lambda-opentofu-github-actions"
818

9-
reviewers {
10-
users = [data.github_user.current.id]
11-
}
12-
1319
prevent_self_review = false
1420

1521
deployment_branch_policy {
@@ -25,3 +31,11 @@ resource "github_actions_environment_secret" "this" {
2531
secret_name = "AWS_IAM_ROLE"
2632
plaintext_value = var.aws_iam_role
2733
}
34+
35+
resource "github_actions_environment_variable" "this" {
36+
repository = "aws-lambda-opentofu-github-actions"
37+
environment = var.environment_name
38+
39+
variable_name = "AWS_REGION"
40+
value = data.aws_region.current.name
41+
}

‎infra/repository/terragrunt.hcl

+2-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ include "root" {
33
}
44

55
inputs = {
6-
aws_iam_role = dependency.github_oidc.outputs.role_arn
6+
aws_iam_role = dependency.github_oidc.outputs.role_arn
7+
environment_name = dependency.github_oidc.outputs.environment_name
78
}
89

910
dependency "github_oidc" {

‎infra/repository/variables.tf

+5
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,8 @@ variable "aws_iam_role" {
22
type = string
33
description = "The ARN of the IAM role to assume"
44
}
5+
6+
variable "environment_name" {
7+
type = string
8+
nullable = false
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.