Skip to content

Commit 1e4320f

Browse files
authored
Make SQS modulair (#7)
* Make queue modulair, refactor and fix naming according architcture * fix creating fake zip file * Fix ci for webhook
1 parent e219b4a commit 1e4320f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+172
-105
lines changed

.github/workflows/lambda-syncer.yml renamed to .github/workflows/lambda-runner-binaries-syncer.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
name: Lambda Runner Distribution Syncer
1+
name: Lambda Runner Binaries Syncer
22
on:
33
push:
44
branches:
55
- master
66
pull_request:
77
paths:
88
- .github/workflows/lambda-agent-webhook.yml
9-
- "modules/action-runner-binary-cache/lambdas/syncer/**"
9+
- "modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/**"
1010

1111
jobs:
1212
build:
1313
runs-on: ubuntu-latest
1414
container: node:12
1515
defaults:
1616
run:
17-
working-directory: modules/action-runner-binary-cache/lambdas/syncer
17+
working-directory: modules/runner-binaries-syncer/lambdas/runner-binaries-syncer
1818

1919
steps:
2020
- uses: actions/checkout@v2

.github/workflows/lambda-agent-webhook.yml renamed to .github/workflows/lambda-webhook.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ on:
55
- master
66
pull_request:
77
paths:
8-
- .github/workflows/lambda-agent-webhook.yml
9-
- "modules/agent/lambdas/webhook/**"
8+
- .github/workflows/lambda-webhook.yml
9+
- "modules/webhook/lambdas/webhook/**"
1010

1111
jobs:
1212
build:
1313
runs-on: ubuntu-latest
1414
container: node:12
1515
defaults:
1616
run:
17-
working-directory: modules/agent/lambdas/webhook
17+
working-directory: modules/webhook/lambdas/webhook
1818

1919
steps:
2020
- uses: actions/checkout@v2

.github/workflows/terraform.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@ jobs:
1919
- name: "Checkout"
2020
uses: actions/checkout@v2
2121
- name: "Fake zip files" # Validate will fail if it cannot find the zip files
22-
run: |
23-
touch modules/action-runner-binary-cache/lambdas/syncer/syncer.zip
24-
touch modules/agent/lambdas/webhook/webhook.zip
22+
run:
23+
touch modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/runner-binaries-syncer.zip
24+
touch modules/webhook/lambdas/webhook/webhook.zip
2525
- name: "Terraform Format"
2626
uses: hashicorp/terraform-github-actions@master
2727
with:

examples/default/outputs.tf

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,13 @@ output "action_runners" {
55
}
66

77

8-
output "lambda_syncer_function_name" {
9-
value = module.runners.lambda_s3_action_runner_dist_syncer.id
8+
output "lambda_binaries_syncer_name" {
9+
value = module.runners.binaries_syncer.lambda.id
1010
}
1111

1212

1313
output "github_app_webhook_secret" {
1414
value = random_password.random.result
1515
}
16+
17+

main.tf

Lines changed: 21 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,34 @@ locals {
33
Environment = var.environment
44
})
55

6+
s3_action_runner_url = "s3://${module.runner_binaries.bucket.id}/${module.runner_binaries.runner_distribution_object_key}"
67
}
8+
79
resource "random_string" "random" {
810
length = 24
911
special = false
1012
upper = false
1113
}
1214

