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 9143ee0..19ebbae 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 @@ -70,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); 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..29ee4ab 100644 --- a/main.tf +++ b/main.tf @@ -1,27 +1,13 @@ -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" } 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