-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f652c60
commit 9bb3747
Showing
12 changed files
with
199 additions
and
185 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,3 +84,4 @@ output "lambda" { | |
ingestion_lambda_arn = module.lambda_ingestion.lambda_function_arn | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
module "cognito_sns" { | ||
source = "terraform-aws-modules/sns/aws" | ||
version = "~> 5.0" | ||
|
||
name = "${local.prefix}-cognito-topic" | ||
|
||
subscriptions = [ | ||
{ | ||
protocol = "email" | ||
endpoint = var.cognito_admin_email | ||
} | ||
] | ||
} | ||
|
||
resource "aws_iam_role" "cognito_sns_role" { | ||
name = "${local.prefix}-cognito-sns-role" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17", | ||
Statement = [ | ||
{ | ||
Effect = "Allow", | ||
Principal = { | ||
Service = "cognito-idp.amazonaws.com" | ||
}, | ||
Action = "sts:AssumeRole" | ||
} | ||
] | ||
}) | ||
} | ||
|
||
resource "aws_iam_policy" "cognito_sns_policy" { | ||
name = "${local.prefix}-cognito-sns-policy" | ||
description = "Allows Cognito to publish messages to the SNS topic" | ||
|
||
policy = jsonencode({ | ||
Version = "2012-10-17", | ||
Statement = [ | ||
{ | ||
Sid = "AllowCognitoToPublish", | ||
Effect = "Allow", | ||
Action = ["sns:Publish"], | ||
Resource = module.cognito_sns.topic_arn | ||
} | ||
] | ||
}) | ||
} | ||
|
||
resource "aws_iam_role_policy_attachment" "cognito_sns_policy_attachment" { | ||
role = aws_iam_role.cognito_sns_role.id | ||
policy_arn = aws_iam_policy.cognito_sns_policy.arn | ||
} | ||
|
||
resource "aws_iam_role" "cognito_lambda_role" { | ||
name = "${local.prefix}-lambda-execution-role" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17", | ||
Statement = [ | ||
{ | ||
Effect = "Allow", | ||
Principal = { | ||
Service = "lambda.amazonaws.com" | ||
}, | ||
Action = "sts:AssumeRole" | ||
} | ||
] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,4 +36,10 @@ variable "api_gateway_stage_name" { | |
description = "The stage name for API Gateway (e.g. dev or live)" | ||
type = string | ||
default = "dev" | ||
} | ||
|
||
variable "cognito_admin_email" { | ||
description = "Admin email address for Cognito SNS notifications" | ||
type = string | ||
default = "[email protected]" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
resource "aws_api_gateway_account" "account" { | ||
cloudwatch_role_arn = aws_iam_role.api_gateway_cloudwatch_role.arn | ||
|
||
depends_on = [ | ||
aws_iam_role.api_gateway_cloudwatch_role, | ||
aws_iam_role_policy.api_gateway_cloudwatch_policy | ||
] | ||
} | ||
|
||
resource "aws_iam_role" "api_gateway_cloudwatch_role" { | ||
name = "${var.prefix}-api-gateway-cloudwatch-role" | ||
|
||
assume_role_policy = jsonencode({ | ||
Version = "2012-10-17", | ||
Statement = [ | ||
{ | ||
Effect = "Allow", | ||
Principal = { | ||
Service = "apigateway.amazonaws.com" | ||
}, | ||
Action = "sts:AssumeRole" | ||
} | ||
] | ||
}) | ||
} | ||
|
||
resource "aws_iam_role_policy" "api_gateway_cloudwatch_policy" { | ||
role = aws_iam_role.api_gateway_cloudwatch_role.id | ||
|
||
policy = jsonencode({ | ||
Version = "2012-10-17", | ||
Statement = [ | ||
{ | ||
Effect = "Allow", | ||
Action = [ | ||
"logs:CreateLogGroup", | ||
"logs:CreateLogStream", | ||
"logs:PutLogEvents", | ||
"logs:DescribeLogGroups", | ||
"logs:DescribeLogStreams", | ||
"logs:GetLogEvents", | ||
"logs:FilterLogEvents" | ||
], | ||
Resource = [ | ||
"arn:aws:logs:${var.region}:${data.aws_caller_identity.current.account_id}:log-group:/aws/apigateway/*" | ||
] | ||
}, | ||
{ | ||
Effect = "Allow", | ||
Action = [ | ||
"apigateway:GET", | ||
"apigateway:PUT", | ||
"apigateway:POST", | ||
"apigateway:DELETE", | ||
"apigateway:PATCH" | ||
], | ||
Resource = aws_api_gateway_rest_api.api_gateway.execution_arn | ||
} | ||
] | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
resource "aws_lambda_function" "api_gateway_lambda" { | ||
function_name = "${var.prefix}-api-gateway-lambda" | ||
runtime = "nodejs18.x" | ||
role = var.lambda_role_arn | ||
handler = "api_gateway_lambda.handler" | ||
|
||
source_code_hash = filebase64sha256("${path.module}/api_gateway_lambda.zip") | ||
filename = "${path.module}/api_gateway_lambda.zip" | ||
timeout = 15 | ||
publish = true | ||
description = "Handles API Gateway requests for the ${var.prefix} service" | ||
} | ||
|
||
resource "aws_lambda_alias" "live" { | ||
name = "live" | ||
description = "Alias pointing to the live version of the Lambda function" | ||
function_name = aws_lambda_function.api_gateway_lambda.arn | ||
function_version = "$LATEST" | ||
} | ||
|
||
resource "aws_lambda_alias" "dev" { | ||
name = "dev" | ||
description = "Alias pointing to the dev version of the Lambda function" | ||
function_name = aws_lambda_function.api_gateway_lambda.arn | ||
function_version = "$LATEST" | ||
} | ||
|
||
resource "aws_lambda_permission" "allow_api_gateway" { | ||
statement_id = "AllowAPIGatewayInvoke" | ||
action = "lambda:InvokeFunction" | ||
function_name = lookup({ | ||
"live" = aws_lambda_alias.live.arn, | ||
"dev" = aws_lambda_alias.dev.arn | ||
}, var.lambda_alias, aws_lambda_alias.live.arn) | ||
principal = "apigateway.amazonaws.com" | ||
source_arn = "arn:aws:execute-api:${var.region}:${data.aws_caller_identity.current.account_id}:${aws_api_gateway_rest_api.api_gateway.id}/*/*/*" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.