Skip to content

Commit cc408cb

Browse files
committed
feat: Add support for user-group sub-module
1 parent e7dc89c commit cc408cb

File tree

6 files changed

+229
-0
lines changed

6 files changed

+229
-0
lines changed

modules/README.md

Whitespace-only changes.

modules/user-group/README.md

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
# AWS ElastiCache - User Group Terraform module
2+
3+
Terraform module which creates AWS ElastiCache users & group resources.
4+
5+
[![SWUbanner](https://raw.githubusercontent.com/vshymanskyy/StandWithUkraine/main/banner2-direct.svg)](https://github.com/vshymanskyy/StandWithUkraine/blob/main/docs/README.md)
6+
7+
## Usage
8+
9+
See [`examples`](https://github.com/clowdhaus/terraform-aws-elasticache/tree/main/examples) directory for working examples to reference:
10+
11+
```hcl
12+
module "elasticache" {
13+
source = "clowdhaus/elasticache/aws//modules/user-group"
14+
15+
user_group_id = "example"
16+
users = {
17+
Moe = {
18+
access_string = "on ~* +@all"
19+
passwords = ["password123456789"]
20+
}
21+
22+
Larry = {
23+
access_string = "on ~* +@all"
24+
25+
authentication_mode = {
26+
type = "iam"
27+
}
28+
}
29+
30+
Curly = {
31+
access_string = "on ~* +@all"
32+
33+
authentication_mode = {
34+
type = "password"
35+
passwords = ["password1", "password2"]
36+
}
37+
}
38+
}
39+
40+
tags = {
41+
Terraform = "true"
42+
Environment = "dev"
43+
}
44+
}
45+
```
46+
47+
## Examples
48+
49+
Examples codified under the [`examples`](https://github.com/clowdhaus/terraform-aws-elasticache/tree/main/examples) are intended to give users references for how to use the module(s) as well as testing/validating changes to the source code of the module. If contributing to the project, please be sure to make any appropriate updates to the relevant examples to allow maintainers to test your changes and to keep the examples up to date for users. Thank you!
50+
51+
- [Complete](https://github.com/clowdhaus/terraform-aws-elasticache/tree/main/examples/complete)
52+
53+
<!-- BEGINNING OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
54+
## Requirements
55+
56+
| Name | Version |
57+
|------|---------|
58+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
59+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.23 |
60+
61+
## Providers
62+
63+
| Name | Version |
64+
|------|---------|
65+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.23 |
66+
67+
## Modules
68+
69+
No modules.
70+
71+
## Resources
72+
73+
| Name | Type |
74+
|------|------|
75+
| [aws_elasticache_user.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_user) | resource |
76+
| [aws_elasticache_user_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_user_group) | resource |
77+
| [aws_elasticache_user_group_association.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/elasticache_user_group_association) | resource |
78+
79+
## Inputs
80+
81+
| Name | Description | Type | Default | Required |
82+
|------|-------------|------|---------|:--------:|
83+
| <a name="input_create"></a> [create](#input\_create) | Determines whether resources will be created (affects all resources) | `bool` | `true` | no |
84+
| <a name="input_create_group"></a> [create\_group](#input\_create\_group) | Determines whether a user group will be created | `bool` | `true` | no |
85+
| <a name="input_engine"></a> [engine](#input\_engine) | The current supported value is `REDIS` | `string` | `"REDIS"` | no |
86+
| <a name="input_tags"></a> [tags](#input\_tags) | A map of tags to add to all resources | `map(string)` | `{}` | no |
87+
| <a name="input_user_group_id"></a> [user\_group\_id](#input\_user\_group\_id) | The ID of the user group | `string` | `""` | no |
88+
| <a name="input_users"></a> [users](#input\_users) | A map of users to create | `any` | `{}` | no |
89+
90+
## Outputs
91+
92+
| Name | Description |
93+
|------|-------------|
94+
| <a name="output_group_arn"></a> [group\_arn](#output\_group\_arn) | The ARN that identifies the user group |
95+
| <a name="output_group_id"></a> [group\_id](#output\_group\_id) | The user group identifier |
96+
| <a name="output_users"></a> [users](#output\_users) | A map of users created and their attributes |
97+
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
98+
99+
## License
100+
101+
Apache-2.0 Licensed. See [LICENSE](https://github.com/clowdhaus/terraform-aws-elasticache/blob/main/LICENSE).

modules/user-group/main.tf

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
locals {
2+
tags = merge(var.tags, { terraform-aws-modules = "elasticache" })
3+
}
4+
5+
################################################################################
6+
# Group
7+
################################################################################
8+
9+
resource "aws_elasticache_user_group" "this" {
10+
count = var.create && var.create_group ? 1 : 0
11+
12+
engine = var.engine
13+
user_group_id = var.user_group_id
14+
tags = local.tags
15+
16+
lifecycle {
17+
ignore_changes = [user_ids]
18+
}
19+
}
20+
21+
################################################################################
22+
# User(s)
23+
################################################################################
24+
25+
resource "aws_elasticache_user" "this" {
26+
for_each = { for k, v in var.users : k => v if var.create }
27+
28+
access_string = each.value.access_string
29+
30+
dynamic "authentication_mode" {
31+
for_each = lookup(each.value, "authentication_mode", []) > 0 ? [each.value.authentication_mode] : []
32+
33+
content {
34+
passwords = try(authentication_mode.value.passwords, [])
35+
type = authentication_mode.value.type
36+
}
37+
}
38+
39+
engine = each.value.engine
40+
no_password_required = try(each.value.no_password_required, null)
41+
passwords = try(each.value.passwords, [])
42+
user_id = try(each.value.user_id, each.key)
43+
user_name = try(each.value.user_name, each.key)
44+
45+
tags = merge(local.tags, try(each.value.tags, {}))
46+
}
47+
48+
resource "aws_elasticache_user_group_association" "this" {
49+
for_each = { for k, v in var.users : k => v if var.create }
50+
51+
user_group_id = var.create && var.create_group ? aws_elasticache_user_group.this[0].user_group_id : each.value.user_group_id
52+
user_id = aws_elasticache_user.this[each.key].user_id
53+
}

modules/user-group/outputs.tf

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
################################################################################
2+
# Group
3+
################################################################################
4+
5+
output "group_arn" {
6+
description = "The ARN that identifies the user group"
7+
value = try(aws_elasticache_user_group.this[0].arn, null)
8+
}
9+
10+
output "group_id" {
11+
description = "The user group identifier"
12+
value = try(aws_elasticache_user_group.this[0].id, null)
13+
}
14+
15+
################################################################################
16+
# User(s)
17+
################################################################################
18+
19+
output "users" {
20+
description = "A map of users created and their attributes"
21+
value = aws_elasticache_user.this
22+
}

modules/user-group/variables.tf

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
variable "create" {
2+
description = "Determines whether resources will be created (affects all resources)"
3+
type = bool
4+
default = true
5+
}
6+
7+
variable "tags" {
8+
description = "A map of tags to add to all resources"
9+
type = map(string)
10+
default = {}
11+
}
12+
13+
################################################################################
14+
# Group
15+
################################################################################
16+
17+
variable "create_group" {
18+
description = "Determines whether a user group will be created"
19+
type = bool
20+
default = true
21+
}
22+
23+
variable "engine" {
24+
description = "The current supported value is `REDIS`"
25+
type = string
26+
default = "REDIS"
27+
}
28+
29+
variable "user_group_id" {
30+
description = "The ID of the user group"
31+
type = string
32+
default = ""
33+
}
34+
35+
################################################################################
36+
# User(s)
37+
################################################################################
38+
39+
variable "users" {
40+
description = "A map of users to create"
41+
type = any
42+
default = {}
43+
}

modules/user-group/versions.tf

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

0 commit comments

Comments
 (0)