13-
module "dsitrubtion_cache" {
14-
source = "./modules/action-runner-binary-cache"
15+
resource "aws_sqs_queue" "queued_builds" {
16+
name = "${var.environment}-queued-builds.fifo"
17+
delay_seconds = 30
18+
fifo_queue = true
19+
receive_wait_time_seconds = 10
20+
content_based_deduplication = true
21+
22+
tags = var.tags
23+
}
24+
25+
module "webhook" {
26+
source = "./modules/webhook"
1527

1628
aws_region = var.aws_region
1729
environment = var.environment
1830
tags = local.tags
1931

20-
distribution_bucket_name = "${var.environment}-dist-${random_string.random.result}"
32+
sqs_build_queue = aws_sqs_queue.queued_builds
33+
github_app_webhook_secret = var.github_app_webhook_secret
2134
}
2235

2336
module "runners" {
@@ -28,36 +41,18 @@ module "runners" {
2841
environment = var.environment
2942
tags = local.tags
3043

31-
s3_location_runner_distribution = module.dsitrubtion_cache.s3_location_runner_distribution
44+
s3_bucket_runner_binaries = module.runner_binaries.bucket
45+
s3_location_runner_binaries = local.s3_action_runner_url
3246
}
3347

34-
35-
module "agent" {
36-
source = "./modules/agent"
48+
module "runner_binaries" {
49+
source = "./modules/runner-binaries-syncer"
3750

3851
aws_region = var.aws_region
3952
environment = var.environment
4053
tags = local.tags
4154

42-
github_app_webhook_secret = var.github_app_webhook_secret
43-
}
44-
45-
46-
resource "aws_iam_policy" "dist_bucket" {
47-
name = "${var.environment}-gh-distribution-bucket"
48-
path = "/"
49-
description = "Policy for the runner to download the github action runner."
50-
51-
policy = templatefile("${path.module}/policies/action-runner-s3-policy.json",
52-
{
53-
s3_arn = module.dsitrubtion_cache.distribution_bucket.arn
54-
}
55-
)
56-
}
57-
58-
resource "aws_iam_role_policy_attachment" "dist_bucket" {
59-
role = module.runners.role.name
60-
policy_arn = aws_iam_policy.dist_bucket.arn
55+
distribution_bucket_name = "${var.environment}-dist-${random_string.random.result}"
6156
}
6257

6358
resource "aws_resourcegroups_group" "resourcegroups_group" {

modules/action-runner-binary-cache/outputs.tf

Lines changed: 0 additions & 11 deletions
This file was deleted.

modules/agent/outputs.tf

Lines changed: 0 additions & 3 deletions
This file was deleted.

modules/agent/policies.tf

Lines changed: 0 additions & 17 deletions
This file was deleted.

modules/agent/webhook_queue.tf

Lines changed: 0 additions & 19 deletions
This file was deleted.

modules/action-runner-binary-cache/lambdas/syncer/package.json renamed to modules/runner-binaries-syncer/lambdas/runner-binaries-syncer/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"lint": "yarn eslint --ext ts,tsx src",
1111
"watch": "ts-node-dev --respawn --exit-child src/local.ts",
1212
"build": "ncc build src/lambda.ts -o dist",
13-
"dist": "yarn build && cd dist && zip ../syncer.zip index.js"
13+
"dist": "yarn build && cd dist && zip ../runner-binaries-syncer.zip index.js"
1414
},
1515
"devDependencies": {
1616
"@octokit/rest": "^17.6.0",

modules/action-runner-binary-cache/main.tf renamed to modules/runner-binaries-syncer/main.tf

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,3 @@ resource "aws_s3_bucket" "action_dist" {
88
force_destroy = true
99
tags = var.tags
1010
}
11-
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
output "bucket" {
2+
value = aws_s3_bucket.action_dist
3+
}
4+
5+
output "runner_distribution_object_key" {
6+
value = local.action_runner_distribution_object_key
7+
}
8+
9+
output "lambda" {
10+
value = aws_lambda_function.syncer
11+
}
12+
13+
output "lambda_role" {
14+
value = aws_iam_role.syncer_lambda
15+
}

modules/action-runner-binary-cache/syncer.tf renamed to modules/runner-binaries-syncer/runner-binaries-syncer.tf

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
resource "aws_lambda_function" "syncer" {
2-
filename = "${path.module}/lambdas/syncer/syncer.zip"
3-
source_code_hash = filebase64sha256("${path.module}/lambdas/syncer/syncer.zip")
2+
filename = "${path.module}/lambdas/runner-binaries-syncer/runner-binaries-syncer.zip"
3+
source_code_hash = filebase64sha256("${path.module}/lambdas/runner-binaries-syncer/runner-binaries-syncer.zip")
44
function_name = "${var.environment}-syncer"
55
role = aws_iam_role.syncer_lambda.arn
66
handler = "index.handler"
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
environment = "test"
2+
distribution_bucket_name = "alkj4klrj32trogjreoigfvj"
3+
aws_region = "eu-west-1"

modules/runners/main.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ resource "aws_launch_template" "runner" {
6868
environment = var.environment
6969
pre_install = var.userdata_pre_install
7070
post_install = var.userdata_post_install
71-
s3_location_runner_distribution = var.s3_location_runner_distribution
71+
s3_location_runner_distribution = var.s3_location_runner_binaries
7272
}))
7373
}
7474

modules/runners/policies.tf

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,3 +45,20 @@ resource "aws_iam_role_policy_attachment" "ssm_parameters" {
4545
role = aws_iam_role.runner.name
4646
policy_arn = aws_iam_policy.ssm_parameters.arn
4747
}
48+
49+
resource "aws_iam_policy" "dist_bucket" {
50+
name = "${var.environment}-gh-distribution-bucket"
51+
path = "/"
52+
description = "Policy for the runner to download the github action runner."
53+
54+
policy = templatefile("${path.module}/policies/instance-s3-policy.json",
55+
{
56+
s3_arn = var.s3_bucket_runner_binaries.arn
57+
}
58+
)
59+
}
60+
61+
resource "aws_iam_role_policy_attachment" "dist_bucket" {
62+
role = aws_iam_role.runner.name
63+
policy_arn = aws_iam_policy.dist_bucket.arn
64+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Sid": "githubActionDist",
6+
"Effect": "Allow",
7+
"Action": ["s3:GetObject", "s3:GetObjectAcl"],
8+
"Resource": ["${s3_arn}/*"]
9+
}
10+
]
11+
}

