Skip to content

Commit

Permalink
Transition to using environmental variables to pass configuration to …
Browse files Browse the repository at this point in the history
…Lambda functions. Modifying the source code directly is an unexpected way to configure AWS Lambdas for production.
  • Loading branch information
woodhull committed Jul 21, 2020
1 parent fd2f49b commit 05b0902
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 30 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ const AWS = require('aws-sdk')
AWS.config.update({region: process.env.AWS_REGION})

const glue = new AWS.Glue()
const crawlerName = '${glue_crawler_name}'
const crawlerName = process.env.GLUE_CRAWLER_NAME

exports.handler = async (event, context) => {
const { body } = event.Records[0]
const parsed_body = JSON.parse(body)

if (parsed_body.table !== 'signatures' || parsed_body.kind !== 'full') {
const logMessage = `Ignoring notification for table $${parsed_body.table} and kind $${parsed_body.kind}`
const logMessage = `Ignoring notification for table ${parsed_body.table} and kind ${parsed_body.kind}`

console.log(logMessage)
return {
Expand Down
2 changes: 1 addition & 1 deletion templates/run-glue-job.js.tpl → lambdas/run-glue-job.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const AWS = require('aws-sdk')
AWS.config.update({region: process.env.AWS_REGION})

const glue = new AWS.Glue()
const jobName = '${glue_job_name}'
const jobName = process.env.GLUE_JOB_NAME

exports.handler = async function (event, context) {
return new Promise((resolve, reject) => {
Expand Down
22 changes: 8 additions & 14 deletions run_glue_crawler.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
data "template_file" "run_glue_crawler_script" {
template = file("${path.module}/templates/run-glue-crawler.js.tpl")

vars = {
glue_crawler_name = aws_glue_crawler.signatures_crawler.name
}
}

data "archive_file" "run_glue_crawler_zip" {
type = "zip"

source {
content = "${data.template_file.run_glue_crawler_script.rendered}"
filename = "index.js"
}


source_file = "${path.module}/lambdas/run-glue-crawler.js"
output_path = "${path.module}/lambdas/run-glue-crawler.zip"
}

Expand All @@ -25,6 +13,12 @@ resource "aws_lambda_function" "glue_crawler_lambda" {
runtime = "nodejs12.x"
timeout = 60
source_code_hash = data.archive_file.run_glue_crawler_zip.output_base64sha256

environment {
variables = {
GLUE_CRAWLER_NAME = aws_glue_crawler.signatures_crawler.name
}
}
}

resource "aws_lambda_event_source_mapping" "run_crawler_on_new_data_export_task" {
Expand Down
20 changes: 7 additions & 13 deletions run_glue_job.tf
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
data "template_file" "run_glue_job_script" {
template = file("${path.module}/templates/run-glue-job.js.tpl")

vars = {
glue_job_name = aws_glue_job.signatures_full.id
}
}

data "archive_file" "run_glue_job_zip" {
type = "zip"

source {
content = "${data.template_file.run_glue_job_script.rendered}"
filename = "index.js"
}

source_file = "${path.module}/lambdas/run-glue-job.js"
output_path = "${path.module}/lambdas/run-glue-job.zip"
}

Expand All @@ -25,6 +13,12 @@ resource "aws_lambda_function" "glue_job_lambda" {
runtime = "nodejs12.x"
timeout = 60
source_code_hash = data.archive_file.run_glue_job_zip.output_base64sha256

environment {
variables = {
GLUE_JOB_NAME = aws_glue_job.signatures_full.id
}
}
}

resource "aws_cloudwatch_event_rule" "trigger_glue_job_on_crawler_finished" {
Expand Down

0 comments on commit 05b0902

Please sign in to comment.