Skip to content

Commit 4efe6aa

Browse files
authored
[NDR-152] Create API Gateway role to enable CloudWatch logging (#340)
1 parent 10731f0 commit 4efe6aa

File tree

3 files changed

+38
-1
lines changed

3 files changed

+38
-1
lines changed

infrastructure/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,7 @@
177177

178178
| Name | Type |
179179
|------|------|
180+
| [aws_api_gateway_account.logging](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_account) | resource |
180181
| [aws_api_gateway_api_key.api_key_pdm](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_api_key) | resource |
181182
| [aws_api_gateway_api_key.apim](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_api_key) | resource |
182183
| [aws_api_gateway_authorizer.repo_authoriser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/api_gateway_authorizer) | resource |
@@ -258,6 +259,7 @@
258259
| [aws_iam_policy.ses_send_email_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
259260
| [aws_iam_policy.ssm_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
260261
| [aws_iam_policy.ssm_access_policy_authoriser](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
262+
| [aws_iam_role.api_gateway_cloudwatch](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
261263
| [aws_iam_role.cognito_unauthenticated](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
262264
| [aws_iam_role.create_post_presign_url_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
263265
| [aws_iam_role.cross_account_backup_iam_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
@@ -270,6 +272,7 @@
270272
| [aws_iam_role.splunk_sqs_forwarder](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
271273
| [aws_iam_role.stitch_presign_url_role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role) | resource |
272274
| [aws_iam_role_policy.splunk_access_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy) | resource |
275+
| [aws_iam_role_policy_attachment.api_gateway_logs](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
273276
| [aws_iam_role_policy_attachment.backup_policy](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
274277
| [aws_iam_role_policy_attachment.cloudwatch_rum_cognito_unauth](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
275278
| [aws_iam_role_policy_attachment.create_post_presign_url](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |

infrastructure/api.tf

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,19 @@ resource "aws_api_gateway_stage" "ndr_api" {
9393
stage_name = var.environment
9494
xray_tracing_enabled = var.enable_xray_tracing
9595

96-
depends_on = [aws_cloudwatch_log_group.api_gateway_stage]
96+
depends_on = [
97+
aws_cloudwatch_log_group.api_gateway_stage
98+
]
9799
}
98100

99101
resource "aws_cloudwatch_log_group" "api_gateway_stage" {
100102
# Name must follow this format to allow execution logging
101103
# https://docs.aws.amazon.com/apigateway/latest/developerguide/set-up-logging.html
102104
name = "API-Gateway-Execution-Logs_${aws_api_gateway_rest_api.ndr_doc_store_api.id}/${var.environment}"
103105
retention_in_days = 0
106+
depends_on = [
107+
aws_api_gateway_account.logging
108+
]
104109
}
105110

106111
resource "aws_api_gateway_method_settings" "api_gateway_stage" {

infrastructure/iam.tf

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,32 @@ resource "aws_iam_role_policy_attachment" "ods_report_presign_url" {
193193
role = aws_iam_role.ods_report_presign_url_role.name
194194
policy_arn = aws_iam_policy.s3_document_data_policy_for_ods_report_lambda.arn
195195
}
196+
197+
resource "aws_iam_role" "api_gateway_cloudwatch" {
198+
count = local.is_sandbox ? 0 : 1
199+
name = "${terraform.workspace}_NdrAPIGatewayLogs"
200+
201+
assume_role_policy = jsonencode({
202+
Version = "2012-10-17"
203+
Statement = [
204+
{
205+
Action = "sts:AssumeRole"
206+
Effect = "Allow"
207+
Principal = {
208+
Service = "apigateway.amazonaws.com"
209+
}
210+
},
211+
]
212+
})
213+
}
214+
215+
resource "aws_iam_role_policy_attachment" "api_gateway_logs" {
216+
count = local.is_sandbox ? 0 : 1
217+
role = aws_iam_role.api_gateway_cloudwatch[0].name
218+
policy_arn = "arn:aws:iam::aws:policy/service-role/AmazonAPIGatewayPushToCloudWatchLogs"
219+
}
220+
221+
resource "aws_api_gateway_account" "logging" {
222+
count = local.is_sandbox ? 0 : 1
223+
cloudwatch_role_arn = aws_iam_role.api_gateway_cloudwatch[0].arn
224+
}

0 commit comments

Comments
 (0)