modules/runners/variables.tf

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,14 @@ variable "environment" {
2828
type = string
2929
}
3030

31-
variable "s3_location_runner_distribution" {
31+
variable "s3_bucket_runner_binaries" {
32+
type = object({
33+
arn = string
34+
})
35+
}
36+
37+
38+
variable "s3_location_runner_binaries" {
3239
description = "S3 location of runner distribution."
3340
type = string
3441
}
Lines changed: 1 addition & 1 deletion
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
module.exports = {
2+
root: true,
3+
parser: '@typescript-eslint/parser',
4+
plugins: ['@typescript-eslint'],
5+
extends: [
6+
'eslint:recommended',
7+
'plugin:@typescript-eslint/eslint-recommended',
8+
'plugin:@typescript-eslint/recommended',
9+
],
10+
};

modules/agent/webhook.tf renamed to modules/webhook/main.tf

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ resource "aws_lambda_function" "webhook" {
5050
environment {
5151
variables = {
5252
GITHUB_APP_WEBHOOK_SECRET = var.github_app_webhook_secret
53-
SQS_URL_WEBHOOK = aws_sqs_queue.webhook_events.id
53+
SQS_URL_WEBHOOK = var.sqs_build_queue.id
5454
}
5555
}
5656

@@ -65,31 +65,53 @@ resource "aws_lambda_permission" "webhook" {
6565
source_arn = "${aws_apigatewayv2_api.webhook.execution_arn}/*/*/${local.webhook_endpoint}"
6666
}
6767

68+
data "aws_iam_policy_document" "lambda_assume_role_policy" {
69+
statement {
70+
actions = ["sts:AssumeRole"]
71+
72+
principals {
73+
type = "Service"
74+
identifiers = ["lambda.amazonaws.com"]
75+
}
76+
}
77+
}
78+
6879
resource "aws_iam_role" "webhook_lambda" {
6980
name = "${var.environment}-action-webhook-lambda-role"
7081
assume_role_policy = data.aws_iam_policy_document.lambda_assume_role_policy.json
7182
tags = var.tags
7283
}
7384

85+
resource "aws_iam_policy" "webhook_logging" {
86+
name = "${var.environment}-lamda-logging-policy"
87+
description = "Lambda logging policy"
88+
89+
policy = templatefile("${path.module}/policies/lambda-cloudwatch.json", {})
90+
}
91+
7492
resource "aws_iam_policy_attachment" "webhook_logging" {
7593
name = "${var.environment}-logging"
7694
roles = [aws_iam_role.webhook_lambda.name]
77-
policy_arn = aws_iam_policy.lambda_logging.arn
95+
policy_arn = aws_iam_policy.webhook_logging.arn
7896
}
7997

8098
resource "aws_iam_policy" "webhook" {
99+
count = var.create_sqs_publish_policy ? 1 : 0
100+
81101
name = "${var.environment}-lamda-webhook-sqs-publish-policy"
82102
description = "Lambda webhook policy"
83103

84104
policy = templatefile("${path.module}/policies/lambda-webhook.json", {
85-
sqs_webhook_event_arn = aws_sqs_queue.webhook_events.arn
105+
sqs_resource_arn = var.sqs_build_queue.arn
86106
})
87107
}
88108

89109
resource "aws_iam_policy_attachment" "webhook" {
110+
count = var.create_sqs_publish_policy ? 1 : 0
111+
90112
name = "${var.environment}-webhook"
91113
roles = [aws_iam_role.webhook_lambda.name]
92-
policy_arn = aws_iam_policy.webhook.arn
114+
policy_arn = aws_iam_policy.webhook[0].arn
93115
}
94116

95117

0 commit comments

Comments
 (0)