Skip to content

Commit 62923b0

Browse files
committed
WIP: attach lambda to SQS queue
1 parent 3905b73 commit 62923b0

File tree

10 files changed

+145
-8
lines changed

10 files changed

+145
-8
lines changed

main.tf

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,16 @@ module "runners" {
2323
tags = var.tags
2424

2525
s3_location_runner_distribution = module.dsitrubtion_cache.s3_location_runner_distribution
26+
sqs = module.agent.sqs
27+
}
28+
29+
module "agent" {
30+
source = "./modules/agent"
31+
32+
aws_region = var.aws_region
33+
environment = var.environment
34+
tags = var.tags
35+
github_app_webhook_secret = "blaat"
2636
}
2737

2838

modules/agent/outputs.tf

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
output "gateway" {
22
value = aws_apigatewayv2_api.webhook
33
}
4+
5+
output "sqs" {
6+
value = aws_sqs_queue.webhook_events
7+
}

modules/agent/webhook.tf

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,12 @@ resource "aws_apigatewayv2_integration" "webhook" {
3838

3939

4040
resource "aws_lambda_function" "webhook" {
41-
filename = "webhook.zip"
42-
function_name = "${var.environment}-webhook"
43-
role = aws_iam_role.webhook_lambda.arn
44-
handler = "lambda.githubWebhook"
45-
runtime = "nodejs12.x"
41+
filename = "${path.module}/lambdas/webhook/webhook.zip"
42+
source_code_hash = filebase64sha256("${path.module}/lambdas/webhook/webhook.zip")
43+
function_name = "${var.environment}-webhook"
44+
role = aws_iam_role.webhook_lambda.arn
45+
handler = "index.githubWebhook"
46+
runtime = "nodejs12.x"
4647

4748
environment {
4849
variables = {
@@ -85,6 +86,3 @@ resource "aws_iam_policy_attachment" "webhook" {
8586
roles = [aws_iam_role.webhook_lambda.name]
8687
policy_arn = aws_iam_policy.webhook.arn
8788
}
88-
89-
90-
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { IncomingHttpHeaders } from 'http';
2+
13
export const handle = async (headers: IncomingHttpHeaders, payload: any): Promise<number> => {
24
return 200;
35
};
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
{
2+
"Records": [
3+
{
4+
"messageId": "f7f4e155-2079-4255-b7a0-1b7b4be45ff9",
5+
"receiptHandle": "AQEBpE+kwApifOOwbeTp0xFbeOOjnPTHvMCPFIbft3ah3C50GAUD2RKz3ZzVKFxFRdD50uHrKt7rKpDHCuavO5TBj9Gql7YH6G4iR9Vqz9XFFAQQGlcHf+EfVsDAewPr0FLiW40ZC+mNNGwYh9Bqbo5MAmpNWxYWImI4VIEGknW0oFLMSSVd6js7eSkRaJoL5belvjl06b48b/PUvyk0Su367xTTRsf6esih3ALb9RBI0ylV78kmDEQLcNi/7X1pA3UChQcvEn5+bp5JKlhalQRFDyRqMmZr7KeUDI/vG2gbMHOWuLkwzTl5jsKGc/pPVi86",
6+
"body": "{\"id\":128620228,\"repositoryName\":\"Hello-World\",\"repositoryOwner\":\"Codertocat\",\"eventType\":\"check_run\",\"installationId\":12345}",
7+
"attributes": {
8+
"ApproximateReceiveCount": "1",
9+
"SentTimestamp": "1588152306469",
10+
"SequenceNumber": "18853311064165616128",
11+
"MessageGroupId": "128620228",
12+
"SenderId": "AROAVQMGTCYMGIEWL5JV5:default-action-runners-webhook",
13+
"MessageDeduplicationId": "bdc9a81e515df0131ddc015b1182b57ffcd79b0321bfe32bb40572b23ee68c50",
14+
"ApproximateFirstReceiveTimestamp": "1588152306469"
15+
},
16+
"messageAttributes": {},
17+
"md5OfBody": "f30235cb7733c3ac59a14d99c59a6dbf",
18+
"eventSource": "aws:sqs",
19+
"eventSourceARN": "arn:aws:sqs:eu-west-1:378776262168:default-action-runners-webhook-events.fifo",
20+
"awsRegion": "eu-west-1"
21+
}
22+
]
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"logs:CreateLogGroup",
8+
"logs:CreateLogStream",
9+
"logs:PutLogEvents"
10+
],
11+
"Resource": "arn:aws:logs:*:*:*"
12+
}
13+
]
14+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Effect": "Allow",
6+
"Action": [
7+
"sqs:ReceiveMessage",
8+
"sqs:GetQueueAttributes",
9+
"sqs:DeleteMessage"
10+
],
11+
"Resource": "${sqs_arn}"
12+
}
13+
]
14+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
resource "aws_lambda_function" "scale_runners_lambda" {
2+
filename = "${path.module}/lambdas/scale-runners/scale-runners.zip"
3+
source_code_hash = filebase64sha256("${path.module}/lambdas/scale-runners/scale-runners.zip")
4+
function_name = "${var.environment}-scale-runners"
5+
role = aws_iam_role.scale_runners_lambda.arn
6+
handler = "index.handler"
7+
runtime = "nodejs12.x"
8+
9+
# environment {
10+
# variables = {
11+
# }
12+
# }
13+
}
14+
15+
resource "aws_lambda_event_source_mapping" "scale_runners_lambda" {
16+
event_source_arn = var.sqs.arn
17+
function_name = aws_lambda_function.scale_runners_lambda.arn
18+
}
19+
20+
resource "aws_lambda_permission" "scale_runners_lambda" {
21+
statement_id = "AllowExecutionFromSQS"
22+
action = "lambda:InvokeFunction"
23+
function_name = aws_lambda_function.scale_runners_lambda.function_name
24+
principal = "sqs.amazonaws.com"
25+
source_arn = var.sqs.arn
26+
}
27+
28+
resource "aws_iam_role" "scale_runners_lambda" {
29+
name = "${var.environment}-action-scale-runners-lambda-role"
30+
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
31+
}
32+
33+
data "aws_iam_policy_document" "lambda_assume_role_policy" {
34+
statement {
35+
actions = ["sts:AssumeRole"]
36+
37+
principals {
38+
type = "Service"
39+
identifiers = ["lambda.amazonaws.com"]
40+
}
41+
}
42+
}
43+
44+
resource "aws_iam_policy" "lambda_logging" {
45+
name = "${var.environment}-lamda-runners-logging-policy"
46+
description = "Lambda logging policy"
47+
48+
policy = templatefile("${path.module}/policies/lambda-cloudwatch.json", {})
49+
}
50+
51+
resource "aws_iam_policy_attachment" "scale_runners_lambda_logging" {
52+
name = "${var.environment}-logging"
53+
roles = [aws_iam_role.scale_runners_lambda.name]
54+
policy_arn = aws_iam_policy.lambda_logging.arn
55+
}
56+
57+
resource "aws_iam_policy" "scale_runners_lambda" {
58+
name = "${var.environment}-lamda-scale-runners-sqs-receive-policy"
59+
description = "Lambda webhook policy"
60+
61+
policy = templatefile("${path.module}/policies/lambda-scale-runners.json", {
62+
sqs_arn = var.sqs.arn
63+
})
64+
}
65+
66+
resource "aws_iam_policy_attachment" "scale_runners_lambda" {
67+
name = "${var.environment}-scale-runners"
68+
roles = [aws_iam_role.scale_runners_lambda.name]
69+
policy_arn = aws_iam_policy.scale_runners_lambda.arn
70+
}

modules/runners/variables.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,3 +78,4 @@ variable "userdata_post_install" {
7878
type = string
7979
default = ""
8080
}
81+
variable "sqs" {}

outputs.tf

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ output "runners" {
44
launch_template_id = module.runners.launch_template.id
55
launch_template_version = module.runners.launch_template.latest_version
66
action_runner_distribution = module.dsitrubtion_cache.s3_location_runner_distribution
7+
gateway = module.agent.gateway
78
}
89
}

0 commit comments

Comments
 (0)