Skip to content

adding mvp terraform config for aws iam resources #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Feb 8, 2024
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
Terraform directory structure

- 📁 [terraform](https://github.com/hackforla/ops-security/tree/cb/example/terraform)
- 📁 [aws-custom-policies](https://github.com/hackforla/ops-security/tree/cb/example/terraform/aws-custom-policies) - JSON configurations for customer-managed policies (AWS-managed policies are referenced by ARN and not needed here)
- 📁 [existing-policies](https://github.com/hackforla/ops-security/tree/cb/example/terraform/aws-custom-policies/existing-policies) - a few of our current policy configurations for reference
- 📁 [modules](https://github.com/hackforla/ops-security/tree/cb/example/terraform/modules) - reusable Terraform configurations
- 📄 [aws-custom-policies.tf](https://github.com/hackforla/ops-security/tree/cb/example/terraform/modules/aws-groups) - maintain custom policies here
- 📄 [aws-groups.tf](https://github.com/hackforla/ops-security/tree/cb/example/terraform/modules/aws-groups) - maintain groups here
- 📄 [aws-users.tf](https://github.com/hackforla/ops-security/tree/cb/example/terraform/modules/aws-users) - maintain users here

---
# Project title and description

Include a project description that explains **what** your project is and **why** it exists. Aim for no more than 3-5 concise sentences. For example, you might say:
Expand Down
13 changes: 13 additions & 0 deletions terraform/aws-custom-policies.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
module "aws_iam_policies" {
source = "./modules/policies"
policies = {
"ManageAccessKeys" = {
description = "Policy for creating, listing, and updating Access Keys"
filename = "manage-access-keys-policy.json"
},
"FullAccessPolicy" = {
description = "Full access to specific resources"
filename = "full-access-policy.json"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ListUsersForConsole",
"Effect": "Allow",
"Action": "iam:ListUsers",
"Resource": "arn:aws:iam::*:*"
},
{
"Sid": "ViewAndUpdateAccessKeys",
"Effect": "Allow",
"Action": [
"iam:UpdateAccessKey",
"iam:CreateAccessKey",
"iam:ListAccessKeys"
],
"Resource": "arn:aws:iam::*:user/*"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowViewAccountInfo",
"Effect": "Allow",
"Action": [
"iam:GetAccountPasswordPolicy",
"iam:GetAccountSummary"
],
"Resource": "*"
},
{
"Sid": "AllowManageOwnPasswords",
"Effect": "Allow",
"Action": [
"iam:ChangePassword",
"iam:GetUser"
],
"Resource": "arn:aws:iam::*:user/${aws:username}"
},
{
"Sid": "AllowManageOwnAccessKeys",
"Effect": "Allow",
"Action": [
"iam:CreateAccessKey",
"iam:DeleteAccessKey",
"iam:ListAccessKeys",
"iam:UpdateAccessKey"
],
"Resource": "arn:aws:iam::*:user/${aws:username}"
},
{
"Sid": "AllowManageOwnSSHPublicKeys",
"Effect": "Allow",
"Action": [
"iam:DeleteSSHPublicKey",
"iam:GetSSHPublicKey",
"iam:ListSSHPublicKeys",
"iam:UpdateSSHPublicKey",
"iam:UploadSSHPublicKey"
],
"Resource": "arn:aws:iam::*:user/${aws:username}"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"elasticloadbalancing:DescribeTargetGroupAttributes",
"elasticloadbalancing:DescribeTags",
"s3:ListAllMyBuckets",
"ecr:GetRegistryScanningConfiguration"
],
"Resource": "*"
},
{
"Sid": "RDS",
"Effect": "Allow",
"Action": [
"rds:DescribeDBInstances",
"rds:DescribeDBClusters",
"rds:DescribeGlobalClusters",
"rds:DescribeDBInstances"
],
"Resource": "*"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowViewAccountInfo",
"Effect": "Allow",
"Action": [
"iam:GetAccountPasswordPolicy",
"iam:GetAccountSummary"
],
"Resource": "*"
},
{
"Sid": "AllowManageOwnPasswords",
"Effect": "Allow",
"Action": [
"iam:ChangePassword",
"iam:GetUser"
],
"Resource": "arn:aws:iam::*:user/${aws:username}"
},
{
"Sid": "AllowManageOwnAccessKeys",
"Effect": "Allow",
"Action": [
"iam:CreateAccessKey",
"iam:DeleteAccessKey",
"iam:ListAccessKeys",
"iam:UpdateAccessKey"
],
"Resource": "arn:aws:iam::*:user/${aws:username}"
},
{
"Sid": "AllowManageOwnSSHPublicKeys",
"Effect": "Allow",
"Action": [
"iam:DeleteSSHPublicKey",
"iam:GetSSHPublicKey",
"iam:ListSSHPublicKeys",
"iam:UpdateSSHPublicKey",
"iam:UploadSSHPublicKey"
],
"Resource": "arn:aws:iam::*:user/${aws:username}"
}
]
}
27 changes: 27 additions & 0 deletions terraform/aws-custom-policies/level-2-project-admin-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "",
"Action": [
"",
"",
"",
""
],
"Resource": ""
},
{
"Sid": "",
"Effect": "",
"Action": [
"",
"",
"",
""
],
"Resource": ""
}
]
}
27 changes: 27 additions & 0 deletions terraform/aws-custom-policies/level-3-ops-mentor-policy.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "",
"Action": [
"",
"",
"",
""
],
"Resource": ""
},
{
"Sid": "",
"Effect": "",
"Action": [
"",
"",
"",
""
],
"Resource": ""
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "",
"Effect": "",
"Action": [
"",
"",
"",
""
],
"Resource": ""
},
{
"Sid": "",
"Effect": "",
"Action": [
"",
"",
"",
""
],
"Resource": ""
}
]
}
42 changes: 42 additions & 0 deletions terraform/aws-groups.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
// Create groups and attach policies
module "iam_read_only_group" {
source = "./modules/groups"

group_name = "read-only-group"
policy_arn = "arn:aws:iam::aws:policy/ReadOnlyAccess"
}

