Skip to content

Commit 942534f

Browse files
author
João Taveira Araújo
committed
refactor(snapshot): address nits
This commit addresses nits uncovered in #58 which were also applicable to the snapshot module.
1 parent 6defef0 commit 942534f

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

modules/snapshot/README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,14 +74,14 @@ locals = {
7474
]
7575
}
7676
# Collect from all endpoints in subset
77-
module "observe_lambda_snapshot" {
77+
module "observe_lambda_snapshot_a" {
7878
source = "observeinc/lambda/aws//modules/snapshot"
7979
lambda = module.observe_lambda
8080
action = local.partial
8181
}
8282
8383
# Collect from all other endpoints
84-
module "observe_lambda_snapshot" {
84+
module "observe_lambda_snapshot_b" {
8585
source = "observeinc/lambda/aws//modules/snapshot"
8686
lambda = module.observe_lambda
8787
exclude = local.partial
@@ -116,13 +116,15 @@ No modules.
116116
| [aws_iam_policy.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_policy) | resource |
117117
| [aws_iam_role_policy_attachment.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/iam_role_policy_attachment) | resource |
118118
| [aws_lambda_permission.this](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lambda_permission) | resource |
119+
| [aws_arn.function](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/arn) | data source |
120+
| [aws_arn.role](https://registry.terraform.io/providers/hashicorp/aws/latest/docs/data-sources/arn) | data source |
119121

120122
## Inputs
121123

122124
| Name | Description | Type | Default | Required |
123125
|------|-------------|------|---------|:--------:|
124126
| <a name="input_action"></a> [action](#input\_action) | List of actions allowed by policy and periodically triggered. By default,<br>this list contains all policies which the lambda can act upon. You should<br>only override this list if you do not want to execute more actions as they<br>become available in future lambda upgrades. If you instead wish to extend<br>this list, or ignore a subset of actions, use \"include\" and \"exclude\". | `list(string)` | <pre>[<br> "apigateway:Get*",<br> "autoscaling:Describe*",<br> "cloudformation:Describe*",<br> "cloudformation:List*",<br> "cloudfront:List*",<br> "dynamodb:Describe*",<br> "dynamodb:List*",<br> "ec2:Describe*",<br> "ecs:Describe*",<br> "ecs:List*",<br> "eks:Describe*",<br> "eks:List*",<br> "elasticbeanstalk:Describe*",<br> "elasticache:Describe*",<br> "elasticfilesystem:Describe*",<br> "elasticloadbalancing:Describe*",<br> "elasticmapreduce:Describe*",<br> "elasticmapreduce:List*",<br> "events:List*",<br> "firehose:Describe*",<br> "firehose:List*",<br> "iam:Get*",<br> "iam:List*",<br> "kinesis:Describe*",<br> "kinesis:List*",<br> "kms:Describe*",<br> "kms:List*",<br> "lambda:List*",<br> "logs:Describe*",<br> "organizations:Describe*",<br> "organizations:List*",<br> "rds:Describe*",<br> "redshift:Describe*",<br> "route53:List*",<br> "s3:GetBucket*",<br> "s3:List*",<br> "secretsmanager:List*",<br> "sns:Get*",<br> "sns:List*",<br> "sqs:Get*",<br> "sqs:List*",<br> "synthetics:Describe*",<br> "synthetics:List*"<br>]</pre> | no |
125-
| <a name="input_eventbridge_name_prefix"></a> [eventbridge\_name\_prefix](#input\_eventbridge\_name\_prefix) | Prefix used for eventbridge rule | `string` | `"observe-lambda-snapshot-"` | no |
127+
| <a name="input_eventbridge_name_prefix"></a> [eventbridge\_name\_prefix](#input\_eventbridge\_name\_prefix) | Prefix used for EventBridge Rule | `string` | `"observe-lambda-snapshot-"` | no |
126128
| <a name="input_eventbridge_schedule_event_bus_name"></a> [eventbridge\_schedule\_event\_bus\_name](#input\_eventbridge\_schedule\_event\_bus\_name) | Event Bus for EventBridge scheduled events | `string` | `"default"` | no |
127129
| <a name="input_eventbridge_schedule_expression"></a> [eventbridge\_schedule\_expression](#input\_eventbridge\_schedule\_expression) | Rate at which snapshot is triggered. Must be valid EventBridge expression | `string` | `"rate(3 hours)"` | no |
128130
| <a name="input_exclude"></a> [exclude](#input\_exclude) | List of actions to exclude from being executed on snapshot request. | `list(string)` | `[]` | no |

modules/snapshot/main.tf

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
locals {
2-
role_name = regex(".*role/(?P<role_name>.*)$", var.lambda.lambda_function.role)["role_name"]
3-
function_name = regex(".*:function:(?P<function_name>.*)$", var.lambda.lambda_function.arn)["function_name"]
4-
52
iam_name_prefix = var.iam_name_prefix != "" ? var.iam_name_prefix : var.eventbridge_name_prefix
63
statement_id_prefix = var.statement_id_prefix != "" ? var.statement_id_prefix : local.iam_name_prefix
74
action = concat(var.action, var.include)
5+
role_resource = split("/", data.aws_arn.role.resource)
6+
role_name = local.role_resource[length(local.role_resource) - 1]
7+
}
8+
9+
data "aws_arn" "role" {
10+
arn = var.lambda.lambda_function.role
11+
}
12+
13+
data "aws_arn" "function" {
14+
arn = var.lambda.lambda_function.arn
815
}
916

1017
resource "aws_iam_policy" "this" {
@@ -29,7 +36,7 @@ resource "aws_iam_role_policy_attachment" "this" {
2936

3037
resource "aws_cloudwatch_event_rule" "trigger" {
3138
name_prefix = var.eventbridge_name_prefix
32-
description = "Periodically trigger Observe lambda to snapshot AWS API"
39+
description = "Periodically trigger Observe Lambda to snapshot AWS API"
3340
schedule_expression = var.eventbridge_schedule_expression
3441
event_bus_name = var.eventbridge_schedule_event_bus_name
3542
}
@@ -50,6 +57,6 @@ resource "aws_lambda_permission" "this" {
5057
statement_id_prefix = local.statement_id_prefix
5158
action = "lambda:InvokeFunction"
5259
principal = "events.amazonaws.com"
53-
function_name = local.function_name
60+
function_name = trimprefix(data.aws_arn.function.resource, "function:")
5461
source_arn = aws_cloudwatch_event_rule.trigger.arn
5562
}

modules/snapshot/variables.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ variable "statement_id_prefix" {
2323
}
2424

2525
variable "eventbridge_name_prefix" {
26-
description = "Prefix used for eventbridge rule"
26+
description = "Prefix used for EventBridge Rule"
2727
type = string
2828
nullable = false
2929
default = "observe-lambda-snapshot-"

0 commit comments

Comments
 (0)