Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cloudwatch logging - api gateway global settings #8

Merged
merged 6 commits into from
Jan 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 23 additions & 0 deletions iam.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
module "apigw_cwl_role" {
source = "terraform-aws-modules/iam/aws//modules/iam-assumable-role"
version = "~> 5.9.2"

trusted_role_services = [
"apigateway.amazonaws.com"
]

create_role = var.enable_global_apigw_logging

role_name_prefix = "apigw-cwl-global"
role_requires_mfa = false

custom_role_policy_arns = [
"arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs",
]
}

resource "aws_api_gateway_account" "api_gateway_account" {
count = var.enable_global_apigw_logging ? 1 : 0

cloudwatch_role_arn = module.apigw_cwl_role.iam_role_arn
}
4 changes: 4 additions & 0 deletions main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ resource "aws_api_gateway_stage" "stage" {
format = jsonencode(var.log_format)
}

tags = {
global_cwl_log_arn = var.enable_global_apigw_logging ? aws_api_gateway_account.api_gateway_account[0].cloudwatch_role_arn : ""
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jaezeu how this tag will be used ?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@niroz89 this is to force implicit dependency so that the stage will be created after the global aws_api_gateway_account cloudwatch role arn is updated.

Copy link

@lawliet89 lawliet89 Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think depends_on = [aws_api_gateway_account.api_gateway_account] might be sufficient. That should handle the "conditional" aspect -- I misunderstood your question on Slack.


}

resource "aws_cloudwatch_log_group" "log_group" {
Expand Down
6 changes: 6 additions & 0 deletions variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -131,3 +131,9 @@ variable "vpc_links" {
)
default = {}
}

variable "enable_global_apigw_logging" {
description = "Enable global apigw logging"
type = bool
default = false
}