module "iam_project_admin_group" {
source = "./modules/groups"

group_name = "project-admin-group"
policy_arn = [ # here we can pass a list of policies that are aws managed or customer managed
"arn:aws:iam::aws:policy/SomeAWSPolicy",
module.iam_policies.policy_arns["ManageAccessKeys"]
]
}

module "iam_ops_mentor_group" {
source = "./modules/groups"

group_name = "project-admin-group"
policy_arn = [ # here we can pass a list of policies that are aws managed or customer managed
"arn:aws:iam::aws:policy/SomeAWSPolicy",
"arn:aws:iam::aws:policy/SomeAWSPolicy",
"arn:aws:iam::aws:policy/SomeAWSPolicy",
"arn:aws:iam::aws:policy/SomeAWSPolicy",
]
}

// Assign users to groups
resource "aws_iam_group_membership" "project_admin_group_membership" {
name = "project_admin_group_membership" # A unique name for the group membership

users = [
module.iam_user_gwenstacy.user_name,
module.iam_user_miles_morales.user_name,
"chelseybeck"
]

group = module.iam_project_admin_group.group_name
}
21 changes: 21 additions & 0 deletions terraform/aws-users.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module "iam_user_gwenstacy" {
source = "./modules/users"

user_name = "gwenstacy"
user_tags = {
"Environment" = "Development"
"Project" = "spiderverse"
}
pgp_key = "user_provided_public_key_here"
}

module "iam_user_milesmorales" {
source = "./modules/users"

user_name = "miles_morales"
user_tags = {
"Environment" = "Production"
"Project" = "spiderverse"
}
pgp_key = "user_provided_public_key_here"
}
9 changes: 9 additions & 0 deletions terraform/backend.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
terraform {
backend "s3" {
bucket = "my-terraform-state-bucket" # Replace with S3 bucket name
key = "path/to/terraform.tfstate" # Path to the state file within the bucket
region = "us-west-2" # AWS region of the S3 bucket
dynamodb_table = "my-terraform-state-lock" # DynamoDB table name for state locking
encrypt = true
}
}
13 changes: 13 additions & 0 deletions terraform/modules/aws-groups/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# groups/main.tf

resource "aws_iam_group" "group" {
name = var.group_name
path = var.group_path
}

# Attaching policies to the group
resource "aws_iam_group_policy_attachment" "group_policy_attachment" {
for_each = toset(var.policy_arns)
group = aws_iam_group.group.name
policy_arn = each.value
}
11 changes: 11 additions & 0 deletions terraform/modules/aws-groups/outputs.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# groups/outputs.tf

output "group_name" {
value = aws_iam_group.group.name
description = "The name of the IAM group"
}

output "group_arn" {
value = aws_iam_group.group.arn
description = "The ARN of the IAM group"
}
22 changes: 22 additions & 0 deletions terraform/modules/aws-groups/variables.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# groups/variables.tf

variable "group_name" {
description = "The name of the IAM group"
type = string
}

variable "group_path" {
description = "Path in which to create the group"
type = string
default = "/"
}

variable "policy_arn" {
description = "The ARN of the policy to attach to the group"
type = string
}

variable "policy_arns" {
description = "List of policy ARNs to attach to the group"
type = list(string)
}
9 changes: 9 additions & 0 deletions terraform/modules/aws-policies/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
# policies/main.tf

resource "aws_iam_policy" "custom_policy" {
for_each = var.policies

name = each.key
description = each.value["description"]
policy = file("${path.module}/policies-json/${each.value["filename"]}")
}
Loading