Skip to content

Commit b5e9346

Browse files
feat: Add module wrappers (#479)
1 parent 0236678 commit b5e9346

20 files changed

+736
-0
lines changed

wrappers/README.md

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
# Wrapper for the root module
2+
3+
The configuration in this directory contains an implementation of a single module wrapper pattern, which allows managing several copies of a module in places where using the native Terraform 0.13+ `for_each` feature is not feasible (e.g., with Terragrunt).
4+
5+
You may want to use a single Terragrunt configuration file to manage multiple resources without duplicating `terragrunt.hcl` files for each copy of the same module.
6+
7+
This wrapper does not implement any extra functionality.
8+
9+
## Usage with Terragrunt
10+
11+
`terragrunt.hcl`:
12+
13+
```hcl
14+
terraform {
15+
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers"
16+
# Alternative source:
17+
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers?ref=master"
18+
}
19+
20+
inputs = {
21+
defaults = { # Default values
22+
create = true
23+
handler = "index.lambda_handler"
24+
runtime = "python3.8"
25+
tags = {
26+
Terraform = "true"
27+
Environment = "dev"
28+
}
29+
}
30+
31+
items = {
32+
my-item = {
33+
# omitted... can be any argument supported by the module
34+
}
35+
my-second-item = {
36+
# omitted... can be any argument supported by the module
37+
}
38+
# omitted...
39+
}
40+
}
41+
```
42+
43+
## Usage with Terraform
44+
45+
```hcl
46+
module "wrapper" {
47+
source = "terraform-aws-modules/lambda/aws//wrappers"
48+
49+
defaults = { # Default values
50+
create = true
51+
handler = "index.lambda_handler"
52+
runtime = "python3.8"
53+
tags = {
54+
Terraform = "true"
55+
Environment = "dev"
56+
}
57+
}
58+
59+
items = {
60+
my-item = {
61+
# omitted... can be any argument supported by the module
62+
}
63+
my-second-item = {
64+
# omitted... can be any argument supported by the module
65+
}
66+
# omitted...
67+
}
68+
}
69+
```
70+
71+
## Example: Manage multiple Lambdas in one Terragrunt layer
72+
73+
`eu-west-1/lambdas/terragrunt.hcl`:
74+
75+
```hcl
76+
terraform {
77+
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers"
78+
# Alternative source:
79+
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers?ref=master"
80+
}
81+
82+
inputs = {
83+
defaults = {
84+
create = true
85+
handler = "index.lambda_handler"
86+
runtime = "python3.8"
87+
tags = {
88+
Terraform = "true"
89+
Environment = "dev"
90+
}
91+
}
92+
93+
items = {
94+
lambda1 = {
95+
function_name = "my-lambda1"
96+
description = "My awesome lambda function"\
97+
source_path = "../src/lambda-function1"
98+
}
99+
lambda2 = {
100+
function_name = "my-lambda2"
101+
description = "My second awesome lambda function"
102+
source_path = "../src/lambda-function2"
103+
}
104+
}
105+
}
106+
```

wrappers/alias/README.md

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
# Wrapper for module: `modules/alias`
2+
3+
The configuration in this directory contains an implementation of a single module wrapper pattern, which allows managing several copies of a module in places where using the native Terraform 0.13+ `for_each` feature is not feasible (e.g., with Terragrunt).
4+
5+
You may want to use a single Terragrunt configuration file to manage multiple resources without duplicating `terragrunt.hcl` files for each copy of the same module.
6+
7+
This wrapper does not implement any extra functionality.
8+
9+
## Usage with Terragrunt
10+
11+
`terragrunt.hcl`:
12+
13+
```hcl
14+
terraform {
15+
source = "tfr:///terraform-aws-modules/lambda/aws"
16+
# Alternative source:
17+
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/alias?ref=master"
18+
}
19+
20+
inputs = {
21+
defaults = { # Default values
22+
create = true
23+
refresh_alias = true
24+
}
25+
26+
items = {
27+
my-item = {
28+
# omitted... can be any argument supported by the module
29+
}
30+
my-second-item = {
31+
# omitted... can be any argument supported by the module
32+
}
33+
# omitted...
34+
}
35+
}
36+
```
37+
38+
## Usage with Terraform
39+
40+
```hcl
41+
module "wrapper" {
42+
source = "terraform-aws-modules/lambda/aws//wrappers/alias"
43+
44+
defaults = { # Default values
45+
create = true
46+
refresh_alias = true
47+
}
48+
49+
items = {
50+
my-item = {
51+
# omitted... can be any argument supported by the module
52+
}
53+
my-second-item = {
54+
# omitted... can be any argument supported by the module
55+
}
56+
# omitted...
57+
}
58+
}
59+
```
60+
61+
## Example: Manage multiple aliases in one Terragrunt layer
62+
63+
`eu-west-1/lambda-aliases/terragrunt.hcl`:
64+
65+
```hcl
66+
terraform {
67+
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/alias"
68+
# Alternative source:
69+
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/alias?ref=master"
70+
}
71+
72+
dependency "lambda1" {
73+
config_path = "../lambdas/lambda1"
74+
}
75+
dependency "lambda2" {
76+
config_path = "../lambdas/lambda2"
77+
}
78+
79+
inputs = {
80+
defaults = {
81+
refresh_alias = true
82+
allowed_triggers = {
83+
AnotherAPIGatewayAny = {
84+
service = "apigateway"
85+
source_arn = "arn:aws:execute-api:eu-west-1:135367859851:abcdedfgse/*/*/*"
86+
}
87+
}
88+
}
89+
90+
items = {
91+
alias1 = {
92+
name = "my-random-alias-1"
93+
function_name = dependency.lambda1.outputs.lambda_function_name
94+
function_version = dependency.lambda1.outputs.lambda_function_version
95+
}
96+
alias2 = {
97+
name = "my-random-alias-2"
98+
function_name = dependency.lambda2.outputs.lambda_function_name
99+
function_version = dependency.lambda2.outputs.lambda_function_version
100+
}
101+
}
102+
}
103+
```

wrappers/alias/main.tf

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
module "wrapper" {
2+
source = "../../modules/alias"
3+
4+
for_each = var.items
5+
6+
create = try(each.value.create, var.defaults.create, true)
7+
use_existing_alias = try(each.value.use_existing_alias, var.defaults.use_existing_alias, false)
8+
refresh_alias = try(each.value.refresh_alias, var.defaults.refresh_alias, true)
9+
create_async_event_config = try(each.value.create_async_event_config, var.defaults.create_async_event_config, false)
10+
create_version_async_event_config = try(each.value.create_version_async_event_config, var.defaults.create_version_async_event_config, true)
11+
create_qualified_alias_async_event_config = try(each.value.create_qualified_alias_async_event_config, var.defaults.create_qualified_alias_async_event_config, true)
12+
create_version_allowed_triggers = try(each.value.create_version_allowed_triggers, var.defaults.create_version_allowed_triggers, true)
13+
create_qualified_alias_allowed_triggers = try(each.value.create_qualified_alias_allowed_triggers, var.defaults.create_qualified_alias_allowed_triggers, true)
14+
name = try(each.value.name, var.defaults.name, null)
15+
description = try(each.value.description, var.defaults.description, null)
16+
function_name = try(each.value.function_name, var.defaults.function_name, null)
17+
function_version = try(each.value.function_version, var.defaults.function_version, null)
18+
routing_additional_version_weights = try(each.value.routing_additional_version_weights, var.defaults.routing_additional_version_weights, {})
19+
maximum_event_age_in_seconds = try(each.value.maximum_event_age_in_seconds, var.defaults.maximum_event_age_in_seconds, null)
20+
maximum_retry_attempts = try(each.value.maximum_retry_attempts, var.defaults.maximum_retry_attempts, null)
21+
destination_on_failure = try(each.value.destination_on_failure, var.defaults.destination_on_failure, null)
22+
destination_on_success = try(each.value.destination_on_success, var.defaults.destination_on_success, null)
23+
allowed_triggers = try(each.value.allowed_triggers, var.defaults.allowed_triggers, {})
24+
event_source_mapping = try(each.value.event_source_mapping, var.defaults.event_source_mapping, {})
25+
26+
}

wrappers/alias/outputs.tf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
output "wrapper" {
2+
description = "Map of outputs of a wrapper."
3+
value = module.wrapper
4+
# sensitive = false # No sensitive module output found
5+
}

wrappers/alias/variables.tf

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
variable "defaults" {
2+
description = "Map of default values which will be used for each item."
3+
type = any
4+
default = {}
5+
}
6+
7+
variable "items" {
8+
description = "Maps of items to create a wrapper from. Values are passed through to the module."
9+
type = any
10+
default = {}
11+
}

wrappers/alias/versions.tf

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
terraform {
2+
required_version = ">= 0.13"
3+
}

wrappers/deploy/README.md

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
# Wrapper for module: `modules/deploy`
2+
3+
The configuration in this directory contains an implementation of a single module wrapper pattern, which allows managing several copies of a module in places where using the native Terraform 0.13+ `for_each` feature is not feasible (e.g., with Terragrunt).
4+
5+
You may want to use a single Terragrunt configuration file to manage multiple resources without duplicating `terragrunt.hcl` files for each copy of the same module.
6+
7+
This wrapper does not implement any extra functionality.
8+
9+
## Usage with Terragrunt
10+
11+
`terragrunt.hcl`:
12+
13+
```hcl
14+
terraform {
15+
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/deploy"
16+
# Alternative source:
17+
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/deploy?ref=master"
18+
}
19+
20+
inputs = {
21+
defaults = { # Default values
22+
create_app = true
23+
tags = {
24+
Terraform = "true"
25+
Environment = "dev"
26+
}
27+
}
28+
29+
items = {
30+
my-item = {
31+
# omitted... can be any argument supported by the module
32+
}
33+
my-second-item = {
34+
# omitted... can be any argument supported by the module
35+
}
36+
# omitted...
37+
}
38+
}
39+
```
40+
41+
## Usage with Terraform
42+
43+
```hcl
44+
module "wrapper" {
45+
source = "terraform-aws-modules/lambda/aws//wrappers/deploy"
46+
47+
defaults = { # Default values
48+
create_app = true
49+
tags = {
50+
Terraform = "true"
51+
Environment = "dev"
52+
}
53+
}
54+
55+
items = {
56+
my-item = {
57+
# omitted... can be any argument supported by the module
58+
}
59+
my-second-item = {
60+
# omitted... can be any argument supported by the module
61+
}
62+
# omitted...
63+
}
64+
}
65+
```
66+
67+
## Example: Manage multiple deployment via AWS CodeDeploy in one Terragrunt layer
68+
69+
`eu-west-1/lambda-deploys/terragrunt.hcl`:
70+
71+
```hcl
72+
terraform {
73+
source = "tfr:///terraform-aws-modules/lambda/aws//wrappers/deploy"
74+
# Alternative source:
75+
# source = "git::[email protected]:terraform-aws-modules/terraform-aws-lambda.git//wrappers/deploy?ref=master"
76+
}
77+
78+
dependency "aliases" {
79+
config_path = "../lambdas-aliases/"
80+
}
81+
dependency "lambda1" {
82+
config_path = "../lambdas/lambda1"
83+
}
84+
dependency "lambda2" {
85+
config_path = "../lambdas/lambda2"
86+
}
87+
88+
inputs = {
89+
defaults = {
90+
create_app = true
91+
reate_deployment_group = true
92+
create_deployment = true
93+
run_deployment = true
94+
wait_deployment_completion = true
95+
96+
triggers = {
97+
start = {
98+
events = ["DeploymentStart"]
99+
name = "DeploymentStart"
100+
target_arn = "arn:aws:sns:eu-west-1:135367859851:sns1"
101+
}
102+
success = {
103+
events = ["DeploymentSuccess"]
104+
name = "DeploymentSuccess"
105+
target_arn = "arn:aws:sns:eu-west-1:135367859851:sns2"
106+
}
107+
}
108+
109+
tags = {
110+
Terraform = "true"
111+
Environment = "dev"
112+
}
113+
}
114+
115+
items = {
116+
deploy1 = {
117+
app_name = "my-random-app-1"
118+
deployment_group_name = "something1"
119+
120+
alias_name = dependency.aliases.outputs.wrapper.alias1.lambda_alias_name
121+
function_name = dependency.lambda1.outputs.lambda_function_name
122+
target_version = dependency.lambda1.outputs.lambda_function_version
123+
}
124+
deploy2 = {
125+
app_name = "my-random-app-2"
126+
deployment_group_name = "something2"
127+
128+
alias_name = dependency.aliases.outputs.wrapper.alias2.lambda_alias_name
129+
function_name = dependency.lambda2.outputs.lambda_function_name
130+
target_version = dependency.lambda2.outputs.lambda_function_version
131+
}
132+
}
133+
}
134+
```

0 commit comments

Comments
 (0)