Skip to content

Commit 421915e

Browse files
Initializing module for creating role to be assumed by github action
0 parents  commit 421915e

File tree

10 files changed

+166
-0
lines changed

10 files changed

+166
-0
lines changed

.github/workflows/documentation.yml

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
name: Generate terraform docs
2+
3+
on:
4+
- pull_request
5+
6+
jobs:
7+
docs:
8+
runs-on: ubuntu-latest
9+
steps:
10+
- uses: actions/checkout@v4
11+
with:
12+
ref: ${{ github.event.pull_request.head.ref }}
13+
14+
- name: Render terraform docs and push changes back to PR
15+
uses: terraform-docs/gh-actions@main
16+
with:
17+
working-dir: .
18+
output-file: README.md
19+
output-method: inject
20+
git-push: "true"

.gitignore

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
crash.*.log
11+
12+
# Exclude all .tfvars files, which are likely to contain sensitive data, such as
13+
# password, private keys, and other secrets. These should not be part of version
14+
# control as they are data points which are potentially sensitive and subject
15+
# to change depending on the environment.
16+
*.tfvars
17+
*.tfvars.json
18+
19+
# Ignore override files as they are usually used to override resources locally and so
20+
# are not checked in
21+
override.tf
22+
override.tf.json
23+
*_override.tf
24+
*_override.tf.json
25+
26+
# Include override files you do wish to add to version control using negated pattern
27+
# !example_override.tf
28+
29+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
30+
# example: *tfplan*
31+
32+
# Ignore CLI configuration files
33+
.terraformrc
34+
terraform.rc

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# Terraform AWS IAM Role for GitHub Actions
2+
3+
This repository provides a Terraform module to create an IAM role with the necessary permissions and trust policies for GitHub Actions to manage AWS Organizations resources.
4+
5+
## Usage
6+
7+
### Module
8+
9+
```hcl
10+
module "github_actions_iam_role" {
11+
source = "github.com/infraspecdev/terraform-aws-github-actions-iam-role?ref=main"
12+
13+
aws_account_id = "YOUR_AWS_ACCOUNT_ID"
14+
github_username = "YOUR_GITHUB_USERNAME"
15+
repository_names = "YOUR_REPO_NAMES"
16+
role_name = "GitHubActionsRole"
17+
}
18+
```
19+
20+
## Variables
21+
22+
- **aws_account_id**: The AWS Account ID where the IAM role will be created.
23+
- **github_username**: The GitHub username or organization name.
24+
- **repository_names**: The list of GitHub repository names.
25+
- **role_name**: (Optional) The name of the IAM role. Default is `GitHubActionsRole`.
26+
27+
## Outputs
28+
29+
- **role_arn**: The ARN of the IAM role.

data.tf

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
data "aws_iam_policy_document" "assume_role_policy" {
2+
statement {
3+
effect = "Allow"
4+
5+
principals {
6+
type = "Federated"
7+
identifiers = ["arn:aws:iam::${var.aws_account_id}:oidc-provider/token.actions.githubusercontent.com"]
8+
}
9+
10+
actions = ["sts:AssumeRoleWithWebIdentity"]
11+
12+
condition {
13+
test = "StringEquals"
14+
variable = "token.actions.githubusercontent.com:sub"
15+
values = local.repository_ref_list
16+
}
17+
18+
condition {
19+
test = "StringEquals"
20+
variable = "token.actions.githubusercontent.com:aud"
21+
values = ["sts.amazonaws.com"]
22+
}
23+
}
24+
}

locals.tf

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
locals {
2+
repository_ref_list = flatten([
3+
for repo in var.repository_names :
4+
"repo:${var.github_username}/${repo}:*"
5+
6+
])
7+
}

main.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
resource "aws_iam_role" "github_actions_role" {
2+
name = var.role_name
3+
assume_role_policy = data.aws_iam_policy_document.assume_role_policy.json
4+
}
5+
6+
resource "aws_iam_role_policy_attachment" "attach_admin_policy" {
7+
role = aws_iam_role.github_actions_role.name
8+
policy_arn = "arn:aws:iam::aws:policy/AdministratorAccess"
9+
}

outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
output "role_arn" {
2+
description = "The ARN of the IAM role"
3+
value = aws_iam_role.github_actions_role.arn
4+
}

provider.tf

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
provider "aws" {
2+
region = "ap-south-1"
3+
4+
default_tags {
5+
tags = {
6+
ManagedBy = "Terraform"
7+
}
8+
}
9+
}

variables.tf

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
variable "aws_account_id" {
2+
description = "The AWS Account ID"
3+
type = string
4+
}
5+
6+
variable "github_username" {
7+
description = "The name of the GitHub user or organization that owns the repository(ies) the role will use."
8+
type = string
9+
}
10+
11+
variable "repository_names" {
12+
description = "List of names of the GitHub repository that will be allowed to assume the role."
13+
type = list(string)
14+
}
15+
16+
variable "role_name" {
17+
description = "The name of the IAM Role to be created."
18+
type = string
19+
default = "GitHubActionsRole"
20+
}

versions.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
terraform {
2+
required_providers {
3+
aws = {
4+
source = "hashicorp/aws"
5+
version = "~> 5.51.0"
6+
}
7+
}
8+
9+
required_version = "~> 1.8.4"
10+
}

0 commit comments

Comments
 (0)