Skip to content

Commit 09d93a4

Browse files
committed
Migration of learning repository files
1 parent 016d763 commit 09d93a4

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

.gitignore

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# Local .terraform directories
2+
**/.terraform/*
3+
4+
# .tfstate files
5+
*.tfstate
6+
*.tfstate.*
7+
8+
# Crash log files
9+
crash.log
10+
# Terraform Lock HCL
11+
.terraform.lock.hcl
12+
# Ignore any .tfvars files that are generated automatically for each Terraform run. Most
13+
# .tfvars files are managed as part of configuration and so should be included in
14+
# version control.
15+
#
16+
# example.tfvars
17+
18+
# Ignore override files as they are usually used to override resources locally and so
19+
# are not checked in
20+
override.tf
21+
override.tf.json
22+
*_override.tf
23+
*_override.tf.json
24+
.terraform-docs.yml
25+
# Include override files you do wish to add to version control using negated pattern
26+
#
27+
# !example_override.tf
28+
29+
# Include tfplan files to ignore the plan output of command: terraform plan -out=tfplan
30+
# example: *tfplan*

README.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# Introduction
2+
- This Terraform Stack create **IAM users** and optionally **IAM groups** dynamically in AWS cloud.
3+
## Permissions
4+
- Crate a policy with content below and attach in EC2 IAM Role or IAM User. These permissions are required to works correctly!
5+
6+
```json
7+
{
8+
"Version": "2012-10-17",
9+
"Statement": [
10+
{
11+
"Sid": "",
12+
"Effect": "Allow",
13+
"Action": [
14+
"iam:CreateGroup",
15+
"iam:AddUserToGroup",
16+
"iam:RemoveUserFromGroup",
17+
"iam:DeleteGroup",
18+
"iam:ListGroupsForUser",
19+
"iam:UpdateGroup",
20+
"iam:DeleteUser",
21+
"iam:GetUser",
22+
"iam:CreateUser",
23+
"iam:GetGroup"
24+
],
25+
"Resource": "*"
26+
}
27+
]
28+
}
29+
```
30+
31+
<!-- BEGIN_TF_DOCS -->
32+
## Requirements
33+
34+
No requirements.
35+
36+
## Providers
37+
38+
| Name | Version |
39+
|------|---------|
40+
| <a name="provider_aws"></a> [aws](#provider\_aws) | n/a |
41+
42+
## Resources
43+
44+
| Name | Type |
45+
|------|------|
46+
| [aws_iam_group.groups](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_group) | resource |
47+
| [aws_iam_user.users](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user) | resource |
48+
| [aws_iam_user_group_membership.user_to_groups](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_user_group_membership) | resource |
49+
50+
## Inputs
51+
52+
| Name | Description | Type | Default | Required |
53+
|------|-------------|------|---------|:--------:|
54+
| <a name="input_access_key"></a> [access\_key](#input\_access\_key) | AWS Access Key | `string` | n/a | yes |
55+
| <a name="input_region"></a> [region](#input\_region) | Region where the resources will be created. | `string` | n/a | yes |
56+
| <a name="input_secret_key"></a> [secret\_key](#input\_secret\_key) | AWS Secret Access Key | `string` | n/a | yes |
57+
| <a name="input_create_groups"></a> [create\_groups](#input\_create\_groups) | Define if Terraform will create new\_groups based on variable groups. | `bool` | `true` | no |
58+
| <a name="input_groups"></a> [groups](#input\_groups) | List of group names for Terraform create, case create\_groups variable be true | `list(string)` | `[]` | no |
59+
| <a name="input_users"></a> [users](#input\_users) | Map for Terraform create users. | `map(any)` | <pre>{<br> "user1": {<br> "groups": [<br> "ADM",<br> "developers"<br> ],<br> "name": "test",<br> "path": "/"<br> }<br>}</pre> | no |
60+
<!-- END_TF_DOCS -->
61+
62+
### This Terraform documentation was generated by [terraform-docs](https://github.com/terraform-docs/terraform-docs).
63+
64+
## Getting started
65+
```sh
66+
terraform init
67+
68+
terraform plan
69+
70+
terraform apply
71+
```

examples/with_new_groups.tf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
module "with_new_groups" {
2+
3+
source = "../../.."
4+
region = "us-east-1"
5+
access_key = "<MY_ACCESS_KEY>"
6+
secret_key = "<MY_SECRET_KEY>"
7+
groups = ["developers"]
8+
create_groups = true
9+
users = {
10+
"user1": {
11+
name: "test_user"
12+
groups: ["ADM", "developers"]
13+
path: "/"
14+
}
15+
}
16+
17+
}

examples/without_new_groups.tf

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
2+
module "without_new_groups" {
3+
4+
source = "../../.."
5+
region = "us-east-1"
6+
access_key = "<MY_ACCESS_KEY>"
7+
secret_key = "<MY_SECRET_KEY>"
8+
create_groups = false
9+
users = {
10+
"user1": {
11+
name: "test_user"
12+
groups: ["Marketing","SysAdmins"]
13+
path: "/"
14+
}
15+
}
16+
17+
}

main.tf

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
provider "aws" {
2+
region = var.region
3+
access_key = var.access_key
4+
secret_key = var.secret_key
5+
}
6+
7+
8+
resource "aws_iam_group" "groups" {
9+
for_each = var.create_groups ? toset(var.groups) : toset([])
10+
name = each.value
11+
path = "/"
12+
}
13+
14+
resource "aws_iam_user" "users" {
15+
for_each = var.users
16+
name = each.value.name
17+
path = try(each.value.path, "/")
18+
19+
}
20+
21+
resource "aws_iam_user_group_membership" "user_to_groups" {
22+
for_each = var.users
23+
user = each.value.name
24+
groups = each.value.groups
25+
depends_on = [aws_iam_user.users, aws_iam_group.groups]
26+
}

variables.tf

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
variable "region" {
2+
type = string
3+
description = "Region where the resources will be created."
4+
}
5+
6+
variable "groups" {
7+
type = list(string)
8+
default = []
9+
description = "List of group names for Terraform create, case create_groups variable be true"
10+
}
11+
12+
variable "users" {
13+
type = map(any)
14+
default = {
15+
"user1" : {
16+
name : "test"
17+
groups : ["ADM", "developers"]
18+
path : "/"
19+
}
20+
}
21+
description = "Map for Terraform create users."
22+
23+
}
24+
25+
variable "create_groups" {
26+
type = bool
27+
default = true
28+
description = "Define if Terraform will create new_groups based on variable groups."
29+
}
30+
31+
variable "access_key" {
32+
type = string
33+
description = "AWS Access Key"
34+
}
35+
variable "secret_key" {
36+
type = string
37+
description = "AWS Secret Access Key"
38+
}

0 commit comments

Comments
 (0)