Skip to content

Commit e23447a

Browse files
authored
Trigger download lambda on deploy (#39)
* Trigger download lambda on deploy * Update docs
1 parent d2aa695 commit e23447a

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ terraform init
158158
terraform apply
159159
```
160160

161-
Check the terraform output for the API gateway url (endpoint), which you need in the next step. The lambda for syncing the GitHub distribution will be executed by a trigger via CloudWatch. To ensure the binary is cached, trigger the `runner-binaries-syncer` manually. The payload does not matter. (e.g. `aws lambda invoke --function-name <environment>-syncer response.json`)
161+
Check the terraform output for the API gateway url (endpoint), which you need in the next step. The lambda for syncing the GitHub distribution will be executed by a trigger via CloudWatch. After deployment the function is triggered via S3 to ensure the distribution is cached.
162162

163163
### Setup GitHub App (part 2)
164164

modules/runner-binaries-syncer/README.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Module - Runner binaries syncer
22

3-
This module creates a lambda that will sync GitHub action binary to a S3 bucket, the lambda will be triggered via a CloudWatch event. The distribution is cached to avoid the latency of downloading the distribution during the setup.
3+
This module creates a lambda that will sync GitHub action binary to a S3 bucket, the lambda will be triggered via a CloudWatch event. The distribution is cached to avoid the latency of downloading the distribution during the setup. After deployment the lambda will be triggered via an S3 object created at deployment time.
44

55
## Usages
66

@@ -41,31 +41,31 @@ No requirements.
4141
## Providers
4242

4343
| Name | Version |
44-
|------|---------|
45-
| aws | n/a |
44+
| ---- | ------- |
45+
| aws | n/a |
4646

4747
## Inputs
4848

49-
| Name | Description | Type | Default | Required |
50-
|------|-------------|------|---------|:--------:|
51-
| aws\_region | AWS region. | `string` | n/a | yes |
52-
| distribution\_bucket\_name | Bucket for storing the action runner distribution. | `string` | n/a | yes |
53-
| environment | A name that identifies the environment, used as prefix and for tagging. | `string` | n/a | yes |
54-
| lambda\_schedule\_expression | Scheduler expression for action runner binary syncer. | `string` | `"cron(27 * * * ? *)"` | no |
55-
| lambda\_timeout | Time out of the lambda in seconds. | `number` | `300` | no |
56-
| lambda\_zip | File location of the lambda zip file. | `string` | `null` | no |
57-
| role\_path | The path that will be added to the role, if not set the environment name will be used. | `string` | `null` | no |
58-
| role\_permissions\_boundary | Permissions boundary that will be added to the created role for the lambda. | `string` | `null` | no |
59-
| tags | Map of tags that will be added to created resources. By default resources will be tagged with name and environment. | `map(string)` | `{}` | no |
49+
| Name | Description | Type | Default | Required |
50+
| ---------------------------- | ------------------------------------------------------------------------------------------------------------------- | ------------- | ---------------------- | :------: |
51+
| aws\_region | AWS region. | `string` | n/a | yes |
52+
| distribution\_bucket\_name | Bucket for storing the action runner distribution. | `string` | n/a | yes |
53+
| environment | A name that identifies the environment, used as prefix and for tagging. | `string` | n/a | yes |
54+
| lambda\_schedule\_expression | Scheduler expression for action runner binary syncer. | `string` | `"cron(27 * * * ? *)"` | no |
55+
| lambda\_timeout | Time out of the lambda in seconds. | `number` | `300` | no |
56+
| lambda\_zip | File location of the lambda zip file. | `string` | `null` | no |
57+
| role\_path | The path that will be added to the role, if not set the environment name will be used. | `string` | `null` | no |
58+
| role\_permissions\_boundary | Permissions boundary that will be added to the created role for the lambda. | `string` | `null` | no |
59+
| tags | Map of tags that will be added to created resources. By default resources will be tagged with name and environment. | `map(string)` | `{}` | no |
6060

6161
## Outputs
6262

63-
| Name | Description |
64-
|------|-------------|
65-
| bucket | n/a |
66-
| lambda | n/a |
67-
| lambda\_role | n/a |
68-
| runner\_distribution\_object\_key | n/a |
63+
| Name | Description |
64+
| --------------------------------- | ----------- |
65+
| bucket | n/a |
66+
| lambda | n/a |
67+
| lambda\_role | n/a |
68+
| runner\_distribution\_object\_key | n/a |
6969

7070
<!-- END OF PRE-COMMIT-TERRAFORM DOCS HOOK -->
7171

modules/runner-binaries-syncer/runner-binaries-syncer.tf

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ resource "aws_lambda_function" "syncer" {
1212
runtime = "nodejs12.x"
1313
timeout = var.lambda_timeout
1414

15-
1615
environment {
1716
variables = {
1817
S3_BUCKET_NAME = aws_s3_bucket.action_dist.id
@@ -77,3 +76,36 @@ resource "aws_lambda_permission" "syncer" {
7776
source_arn = aws_cloudwatch_event_rule.syncer.arn
7877
}
7978

79+
###################################################################################
80+
### Extra trigger to trigger from S3 to execute the lambda after first deployment
81+
###################################################################################
82+
83+
resource "aws_s3_bucket_object" "trigger" {
84+
bucket = aws_s3_bucket.action_dist.id
85+
key = "triggers/${aws_lambda_function.syncer.id}-trigger.json"
86+
source = "${path.module}/trigger.json"
87+
etag = filemd5("${path.module}/trigger.json")
88+
89+
depends_on = [aws_s3_bucket_notification.on_deploy]
90+
}
91+
92+
resource "aws_s3_bucket_notification" "on_deploy" {
93+
bucket = aws_s3_bucket.action_dist.id
94+
95+
lambda_function {
96+
lambda_function_arn = aws_lambda_function.syncer.arn
97+
events = ["s3:ObjectCreated:*"]
98+
filter_prefix = "triggers/"
99+
filter_suffix = ".json"
100+
}
101+
102+
depends_on = [aws_lambda_permission.on_deploy]
103+
}
104+
105+
resource "aws_lambda_permission" "on_deploy" {
106+
statement_id = "AllowExecutionFromS3Bucket"
107+
action = "lambda:InvokeFunction"
108+
function_name = aws_lambda_function.syncer.arn
109+
principal = "s3.amazonaws.com"
110+
source_arn = aws_s3_bucket.action_dist.arn
111+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"key": "value"
3+
}

0 commit comments

Comments
 (0)