Skip to content

Commit 7b8c54e

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 7b8c54e

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
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: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +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)
85
}
96

7+
data "aws_arn" "role" {
8+
arn = var.lambda.lambda_function.role
9+
}
10+
11+
data "aws_arn" "function" {
12+
arn = var.lambda.lambda_function.arn
13+
}
14+
1015
resource "aws_iam_policy" "this" {
1116
name_prefix = local.iam_name_prefix
1217
policy = jsonencode({
@@ -23,13 +28,13 @@ resource "aws_iam_policy" "this" {
2328
}
2429

2530
resource "aws_iam_role_policy_attachment" "this" {
26-
role = local.role_name
31+
role = trimprefix(data.aws_arn.role.resource, "role/")
2732
policy_arn = aws_iam_policy.this.arn
2833
}
2934

3035
resource "aws_cloudwatch_event_rule" "trigger" {
3136
name_prefix = var.eventbridge_name_prefix
32-
description = "Periodically trigger Observe lambda to snapshot AWS API"
37+
description = "Periodically trigger Observe Lambda to snapshot AWS API"
3338
schedule_expression = var.eventbridge_schedule_expression
3439
event_bus_name = var.eventbridge_schedule_event_bus_name
3540
}
@@ -50,6 +55,6 @@ resource "aws_lambda_permission" "this" {
5055
statement_id_prefix = local.statement_id_prefix
5156
action = "lambda:InvokeFunction"
5257
principal = "events.amazonaws.com"
53-
function_name = local.function_name
58+
function_name = trimprefix(data.aws_arn.function.resource, "function:")
5459
source_arn = aws_cloudwatch_event_rule.trigger.arn
5560
}

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)