From 0a1fae86060248353eea2ededad26f43774e500e Mon Sep 17 00:00:00 2001 From: Bryant Biggs Date: Thu, 9 Dec 2021 10:25:31 -0500 Subject: [PATCH] feat: Update lambda module and bump Terraform/AWS provider versions (#151) --- .github/workflows/unit-test.yml | 33 ++++++++++++++ .gitignore | 1 + .pre-commit-config.yaml | 3 +- README.md | 10 ++-- examples/cloudwatch-alerts-to-slack/README.md | 12 ++--- examples/cloudwatch-alerts-to-slack/main.tf | 2 +- .../cloudwatch-alerts-to-slack/outputs.tf | 4 +- .../cloudwatch-alerts-to-slack/versions.tf | 12 +++-- examples/notify-slack-simple/README.md | 8 ++-- examples/notify-slack-simple/outputs.tf | 4 +- examples/notify-slack-simple/versions.tf | 7 ++- functions/notify_slack.zip | Bin 1555 -> 0 bytes functions/pytest.ini.sample | 1 - main.tf | 43 +++++++++--------- outputs.tf | 14 +++--- versions.tf | 7 ++- 16 files changed, 103 insertions(+), 58 deletions(-) create mode 100644 .github/workflows/unit-test.yml delete mode 100644 functions/notify_slack.zip diff --git a/.github/workflows/unit-test.yml b/.github/workflows/unit-test.yml new file mode 100644 index 00000000..c1188980 --- /dev/null +++ b/.github/workflows/unit-test.yml @@ -0,0 +1,33 @@ +name: Unit Test + +on: + pull_request: + branches: + - main + - master + paths: + - 'functions/**' + - '.github/workflows/unit-test.yml' + +defaults: + run: + working-directory: functions + +jobs: + test: + name: Execute unit tests + runs-on: ubuntu-latest + + steps: + - name: Checkout + uses: actions/checkout@v2 + + - name: Set up Python 3.8 + uses: actions/setup-python@v2 + with: + python-version: 3.8 + + - name: Install pipenv + run: | + python -m pip install --upgrade pip + python -m pip install pipenv diff --git a/.gitignore b/.gitignore index abdc9d29..927c2793 100644 --- a/.gitignore +++ b/.gitignore @@ -32,3 +32,4 @@ terraform.rc builds/ __pycache__/ functions/pytest.ini +*.zip diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index b0e03fc8..1ada986a 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -1,6 +1,6 @@ repos: - repo: https://github.com/antonbabenko/pre-commit-terraform - rev: v1.58.0 + rev: v1.60.0 hooks: - id: terraform_fmt - id: terraform_validate @@ -26,3 +26,4 @@ repos: rev: v4.0.1 hooks: - id: check-merge-conflict + - id: end-of-file-fixer diff --git a/README.md b/README.md index 89a9214d..6d3e4a13 100644 --- a/README.md +++ b/README.md @@ -80,20 +80,20 @@ To run the tests: | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 0.13.0 | -| [aws](#requirement\_aws) | >= 2.35 | +| [terraform](#requirement\_terraform) | >= 0.13.1 | +| [aws](#requirement\_aws) | >= 3.61 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 2.35 | +| [aws](#provider\_aws) | >= 3.61 | ## Modules | Name | Source | Version | |------|--------|---------| -| [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | 1.47.0 | +| [lambda](#module\_lambda) | terraform-aws-modules/lambda/aws | 2.27.1 | ## Resources @@ -153,7 +153,7 @@ To run the tests: | [notify\_slack\_lambda\_function\_last\_modified](#output\_notify\_slack\_lambda\_function\_last\_modified) | The date Lambda function was last modified | | [notify\_slack\_lambda\_function\_name](#output\_notify\_slack\_lambda\_function\_name) | The name of the Lambda function | | [notify\_slack\_lambda\_function\_version](#output\_notify\_slack\_lambda\_function\_version) | Latest published version of your Lambda function | -| [this\_slack\_topic\_arn](#output\_this\_slack\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack | +| [slack\_topic\_arn](#output\_slack\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack | ## Authors diff --git a/examples/cloudwatch-alerts-to-slack/README.md b/examples/cloudwatch-alerts-to-slack/README.md index ec752a2c..694d4efe 100644 --- a/examples/cloudwatch-alerts-to-slack/README.md +++ b/examples/cloudwatch-alerts-to-slack/README.md @@ -60,16 +60,16 @@ Note that this example may create resources which can cost money. Run `terraform | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 0.13.0 | -| [aws](#requirement\_aws) | >= 2.35 | -| [random](#requirement\_random) | >= 2 | +| [terraform](#requirement\_terraform) | >= 0.13.1 | +| [aws](#requirement\_aws) | >= 3.61 | +| [random](#requirement\_random) | >= 2.0 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 2.35 | -| [random](#provider\_random) | >= 2 | +| [aws](#provider\_aws) | >= 3.61 | +| [random](#provider\_random) | >= 2.0 | ## Modules @@ -102,5 +102,5 @@ No inputs. | [notify\_slack\_lambda\_function\_last\_modified](#output\_notify\_slack\_lambda\_function\_last\_modified) | The date Lambda function was last modified | | [notify\_slack\_lambda\_function\_name](#output\_notify\_slack\_lambda\_function\_name) | The name of the Lambda function | | [notify\_slack\_lambda\_function\_version](#output\_notify\_slack\_lambda\_function\_version) | Latest published version of your Lambda function | -| [this\_sns\_topic\_arn](#output\_this\_sns\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack | +| [sns\_topic\_arn](#output\_sns\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack | diff --git a/examples/cloudwatch-alerts-to-slack/main.tf b/examples/cloudwatch-alerts-to-slack/main.tf index 1e189e58..20492999 100644 --- a/examples/cloudwatch-alerts-to-slack/main.tf +++ b/examples/cloudwatch-alerts-to-slack/main.tf @@ -55,7 +55,7 @@ resource "aws_cloudwatch_metric_alarm" "lambda_duration" { threshold = "5000" alarm_description = "Duration of notifying slack exceeds threshold" - alarm_actions = [module.notify_slack["develop"].this_slack_topic_arn] + alarm_actions = [module.notify_slack["develop"].slack_topic_arn] dimensions = { FunctionName = module.notify_slack["develop"].notify_slack_lambda_function_name diff --git a/examples/cloudwatch-alerts-to-slack/outputs.tf b/examples/cloudwatch-alerts-to-slack/outputs.tf index 0da0d553..6070a685 100644 --- a/examples/cloudwatch-alerts-to-slack/outputs.tf +++ b/examples/cloudwatch-alerts-to-slack/outputs.tf @@ -1,6 +1,6 @@ -output "this_sns_topic_arn" { +output "sns_topic_arn" { description = "The ARN of the SNS topic from which messages will be sent to Slack" - value = module.notify_slack["develop"].this_slack_topic_arn + value = module.notify_slack["develop"].slack_topic_arn } output "lambda_iam_role_arn" { diff --git a/examples/cloudwatch-alerts-to-slack/versions.tf b/examples/cloudwatch-alerts-to-slack/versions.tf index c06e090a..2a88692a 100644 --- a/examples/cloudwatch-alerts-to-slack/versions.tf +++ b/examples/cloudwatch-alerts-to-slack/versions.tf @@ -1,8 +1,14 @@ terraform { - required_version = ">= 0.13.0" + required_version = ">= 0.13.1" required_providers { - aws = ">= 2.35" - random = ">= 2" + aws = { + source = "hashicorp/aws" + version = ">= 3.61" + } + random = { + source = "hashicorp/random" + version = ">= 2.0" + } } } diff --git a/examples/notify-slack-simple/README.md b/examples/notify-slack-simple/README.md index f8be76af..62f0deb8 100644 --- a/examples/notify-slack-simple/README.md +++ b/examples/notify-slack-simple/README.md @@ -23,14 +23,14 @@ Note that this example may create resources which can cost money (AWS Elastic IP | Name | Version | |------|---------| -| [terraform](#requirement\_terraform) | >= 0.13.0 | -| [aws](#requirement\_aws) | >= 2.35 | +| [terraform](#requirement\_terraform) | >= 0.13.1 | +| [aws](#requirement\_aws) | >= 3.61 | ## Providers | Name | Version | |------|---------| -| [aws](#provider\_aws) | >= 2.35 | +| [aws](#provider\_aws) | >= 3.61 | ## Modules @@ -60,5 +60,5 @@ No inputs. | [notify\_slack\_lambda\_function\_last\_modified](#output\_notify\_slack\_lambda\_function\_last\_modified) | The date Lambda function was last modified | | [notify\_slack\_lambda\_function\_name](#output\_notify\_slack\_lambda\_function\_name) | The name of the Lambda function | | [notify\_slack\_lambda\_function\_version](#output\_notify\_slack\_lambda\_function\_version) | Latest published version of your Lambda function | -| [this\_sns\_topic\_arn](#output\_this\_sns\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack | +| [sns\_topic\_arn](#output\_sns\_topic\_arn) | The ARN of the SNS topic from which messages will be sent to Slack | diff --git a/examples/notify-slack-simple/outputs.tf b/examples/notify-slack-simple/outputs.tf index e75e57ff..05ef0c9a 100644 --- a/examples/notify-slack-simple/outputs.tf +++ b/examples/notify-slack-simple/outputs.tf @@ -1,6 +1,6 @@ -output "this_sns_topic_arn" { +output "sns_topic_arn" { description = "The ARN of the SNS topic from which messages will be sent to Slack" - value = module.notify_slack.this_slack_topic_arn + value = module.notify_slack.slack_topic_arn } output "lambda_iam_role_arn" { diff --git a/examples/notify-slack-simple/versions.tf b/examples/notify-slack-simple/versions.tf index 1b562bbd..4440a44d 100644 --- a/examples/notify-slack-simple/versions.tf +++ b/examples/notify-slack-simple/versions.tf @@ -1,7 +1,10 @@ terraform { - required_version = ">= 0.13.0" + required_version = ">= 0.13.1" required_providers { - aws = ">= 2.35" + aws = { + source = "hashicorp/aws" + version = ">= 3.61" + } } } diff --git a/functions/notify_slack.zip b/functions/notify_slack.zip deleted file mode 100644 index e793f89a59ba52a872bcb361c4265864094d3273..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1555 zcmV+u2JHDzO9KQH00;mG0000XiU0rr000000000001p5F0B&z|X=Zs}b8KN_Yc6nk zj8{=_<2Dk0_pg}aKFC5uU_~|l@^jes7z*!e)Fuhv6;&F84wst^6PsSI_W?d* zuAV9bDBOw#+{`ZFk(n)gy_)w5Ttm^Q^jnpzRy-c`02FSR6fgv-Qxp$ONwJ7VxASZ^ z{_SEqiIzP8qn@lPz+I_f*o2812_ej0MT2i3>m<+suA|dt8kxD z=Zd-ec^$Q*bOe4*xyCoZ6;k~QZph)xsk{?)$_sE-Cn2Z2=w8dal5kclg~Ggn`s}h_ zNA)Xo&@Y*|v(@ghL6U<;@Ito6RC@67V=jf398>zJQ(DshNs;EV{CIc?AGfl^|1^a^ zB=AEZk{^J4pca1M{+=^#Pz^_u|Nh%?gVpTvS$bbfgKyQKi|^HeRISn7FKp zW!q=HV179|`(~c< z()rn!(PT1i9D7@h%GT3K^_%&4HW^)wJNj74Z|tP<_-gw5MYObmRP>2UT3GJ(eA~oi zFS{F~FNzRDyB!36pXE|Cj#~})<+rIZG~bpej4LdEv2e03v{6kq+fEGa#Sfe6vXLLE z%jx(w=(c~KPbZ&Uhjfz&Pz|V5zGm)LGP<3^nZ>xZ7{};8g5{JzAf2F=$7r2=N6VB}6^f$YMRJn^ z*Zu^rMA0=M?{M%wN^&VJsG%@CX2)7_6mG8B5%1`jPP{PQ$EjB&wORl8FA_VJyFUQ> zaWb7X>$6AL7M4O?bMw-yR$Nvn`U>w87i#R)9g%h-+7TND8F-|0gQgkG5+F>htvl-6H`((EdVC^(xlleD+;i;JLm2e!3a$2qmnr*2l zII2EApfCyK(oLR@=-3*f%js{~_|Ng=dLG#aao`%DqBx?Sj^+}ELx^naj*gG$N5g(J zw%&pmP|y{}VgP^b_J2j;)SY9x;NqsFLSq_&lP;ne=28_pS}r~<7tvhUU(vkLa&&oM zmA5Q>i*pw3KM-#jT+52(BPF6`sw>V+pA1O6_^Gkil@!{#d|$Kj^wVLz01+>mD_+7s zhVai%pH9}yxZyH|IkE>V+%R2hmrkg)OR-D16WELM*X#u3DB&->yINW%@T@@M&L|(W zd;bpr0RR6`O9u$a+Sb&A1pok54gdg9O928D0~7!V00;m803nLW+Sb&A1pok54gdfT z00000000000001_fdBvi0B&z|X=Zs}b8KN_Yc6nkP)h{{000000RRC2Jpcdzzy$yR F008so?nM9q diff --git a/functions/pytest.ini.sample b/functions/pytest.ini.sample index 529fc855..2d107355 100644 --- a/functions/pytest.ini.sample +++ b/functions/pytest.ini.sample @@ -5,4 +5,3 @@ env = SLACK_EMOJI=:aws: SLACK_USERNAME=notify_slack_test SLACK_WEBHOOK_URL=https://hooks.slack.com/services/YOUR/WEBOOK/URL - diff --git a/main.tf b/main.tf index 738ffb69..d9043ea2 100644 --- a/main.tf +++ b/main.tf @@ -2,31 +2,18 @@ data "aws_caller_identity" "current" {} data "aws_partition" "current" {} data "aws_region" "current" {} -resource "aws_sns_topic" "this" { - count = var.create_sns_topic && var.create ? 1 : 0 - - name = var.sns_topic_name - - kms_master_key_id = var.sns_topic_kms_key_id - - tags = merge(var.tags, var.sns_topic_tags) -} - locals { - sns_topic_arn = element( - concat( - aws_sns_topic.this.*.arn, - ["arn:${data.aws_partition.current.id}:sns:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.sns_topic_name}"], - [""] - ), - 0, + sns_topic_arn = try( + aws_sns_topic.this[0].arn, + "arn:${data.aws_partition.current.id}:sns:${data.aws_region.current.name}:${data.aws_caller_identity.current.account_id}:${var.sns_topic_name}", + "" ) lambda_policy_document = { sid = "AllowWriteToCloudwatchLogs" effect = "Allow" actions = ["logs:CreateLogStream", "logs:PutLogEvents"] - resources = [replace("${element(concat(aws_cloudwatch_log_group.lambda[*].arn, [""]), 0)}:*", ":*:*", ":*")] + resources = [replace("${try(aws_cloudwatch_log_group.lambda[0].arn, "")}:*", ":*:*", ":*")] } lambda_policy_document_kms = { @@ -61,18 +48,29 @@ resource "aws_cloudwatch_log_group" "lambda" { tags = merge(var.tags, var.cloudwatch_log_group_tags) } +resource "aws_sns_topic" "this" { + count = var.create_sns_topic && var.create ? 1 : 0 + + name = var.sns_topic_name + + kms_master_key_id = var.sns_topic_kms_key_id + + tags = merge(var.tags, var.sns_topic_tags) +} + + resource "aws_sns_topic_subscription" "sns_notify_slack" { count = var.create ? 1 : 0 topic_arn = local.sns_topic_arn protocol = "lambda" - endpoint = module.lambda.this_lambda_function_arn + endpoint = module.lambda.lambda_function_arn filter_policy = var.subscription_filter_policy } module "lambda" { source = "terraform-aws-modules/lambda/aws" - version = "1.47.0" + version = "2.27.1" create = var.create @@ -86,7 +84,8 @@ module "lambda" { kms_key_arn = var.kms_key_arn reserved_concurrent_executions = var.reserved_concurrent_executions - # If publish is disabled, there will be "Error adding new Lambda Permission for notify_slack: InvalidParameterValueException: We currently do not support adding policies for $LATEST." + # If publish is disabled, there will be "Error adding new Lambda Permission for notify_slack: + # InvalidParameterValueException: We currently do not support adding policies for $LATEST." publish = true environment_variables = { @@ -109,7 +108,7 @@ module "lambda" { # the value of presense of KMS. Famous "computed values in count" bug... attach_cloudwatch_logs_policy = false attach_policy_json = true - policy_json = element(concat(data.aws_iam_policy_document.lambda[*].json, [""]), 0) + policy_json = try(data.aws_iam_policy_document.lambda[0].json, "") use_existing_cloudwatch_log_group = true attach_network_policy = var.lambda_function_vpc_subnet_ids != null diff --git a/outputs.tf b/outputs.tf index f9d3e2b7..000ef549 100644 --- a/outputs.tf +++ b/outputs.tf @@ -1,4 +1,4 @@ -output "this_slack_topic_arn" { +output "slack_topic_arn" { description = "The ARN of the SNS topic from which messages will be sent to Slack" value = local.sns_topic_arn } @@ -15,30 +15,30 @@ output "lambda_iam_role_name" { output "notify_slack_lambda_function_arn" { description = "The ARN of the Lambda function" - value = module.lambda.this_lambda_function_arn + value = module.lambda.lambda_function_arn } output "notify_slack_lambda_function_name" { description = "The name of the Lambda function" - value = module.lambda.this_lambda_function_name + value = module.lambda.lambda_function_name } output "notify_slack_lambda_function_invoke_arn" { description = "The ARN to be used for invoking Lambda function from API Gateway" - value = module.lambda.this_lambda_function_invoke_arn + value = module.lambda.lambda_function_invoke_arn } output "notify_slack_lambda_function_last_modified" { description = "The date Lambda function was last modified" - value = module.lambda.this_lambda_function_last_modified + value = module.lambda.lambda_function_last_modified } output "notify_slack_lambda_function_version" { description = "Latest published version of your Lambda function" - value = module.lambda.this_lambda_function_version + value = module.lambda.lambda_function_version } output "lambda_cloudwatch_log_group_arn" { description = "The Amazon Resource Name (ARN) specifying the log group" - value = element(concat(aws_cloudwatch_log_group.lambda.*.arn, [""]), 0) + value = try(aws_cloudwatch_log_group.lambda[0].arn, "") } diff --git a/versions.tf b/versions.tf index 1b562bbd..4440a44d 100644 --- a/versions.tf +++ b/versions.tf @@ -1,7 +1,10 @@ terraform { - required_version = ">= 0.13.0" + required_version = ">= 0.13.1" required_providers { - aws = ">= 2.35" + aws = { + source = "hashicorp/aws" + version = ">= 3.61" + } } }