Skip to content

Commit

Permalink
Merge pull request #2 from next-insurance/remove_chunk_library
Browse files Browse the repository at this point in the history
Remove chunk library
  • Loading branch information
eliran89c authored Dec 14, 2021
2 parents fd05f47 + 920a734 commit 168c8bf
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 20 deletions.
4 changes: 3 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
.application.json
**/node_modules
**/node_modules
.terraform
*.zip
10 changes: 8 additions & 2 deletions VpcSubnetIpMonitor/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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);
Expand Down
1 change: 0 additions & 1 deletion buildspec.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
18 changes: 2 additions & 16 deletions main.tf
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 168c8bf

Please # to comment.