Skip to content

Commit ff5f109

Browse files
committed
feat(function): add new module
1 parent 7df7c86 commit ff5f109

File tree

9 files changed

+388
-1
lines changed

9 files changed

+388
-1
lines changed

.github/labeler.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@
99
- any-glob-to-any-file:
1010
- modules/cache-policy/**/*
1111

12+
":floppy_disk: function":
13+
- changed-files:
14+
- any-glob-to-any-file:
15+
- modules/function/**/*
16+
1217
":floppy_disk: key-value-store":
1318
- changed-files:
1419
- any-glob-to-any-file:

.github/labels.yaml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
- color: "fbca04"
66
description: "This issue or pull request is related to distribution module."
77
name: ":floppy_disk: distribution"
8+
- color: "fbca04"
9+
description: "This issue or pull request is related to function module."
10+
name: ":floppy_disk: function"
811
- color: "fbca04"
912
description: "This issue or pull request is related to key-value-store module."
1013
name: ":floppy_disk: key-value-store"

README.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ Terraform module which creates CloudFront related resources on AWS.
88

99
- [cache-policy](./modules/cache-policy)
1010
- [distribution](./modules/distribution)
11+
- [function](./modules/function)
1112
- [key-value-store](./modules/key-value-store)
1213
- [origin-access-control](./modules/origin-access-control)
1314
- [origin-request-policy](./modules/origin-request-policy)
@@ -21,6 +22,8 @@ Terraform Modules from [this package](https://github.com/tedilabs/terraform-aws-
2122

2223
- **AWS CloudFront**
2324
- Distribution
25+
- Connection Function
26+
- Function
2427
- Key-value Store
2528
- Real-time Log Configuration (Comming soon!)
2629
- Origins
@@ -52,4 +55,4 @@ Like this project? Follow the repository on [GitHub](https://github.com/tedilabs
5255

5356
Provided under the terms of the [Apache License](LICENSE).
5457

55-
Copyright © 2022-2026, [Byungjin Park](https://www.posquit0.com).
58+
Copyright © 2022-2026, [Byungjin Park](https://www.posquit0.com).

modules/function/README.md

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
# function
2+
3+
This module creates following resources.
4+
5+
- `aws_cloudfront_function` (optional)
6+
- `aws_cloudfront_connection_function` (optional)
7+
8+
<!-- BEGIN_TF_DOCS -->
9+
## Requirements
10+
11+
| Name | Version |
12+
|------|---------|
13+
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.12 |
14+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 6.35 |
15+
16+
## Providers
17+
18+
| Name | Version |
19+
|------|---------|
20+
| <a name="provider_aws"></a> [aws](#provider\_aws) | 6.35.0 |
21+
22+
## Modules
23+
24+
| Name | Source | Version |
25+
|------|--------|---------|
26+
| <a name="module_resource_group"></a> [resource\_group](#module\_resource\_group) | tedilabs/misc/aws//modules/resource-group | ~> 0.12.0 |
27+
28+
## Resources
29+
30+
| Name | Type |
31+
|------|------|
32+
| [aws_cloudfront_connection_function.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_connection_function) | resource |
33+
| [aws_cloudfront_function.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudfront_function) | resource |
34+
35+
## Inputs
36+
37+
| Name | Description | Type | Default | Required |
38+
|------|-------------|------|---------|:--------:|
39+
| <a name="input_code"></a> [code](#input\_code) | (Required) The source code of the function. Maximum length is 40960 characters for CONNECTION functions. | `string` | n/a | yes |
40+
| <a name="input_name"></a> [name](#input\_name) | (Required) A unique name for the CloudFront Function. | `string` | n/a | yes |
41+
| <a name="input_runtime"></a> [runtime](#input\_runtime) | (Required) The identifier of the function's runtime. Valid values are `cloudfront-js-1.0` and `cloudfront-js-2.0`. | `string` | n/a | yes |
42+
| <a name="input_description"></a> [description](#input\_description) | (Optional) A comment to describe the CloudFront Function. Defaults to `Managed by Terraform.`. | `string` | `"Managed by Terraform."` | no |
43+
| <a name="input_key_value_store"></a> [key\_value\_store](#input\_key\_value\_store) | (Optional) The ARN of CloudFront Key Value Store to associate with the function. AWS limits associations to one key value store per function. | `string` | `null` | no |
44+
| <a name="input_module_tags_enabled"></a> [module\_tags\_enabled](#input\_module\_tags\_enabled) | (Optional) Whether to create AWS Resource Tags for the module informations. | `bool` | `true` | no |
45+
| <a name="input_publish"></a> [publish](#input\_publish) | (Optional) Whether to publish the function to the LIVE stage after creation or update. Defaults to `true`. | `bool` | `true` | no |
46+
| <a name="input_resource_group"></a> [resource\_group](#input\_resource\_group) | (Optional) A configurations of Resource Group for this module. `resource_group` as defined below.<br/> (Optional) `enabled` - Whether to create Resource Group to find and group AWS resources which are created by this module. Defaults to `true`.<br/> (Optional) `name` - The name of Resource Group. A Resource Group name can have a maximum of 127 characters, including letters, numbers, hyphens, dots, and underscores. The name cannot start with `AWS` or `aws`. If not provided, a name will be generated using the module name and instance name.<br/> (Optional) `description` - The description of Resource Group. Defaults to `Managed by Terraform.`. | <pre>object({<br/> enabled = optional(bool, true)<br/> name = optional(string, "")<br/> description = optional(string, "Managed by Terraform.")<br/> })</pre> | `{}` | no |
47+
| <a name="input_tags"></a> [tags](#input\_tags) | (Optional) A map of tags to add to all resources. | `map(string)` | `{}` | no |
48+
| <a name="input_type"></a> [type](#input\_type) | (Optional) The type of CloudFront Function to create. Valid values are `GENERAL` and `CONNECTION`. Defaults to `GENERAL`. | `string` | `"GENERAL"` | no |
49+
50+
## Outputs
51+
52+
| Name | Description |
53+
|------|-------------|
54+
| <a name="output_arn"></a> [arn](#output\_arn) | The ARN of the CloudFront Function. |
55+
| <a name="output_description"></a> [description](#output\_description) | The comment describing the CloudFront function. |
56+
| <a name="output_etag"></a> [etag](#output\_etag) | The ETag hash of the function. This is the value for the DEVELOPMENT stage of the function. |
57+
| <a name="output_id"></a> [id](#output\_id) | The ID of the CloudFront Function. |
58+
| <a name="output_key_value_store"></a> [key\_value\_store](#output\_key\_value\_store) | The ARN of the CloudFront Key Value Store associated with the function, if any. |
59+
| <a name="output_live_stage_etag"></a> [live\_stage\_etag](#output\_live\_stage\_etag) | The ETag hash of the LIVE stage of the function. Will be empty if the function has not been published. |
60+
| <a name="output_name"></a> [name](#output\_name) | The name of the CloudFront Function. |
61+
| <a name="output_resource_group"></a> [resource\_group](#output\_resource\_group) | The resource group created to manage resources in this module. |
62+
| <a name="output_runtime"></a> [runtime](#output\_runtime) | The runtime environment for the function. |
63+
| <a name="output_status"></a> [status](#output\_status) | The status of the function. Can be UNPUBLISHED, UNASSOCIATED or ASSOCIATED. |
64+
| <a name="output_type"></a> [type](#output\_type) | The type of the CloudFront Function. |
65+
<!-- END_TF_DOCS -->

modules/function/main.tf

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
locals {
2+
metadata = {
3+
package = "terraform-aws-cloudfront"
4+
version = trimspace(file("${path.module}/../../VERSION"))
5+
module = basename(path.module)
6+
name = "${var.type}/${var.name}"
7+
}
8+
module_tags = var.module_tags_enabled ? {
9+
"module.terraform.io/package" = local.metadata.package
10+
"module.terraform.io/version" = local.metadata.version
11+
"module.terraform.io/name" = local.metadata.module
12+
"module.terraform.io/full-name" = "${local.metadata.package}/${local.metadata.module}"
13+
"module.terraform.io/instance" = local.metadata.name
14+
} : {}
15+
}
16+
17+
18+
###################################################
19+
# CloudFront Function
20+
###################################################
21+
22+
resource "aws_cloudfront_function" "this" {
23+
count = var.type == "GENERAL" ? 1 : 0
24+
25+
name = var.name
26+
comment = var.description
27+
publish = var.publish
28+
29+
runtime = var.runtime
30+
code = var.code
31+
32+
key_value_store_associations = (var.key_value_store != null
33+
? [var.key_value_store]
34+
: null
35+
)
36+
}
37+
38+
39+
###################################################
40+
# CloudFront Connection Function (CONNECTION)
41+
###################################################
42+
43+
resource "aws_cloudfront_connection_function" "this" {
44+
count = var.type == "CONNECTION" ? 1 : 0
45+
46+
name = var.name
47+
publish = var.publish
48+
49+
connection_function_code = var.code
50+
51+
connection_function_config {
52+
comment = var.description
53+
54+
runtime = var.runtime
55+
56+
dynamic "key_value_store_association" {
57+
for_each = (var.key_value_store != null) ? ["go"] : []
58+
59+
content {
60+
key_value_store_arn = var.key_value_store
61+
}
62+
}
63+
}
64+
65+
tags = merge(
66+
{
67+
"Name" = local.metadata.name
68+
},
69+
local.module_tags,
70+
var.tags,
71+
)
72+
}

modules/function/outputs.tf

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
output "id" {
2+
description = "The ID of the CloudFront Function."
3+
value = {
4+
"GENERAL" = one(aws_cloudfront_function.this[*].name)
5+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].id)
6+
}[var.type]
7+
}
8+
9+
output "arn" {
10+
description = "The ARN of the CloudFront Function."
11+
value = {
12+
"GENERAL" = one(aws_cloudfront_function.this[*].arn)
13+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].connection_function_arn)
14+
}[var.type]
15+
}
16+
17+
output "name" {
18+
description = "The name of the CloudFront Function."
19+
value = {
20+
"GENERAL" = one(aws_cloudfront_function.this[*].name)
21+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].name)
22+
}[var.type]
23+
}
24+
25+
output "description" {
26+
description = "The comment describing the CloudFront function."
27+
value = {
28+
"GENERAL" = one(aws_cloudfront_function.this[*].comment)
29+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].connection_function_config[0].comment)
30+
}[var.type]
31+
}
32+
33+
output "type" {
34+
description = "The type of the CloudFront Function."
35+
value = var.type
36+
}
37+
38+
output "status" {
39+
description = "The status of the function. Can be UNPUBLISHED, UNASSOCIATED or ASSOCIATED."
40+
value = {
41+
"GENERAL" = one(aws_cloudfront_function.this[*].status)
42+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].status)
43+
}[var.type]
44+
}
45+
46+
output "runtime" {
47+
description = "The runtime environment for the function."
48+
value = {
49+
"GENERAL" = one(aws_cloudfront_function.this[*].runtime)
50+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].connection_function_config[0].runtime)
51+
}[var.type]
52+
}
53+
54+
output "key_value_store" {
55+
description = "The ARN of the CloudFront Key Value Store associated with the function, if any."
56+
value = try(
57+
one(aws_cloudfront_function.this[0].key_value_store_associations[*]),
58+
aws_cloudfront_connection_function.this[0].connection_function_config[0].key_value_store_association[0].key_value_store_arn,
59+
var.key_value_store,
60+
)
61+
}
62+
63+
output "etag" {
64+
description = "The ETag hash of the function. This is the value for the DEVELOPMENT stage of the function."
65+
value = {
66+
"GENERAL" = one(aws_cloudfront_function.this[*].etag)
67+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].etag)
68+
}[var.type]
69+
}
70+
71+
output "live_stage_etag" {
72+
description = "The ETag hash of the LIVE stage of the function. Will be empty if the function has not been published."
73+
value = {
74+
"GENERAL" = one(aws_cloudfront_function.this[*].live_stage_etag)
75+
"CONNECTION" = one(aws_cloudfront_connection_function.this[*].live_stage_etag)
76+
}[var.type]
77+
}
78+
79+
output "resource_group" {
80+
description = "The resource group created to manage resources in this module."
81+
value = merge(
82+
{
83+
enabled = var.resource_group.enabled && var.module_tags_enabled
84+
},
85+
(var.resource_group.enabled && var.module_tags_enabled
86+
? {
87+
arn = module.resource_group[0].arn
88+
name = module.resource_group[0].name
89+
}
90+
: {}
91+
)
92+
)
93+
}
94+
95+
# output "debug" {
96+
# value = try(
97+
# {
98+
# for k, v in one(aws_cloudfront_function.this[*]) :
99+
# k => v
100+
# if !contains(["id", "arn", "name", "comment", "runtime", "etag", "live_stage_etag", "status", "publish", "code", "key_value_store_associations"], k)
101+
# },
102+
# {
103+
# for k, v in one(aws_cloudfront_connection_function.this[*]) :
104+
# k => v
105+
# if !contains(["id", "connection_function_arn", "name", "etag", "live_stage_etag", "status", "publish"], k)
106+
# }
107+
# )
108+
# }

modules/function/resource-group.tf

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
locals {
2+
resource_group_name = (var.resource_group.name != ""
3+
? var.resource_group.name
4+
: join(".", [
5+
local.metadata.package,
6+
local.metadata.module,
7+
replace(local.metadata.name, "/[^a-zA-Z0-9_\\.-]/", "-"),
8+
])
9+
)
10+
}
11+
12+
13+
module "resource_group" {
14+
source = "tedilabs/misc/aws//modules/resource-group"
15+
version = "~> 0.12.0"
16+
17+
count = (var.resource_group.enabled && var.module_tags_enabled) ? 1 : 0
18+
19+
name = local.resource_group_name
20+
description = var.resource_group.description
21+
22+
query = {
23+
resource_tags = local.module_tags
24+
}
25+
26+
module_tags_enabled = false
27+
tags = merge(
28+
local.module_tags,
29+
var.tags,
30+
)
31+
}

0 commit comments

Comments
 (0)