From e3ca0ea3e8cacf742075e2c0204ae3b4183f2cb8 Mon Sep 17 00:00:00 2001 From: Eliran Cohen Date: Tue, 14 Dec 2021 10:30:02 +0200 Subject: [PATCH 1/4] remove chank library --- VpcSubnetIpMonitor/index.js | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/VpcSubnetIpMonitor/index.js b/VpcSubnetIpMonitor/index.js index 9143ee0..a59ea76 100644 --- a/VpcSubnetIpMonitor/index.js +++ b/VpcSubnetIpMonitor/index.js @@ -7,7 +7,13 @@ AWS.config.update({region: process.env.AWS_REGION}); var ec2 = new AWS.EC2(); var cw = new AWS.CloudWatch(); -var chunk = require('chunk'); + +// Create a generator to split the result to chunks of 20 items +function* chunks(arr, n) { + for (let i = 0; i < arr.length; i += n) { + yield arr.slice(i, i + n); + } +} /** * Checks all subnets in an account for IP address availability, and reports From 454882049e3dc90e6d5f60206e84f3a7ee77de98 Mon Sep 17 00:00:00 2001 From: Eliran Cohen Date: Tue, 14 Dec 2021 10:31:42 +0200 Subject: [PATCH 2/4] remove null_resources --- buildspec.yml | 1 - main.tf | 17 +---------------- 2 files changed, 1 insertion(+), 17 deletions(-) diff --git a/buildspec.yml b/buildspec.yml index 7864b4f..cfe6b03 100644 --- a/buildspec.yml +++ b/buildspec.yml @@ -2,7 +2,6 @@ version: 0.1 phases: install: commands: - - npm install chunk - aws cloudformation package --template-file template.yaml --s3-bucket VpcSubnetIpMonitor --output-template-file NewTemplate.yaml artifacts: diff --git a/main.tf b/main.tf index 16ae44f..75f0fb8 100644 --- a/main.tf +++ b/main.tf @@ -1,21 +1,6 @@ -resource "null_resource" "npm_install" { - triggers = { - index = sha256(file("${path.module}/VpcSubnetIpMonitor/index.js")) - } - provisioner "local-exec" { - command = "cd ${path.module}/VpcSubnetIpMonitor && npm install chunk" - } -} - -data "null_data_source" "wait_for_npm_install" { - inputs = { - lambda_dependency_id = null_resource.npm_install.id - source_dir = "${path.module}/VpcSubnetIpMonitor/" - } -} data "archive_file" "lambda_zip" { type = "zip" - source_dir = data.null_data_source.wait_for_npm_install.outputs["source_dir"] + source_dir = "${path.module}/VpcSubnetIpMonitor/" output_path = "${path.module}/lambda_function.zip" } From 6caf4a09d01e902de0da7f6aec358a877a8d1aa7 Mon Sep 17 00:00:00 2001 From: Eliran Cohen Date: Tue, 14 Dec 2021 10:47:25 +0200 Subject: [PATCH 3/4] stip the generator --- .gitignore | 4 +++- VpcSubnetIpMonitor/index.js | 2 +- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index dffdb74..efa8829 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ .application.json -**/node_modules \ No newline at end of file +**/node_modules +.terraform +*.zip diff --git a/VpcSubnetIpMonitor/index.js b/VpcSubnetIpMonitor/index.js index a59ea76..19ebbae 100644 --- a/VpcSubnetIpMonitor/index.js +++ b/VpcSubnetIpMonitor/index.js @@ -76,7 +76,7 @@ exports.handler = (event, context, callback) => { }); // putMetricData can only accept 20 metrics at a time. - chunk(MetricData, 20).forEach(function (MetricChunk) { + [...chunks(MetricData, 20)].forEach(function (MetricChunk) { cw.putMetricData({Namespace: 'VPC', MetricData: MetricChunk}, function(err, data) { if (err) { console.log(err,err.stack); From 920a734693bcb5cd069f28b18ef97c66d4deb0e9 Mon Sep 17 00:00:00 2001 From: Eliran Cohen Date: Tue, 14 Dec 2021 10:48:29 +0200 Subject: [PATCH 4/4] add Lambda function description --- main.tf | 1 + 1 file changed, 1 insertion(+) diff --git a/main.tf b/main.tf index 75f0fb8..29ee4ab 100644 --- a/main.tf +++ b/main.tf @@ -7,6 +7,7 @@ data "archive_file" "lambda_zip" { resource "aws_lambda_function" "monitoring_lambda" { function_name = var.lambda_function_name filename = data.archive_file.lambda_zip.output_path + description = "Publish the IP Address availability in all VPC-based subnets as CloudWatch Metrics." role = aws_iam_role.monitoring_lambda_role.arn handler = "index.handler" source_code_hash = data.archive_file.lambda_zip.output_base64sha256