Skip to content

Commit c123558

Browse files
authored
feat!: Added creation of CW Log Group. Bump AWS provider version. (#103)
1 parent 2f45c68 commit c123558

File tree

11 files changed

+105
-40
lines changed

11 files changed

+105
-40
lines changed

.pre-commit-config.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
repos:
22
- repo: https://github.com/antonbabenko/pre-commit-terraform
3-
rev: v1.88.0
3+
rev: v1.88.2
44
hooks:
55
- id: terraform_fmt
66
- id: terraform_wrapper_module_for_each

README.md

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -84,12 +84,13 @@ module "api_gateway" {
8484
8585
create = false # to disable all resources
8686
87-
create_api_gateway = false # to control creation of API Gateway
88-
create_api_domain_name = false # to control creation of API Gateway Domain Name
89-
create_default_stage = false # to control creation of "$default" stage
90-
create_default_stage_api_mapping = false # to control creation of "$default" stage and API mapping
91-
create_routes_and_integrations = false # to control creation of routes and integrations
92-
create_vpc_link = false # to control creation of VPC link
87+
create_api_gateway = false # to control creation of API Gateway
88+
create_api_domain_name = false # to control creation of API Gateway Domain Name
89+
create_default_stage = false # to control creation of "$default" stage
90+
create_default_stage_access_log_group = false # to control creation of CloudWatch Access log group for "$default" stage
91+
create_default_stage_api_mapping = false # to control creation of "$default" stage and API mapping
92+
create_routes_and_integrations = false # to control creation of routes and integrations
93+
create_vpc_link = false # to control creation of VPC link
9394
9495
integrations= {
9596
"GET /" = {
@@ -116,13 +117,13 @@ module "api_gateway" {
116117
| Name | Version |
117118
|------|---------|
118119
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
119-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0 |
120+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.30 |
120121

121122
## Providers
122123

123124
| Name | Version |
124125
|------|---------|
125-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0 |
126+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.30 |
126127

127128
## Modules
128129

@@ -140,6 +141,7 @@ No modules.
140141
| [aws_apigatewayv2_route.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_route) | resource |
141142
| [aws_apigatewayv2_stage.default](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_stage) | resource |
142143
| [aws_apigatewayv2_vpc_link.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_vpc_link) | resource |
144+
| [aws_cloudwatch_log_group.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
143145

144146
## Inputs
145147

@@ -154,13 +156,21 @@ No modules.
154156
| <a name="input_create_api_domain_name"></a> [create\_api\_domain\_name](#input\_create\_api\_domain\_name) | Whether to create API domain name resource | `bool` | `true` | no |
155157
| <a name="input_create_api_gateway"></a> [create\_api\_gateway](#input\_create\_api\_gateway) | Whether to create API Gateway | `bool` | `true` | no |
156158
| <a name="input_create_default_stage"></a> [create\_default\_stage](#input\_create\_default\_stage) | Whether to create default stage | `bool` | `true` | no |
159+
| <a name="input_create_default_stage_access_log_group"></a> [create\_default\_stage\_access\_log\_group](#input\_create\_default\_stage\_access\_log\_group) | Whether to create CloudWatch log group for Access logs | `bool` | `false` | no |
157160
| <a name="input_create_default_stage_api_mapping"></a> [create\_default\_stage\_api\_mapping](#input\_create\_default\_stage\_api\_mapping) | Whether to create default stage API mapping | `bool` | `true` | no |
158161
| <a name="input_create_routes_and_integrations"></a> [create\_routes\_and\_integrations](#input\_create\_routes\_and\_integrations) | Whether to create routes and integrations resources | `bool` | `true` | no |
159162
| <a name="input_create_vpc_link"></a> [create\_vpc\_link](#input\_create\_vpc\_link) | Whether to create VPC links | `bool` | `true` | no |
160163
| <a name="input_credentials_arn"></a> [credentials\_arn](#input\_credentials\_arn) | Part of quick create. Specifies any credentials required for the integration. Applicable for HTTP APIs. | `string` | `null` | no |
161164
| <a name="input_default_route_settings"></a> [default\_route\_settings](#input\_default\_route\_settings) | Settings for default route | `map(string)` | `{}` | no |
162165
| <a name="input_default_stage_access_log_destination_arn"></a> [default\_stage\_access\_log\_destination\_arn](#input\_default\_stage\_access\_log\_destination\_arn) | Default stage's ARN of the CloudWatch Logs log group to receive access logs. Any trailing :* is trimmed from the ARN. | `string` | `null` | no |
163166
| <a name="input_default_stage_access_log_format"></a> [default\_stage\_access\_log\_format](#input\_default\_stage\_access\_log\_format) | Default stage's single line format of the access logs of data, as specified by selected $context variables. | `string` | `null` | no |
167+
| <a name="input_default_stage_access_log_group_class"></a> [default\_stage\_access\_log\_group\_class](#input\_default\_stage\_access\_log\_group\_class) | Specified the log class of the Access log group. Possible values are: STANDARD or INFREQUENT\_ACCESS | `string` | `null` | no |
168+
| <a name="input_default_stage_access_log_group_kms_key_id"></a> [default\_stage\_access\_log\_group\_kms\_key\_id](#input\_default\_stage\_access\_log\_group\_kms\_key\_id) | The ARN of the KMS Key to use when encrypting log data for Access logs | `string` | `null` | no |
169+
| <a name="input_default_stage_access_log_group_name"></a> [default\_stage\_access\_log\_group\_name](#input\_default\_stage\_access\_log\_group\_name) | Specifies the name of CloudWatch Log Group for Access logs | `string` | `null` | no |
170+
| <a name="input_default_stage_access_log_group_name_suffix"></a> [default\_stage\_access\_log\_group\_name\_suffix](#input\_default\_stage\_access\_log\_group\_name\_suffix) | Specifies the name suffix of CloudWatch Log Group for Access logs | `string` | `""` | no |
171+
| <a name="input_default_stage_access_log_group_retention_in_days"></a> [default\_stage\_access\_log\_group\_retention\_in\_days](#input\_default\_stage\_access\_log\_group\_retention\_in\_days) | Specifies the number of days you want to retain log events in the specified log group for Access logs | `number` | `null` | no |
172+
| <a name="input_default_stage_access_log_group_skip_destroy"></a> [default\_stage\_access\_log\_group\_skip\_destroy](#input\_default\_stage\_access\_log\_group\_skip\_destroy) | Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state | `bool` | `false` | no |
173+
| <a name="input_default_stage_access_log_group_tags"></a> [default\_stage\_access\_log\_group\_tags](#input\_default\_stage\_access\_log\_group\_tags) | Additional tags for the Access logs | `map(string)` | `{}` | no |
164174
| <a name="input_default_stage_tags"></a> [default\_stage\_tags](#input\_default\_stage\_tags) | A mapping of tags to assign to the default stage resource. | `map(string)` | `{}` | no |
165175
| <a name="input_description"></a> [description](#input\_description) | The description of the API. | `string` | `null` | no |
166176
| <a name="input_disable_execute_api_endpoint"></a> [disable\_execute\_api\_endpoint](#input\_disable\_execute\_api\_endpoint) | Whether clients can invoke the API by using the default execute-api endpoint. To require that clients use a custom domain name to invoke the API, disable the default endpoint | `string` | `false` | no |

examples/complete-http/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Note that this example may create resources which cost money. Run `terraform des
2121
| Name | Version |
2222
|------|---------|
2323
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
24-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0 |
24+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.30 |
2525
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
2626
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2727
| <a name="requirement_tls"></a> [tls](#requirement\_tls) | >= 3.1 |
@@ -30,7 +30,7 @@ Note that this example may create resources which cost money. Run `terraform des
3030

3131
| Name | Version |
3232
|------|---------|
33-
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 4.0 |
33+
| <a name="provider_aws"></a> [aws](#provider\_aws) | >= 5.30 |
3434
| <a name="provider_null"></a> [null](#provider\_null) | >= 2.0 |
3535
| <a name="provider_random"></a> [random](#provider\_random) | >= 2.0 |
3636
| <a name="provider_tls"></a> [tls](#provider\_tls) | >= 3.1 |
@@ -49,7 +49,6 @@ Note that this example may create resources which cost money. Run `terraform des
4949
| Name | Type |
5050
|------|------|
5151
| [aws_apigatewayv2_authorizer.some_authorizer](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/apigatewayv2_authorizer) | resource |
52-
| [aws_cloudwatch_log_group.logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cloudwatch_log_group) | resource |
5352
| [aws_cognito_user_pool.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/cognito_user_pool) | resource |
5453
| [aws_route53_record.api](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/route53_record) | resource |
5554
| [aws_s3_bucket.truststore](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/s3_bucket) | resource |

examples/complete-http/main.tf

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ module "api_gateway" {
2626
description = "My awesome HTTP API Gateway"
2727
protocol_type = "HTTP"
2828

29+
create_default_stage_access_log_group = true
30+
2931
fail_on_warnings = false
3032

3133
cors_configuration = {
@@ -42,8 +44,7 @@ module "api_gateway" {
4244
domain_name = local.domain_name
4345
domain_name_certificate_arn = module.acm.acm_certificate_arn
4446

45-
default_stage_access_log_destination_arn = aws_cloudwatch_log_group.logs.arn
46-
default_stage_access_log_format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
47+
default_stage_access_log_format = "$context.identity.sourceIp - - [$context.requestTime] \"$context.httpMethod $context.routeKey $context.protocol\" $context.status $context.responseLength $context.requestId $context.integrationErrorMessage"
4748

4849
default_route_settings = {
4950
detailed_metrics_enabled = true
@@ -62,7 +63,6 @@ module "api_gateway" {
6263
}
6364

6465
integrations = {
65-
6666
"ANY /" = {
6767
lambda_arn = module.lambda_function.lambda_function_arn
6868
payload_format_version = "2.0"
@@ -244,10 +244,6 @@ resource "random_pet" "this" {
244244
length = 2
245245
}
246246

247-
resource "aws_cloudwatch_log_group" "logs" {
248-
name = random_pet.this.id
249-
}
250-
251247
#############################################
252248
# Using packaged function from Lambda module
253249
#############################################

examples/complete-http/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0"
7+
version = ">= 5.30"
88
}
99
random = {
1010
source = "hashicorp/random"

examples/vpc-link-http/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ Note that this example may create resources which cost money. Run `terraform des
2121
| Name | Version |
2222
|------|---------|
2323
| <a name="requirement_terraform"></a> [terraform](#requirement\_terraform) | >= 1.0 |
24-
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 4.0 |
24+
| <a name="requirement_aws"></a> [aws](#requirement\_aws) | >= 5.30 |
2525
| <a name="requirement_null"></a> [null](#requirement\_null) | >= 2.0 |
2626
| <a name="requirement_random"></a> [random](#requirement\_random) | >= 2.0 |
2727

examples/vpc-link-http/versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0"
7+
version = ">= 5.30"
88
}
99
random = {
1010
source = "hashicorp/random"

main.tf

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,19 @@ resource "aws_apigatewayv2_domain_name" "this" {
6060
tags = merge(var.domain_name_tags, var.tags)
6161
}
6262

63+
# Default stage log group
64+
resource "aws_cloudwatch_log_group" "this" {
65+
count = var.create && var.create_default_stage && var.create_default_stage_access_log_group ? 1 : 0
66+
67+
name = coalesce(var.default_stage_access_log_group_name, "${var.name}${var.default_stage_access_log_group_name_suffix}")
68+
retention_in_days = var.default_stage_access_log_group_retention_in_days
69+
kms_key_id = var.default_stage_access_log_group_kms_key_id
70+
skip_destroy = var.default_stage_access_log_group_skip_destroy
71+
log_group_class = var.default_stage_access_log_group_class
72+
73+
tags = merge(var.tags, var.default_stage_access_log_group_tags)
74+
}
75+
6376
# Default stage
6477
resource "aws_apigatewayv2_stage" "default" {
6578
count = var.create && var.create_default_stage ? 1 : 0
@@ -69,10 +82,10 @@ resource "aws_apigatewayv2_stage" "default" {
6982
auto_deploy = true
7083

7184
dynamic "access_log_settings" {
72-
for_each = var.default_stage_access_log_destination_arn != null && var.default_stage_access_log_format != null ? [true] : []
85+
for_each = (var.default_stage_access_log_destination_arn != null || var.create_default_stage_access_log_group) && var.default_stage_access_log_format != null ? [true] : []
7386

7487
content {
75-
destination_arn = var.default_stage_access_log_destination_arn
88+
destination_arn = try(aws_cloudwatch_log_group.this[0].arn, var.default_stage_access_log_destination_arn)
7689
format = var.default_stage_access_log_format
7790
}
7891
}
@@ -110,6 +123,8 @@ resource "aws_apigatewayv2_stage" "default" {
110123
lifecycle {
111124
ignore_changes = [deployment_id]
112125
}
126+
127+
depends_on = [aws_apigatewayv2_integration.this]
113128
}
114129

115130
# Default API mapping
@@ -123,7 +138,7 @@ resource "aws_apigatewayv2_api_mapping" "this" {
123138

124139
# Routes and integrations
125140
resource "aws_apigatewayv2_route" "this" {
126-
for_each = var.create && var.create_routes_and_integrations ? var.integrations : {}
141+
for_each = { for k, v in var.integrations : k => v if var.create && var.create_routes_and_integrations }
127142

128143
api_id = aws_apigatewayv2_api.this[0].id
129144
route_key = each.key
@@ -142,7 +157,7 @@ resource "aws_apigatewayv2_route" "this" {
142157
}
143158

144159
resource "aws_apigatewayv2_integration" "this" {
145-
for_each = var.create && var.create_routes_and_integrations ? var.integrations : {}
160+
for_each = { for k, v in var.integrations : k => v if var.create && var.create_routes_and_integrations }
146161

147162
api_id = aws_apigatewayv2_api.this[0].id
148163
description = try(each.value.description, null)
@@ -186,7 +201,7 @@ resource "aws_apigatewayv2_integration" "this" {
186201

187202
# Authorizers
188203
resource "aws_apigatewayv2_authorizer" "this" {
189-
for_each = var.create && var.create_routes_and_integrations ? var.authorizers : {}
204+
for_each = { for k, v in var.authorizers : k => v if var.create && var.create_routes_and_integrations }
190205

191206
api_id = aws_apigatewayv2_api.this[0].id
192207

@@ -211,7 +226,7 @@ resource "aws_apigatewayv2_authorizer" "this" {
211226

212227
# VPC Link (Private API)
213228
resource "aws_apigatewayv2_vpc_link" "this" {
214-
for_each = var.create && var.create_vpc_link ? var.vpc_links : {}
229+
for_each = { for k, v in var.vpc_links : k => v if var.create && var.create_vpc_link }
215230

216231
name = try(each.value.name, each.key)
217232
security_group_ids = each.value["security_group_ids"]

variables.tf

Lines changed: 48 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,11 @@ variable "create_default_stage_api_mapping" {
2222
default = true
2323
}
2424

25-
# variable "create_stage" {
26-
# description = "Whether to create custom stage"
27-
# type = bool
28-
# default = false
29-
# }
30-
#
31-
# variable "create_stage_api_mapping" {
32-
# description = "Whether to create stage API mapping"
33-
# type = bool
34-
# default = false
35-
# }
25+
variable "create_default_stage_access_log_group" {
26+
description = "Whether to create CloudWatch log group for Access logs"
27+
type = bool
28+
default = false
29+
}
3630

3731
variable "create_api_domain_name" {
3832
description = "Whether to create API domain name resource"
@@ -163,6 +157,49 @@ variable "default_stage_tags" {
163157
default = {}
164158
}
165159

160+
# Log group for default stage
161+
variable "default_stage_access_log_group_name" {
162+
description = "Specifies the name of CloudWatch Log Group for Access logs"
163+
type = string
164+
default = null
165+
}
166+
167+
variable "default_stage_access_log_group_name_suffix" {
168+
description = "Specifies the name suffix of CloudWatch Log Group for Access logs"
169+
type = string
170+
default = ""
171+
}
172+
173+
variable "default_stage_access_log_group_retention_in_days" {
174+
description = "Specifies the number of days you want to retain log events in the specified log group for Access logs"
175+
type = number
176+
default = null
177+
}
178+
179+
variable "default_stage_access_log_group_kms_key_id" {
180+
description = "The ARN of the KMS Key to use when encrypting log data for Access logs"
181+
type = string
182+
default = null
183+
}
184+
185+
variable "default_stage_access_log_group_skip_destroy" {
186+
description = "Set to true if you do not wish the log group (and any logs it may contain) to be deleted at destroy time, and instead just remove the log group from the Terraform state"
187+
type = bool
188+
default = false
189+
}
190+
191+
variable "default_stage_access_log_group_class" {
192+
description = "Specified the log class of the Access log group. Possible values are: STANDARD or INFREQUENT_ACCESS"
193+
type = string
194+
default = null
195+
}
196+
197+
variable "default_stage_access_log_group_tags" {
198+
description = "Additional tags for the Access logs"
199+
type = map(string)
200+
default = {}
201+
}
202+
166203
#####
167204
# default stage API mapping
168205

versions.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ terraform {
44
required_providers {
55
aws = {
66
source = "hashicorp/aws"
7-
version = ">= 4.0"
7+
version = ">= 5.30"
88
}
99
}
1010
}

0 commit comments

Comments
 (0)