Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ AWS_FUNCTION_VERSION // (default: '')
AWS_VPC_SUBNETS // (default: '')
AWS_VPC_SECURITY_GROUPS // (default: '')
AWS_TRACING_CONFIG // (default: '')
AWS_LOGS_RETENTION_IN_DAYS // (default: '')
EVENT_FILE // (default: 'event.json')
PACKAGE_DIRECTORY // (default: not set)
CONTEXT_FILE // (default: 'context.json')
Expand Down Expand Up @@ -164,6 +165,7 @@ $ node-lambda deploy --help
-g, --vpcSecurityGroups [] VPC Security Group ID(s, comma separated list) for your Lambda Function, when using this, the above param is also required
-Q, --deadLetterConfigTargetArn [] Lambda DLQ resource
-T, --tracingConfig [] Lambda tracing settings
-R, --retentionInDays [] CloudWatchLogs retentionInDays settings
-A, --packageDirectory [] Local package directory
-I, --dockerImage [] Docker image for npm install
-S, --eventSourceFile [event_sources.json] Path to file holding event source mapping variables (e.g. "event_sources.json")
Expand Down
3 changes: 3 additions & 0 deletions bin/node-lambda
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ const AWS_FUNCTION_VERSION = process.env.AWS_FUNCTION_VERSION || ''
const AWS_VPC_SUBNETS = process.env.AWS_VPC_SUBNETS || ''
const AWS_VPC_SECURITY_GROUPS = process.env.AWS_VPC_SECURITY_GROUPS || ''
const AWS_TRACING_CONFIG = process.env.AWS_TRACING_CONFIG || ''
const AWS_LOGS_RETENTION_IN_DAYS = process.env.AWS_LOGS_RETENTION_IN_DAYS || ''
const EVENT_FILE = process.env.EVENT_FILE || 'event.json'
const PACKAGE_DIRECTORY = process.env.PACKAGE_DIRECTORY
const CONTEXT_FILE = process.env.CONTEXT_FILE || 'context.json'
Expand Down Expand Up @@ -82,6 +83,8 @@ program
AWS_DLQ_TARGET_ARN)
.option('-T, --tracingConfig [' + AWS_TRACING_CONFIG + ']', 'Lambda tracing settings',
AWS_TRACING_CONFIG)
.option('-R, --retentionInDays [' + AWS_LOGS_RETENTION_IN_DAYS + ']', 'CloudWatchLogs retentionInDays settings',
AWS_LOGS_RETENTION_IN_DAYS)
.option('-A, --packageDirectory [' + PACKAGE_DIRECTORY + ']', 'Local Package Directory', PACKAGE_DIRECTORY)
.option('-G, --sourceDirectory [' + SRC_DIRECTORY + ']', 'Path to lambda source Directory (e.g. "./some-lambda")', SRC_DIRECTORY)
.option('-I, --dockerImage [' + DOCKER_IMAGE + ']', 'Docker image for npm install', DOCKER_IMAGE)
Expand Down
1 change: 1 addition & 0 deletions lib/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,6 @@ AWS_RUNTIME=nodejs6.10
AWS_VPC_SUBNETS=
AWS_VPC_SECURITY_GROUPS=
AWS_TRACING_CONFIG=
AWS_LOGS_RETENTION_IN_DAYS=
EXCLUDE_GLOBS="event.json"
PACKAGE_DIRECTORY=build
24 changes: 24 additions & 0 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const archiver = require('archiver')
const dotenv = require('dotenv')
const proxy = require('proxy-agent')
const ScheduleEvents = require(path.join(__dirname, 'schedule_events'))
const CloudWatchLogs = require(path.join(__dirname, 'cloudwatch_logs'))

const maxBufferSize = 50 * 1024 * 1024

Expand Down Expand Up @@ -693,6 +694,18 @@ Lambda.prototype._updateScheduleEvents = (scheduleEvents, functionArn, scheduleL
})
}

Lambda.prototype._setLogsRetentionPolicy = (cloudWatchLogs, program, functionName) => {
const days = parseInt(program.retentionInDays)
if (!Number.isInteger(days)) return Promise.resolve({})
return cloudWatchLogs.setLogsRetentionPolicy({
FunctionName: functionName,
retentionInDays: days
}).then(() => {
// Since it is similar to _updateScheduleEvents, it returns meaningful values
return { retentionInDays: days }
})
}

Lambda.prototype.package = function (program) {
const _this = this
if (!program.packageDirectory) {
Expand Down Expand Up @@ -763,6 +776,7 @@ Lambda.prototype._deployToRegion = function (program, params, region) {

const lambda = new aws.Lambda({ apiVersion: '2015-03-31' })
const scheduleEvents = new ScheduleEvents(aws)
const cloudWatchLogs = new CloudWatchLogs(aws)

// Checking function
return lambda.getFunction({
Expand All @@ -787,6 +801,11 @@ Lambda.prototype._deployToRegion = function (program, params, region) {
params.FunctionName,
existingEventSourceList,
eventSourceList.EventSourceMappings
),
_this._setLogsRetentionPolicy(
cloudWatchLogs,
program,
params.FunctionName
)
]).then((results) => {
resolve(results)
Expand All @@ -811,6 +830,11 @@ Lambda.prototype._deployToRegion = function (program, params, region) {
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
),
_this._setLogsRetentionPolicy(
cloudWatchLogs,
program,
params.FunctionName
)
]).then((results) => {
resolve(results)
Expand Down
22 changes: 22 additions & 0 deletions test/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const originalProgram = {
runtime: 'nodejs6.10',
deadLetterConfigTargetArn: '',
tracingConfig: '',
retentionInDays: 30,
region: 'us-east-1,us-west-2,eu-west-1',
eventFile: 'event.json',
eventSourceFile: '',
Expand Down Expand Up @@ -90,6 +91,12 @@ const _mockSetting = () => {
awsMock.mock('CloudWatchEvents', 'putTargets', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchLogs', 'createLogGroup', (params, callback) => {
callback(null, {})
})
awsMock.mock('CloudWatchLogs', 'putRetentionPolicy', (params, callback) => {
callback(null, {})
})

Object.keys(lambdaMockSettings).forEach((method) => {
awsMock.mock('Lambda', method, (params, callback) => {
Expand All @@ -102,6 +109,7 @@ const _mockSetting = () => {

const _awsRestore = () => {
awsMock.restore('CloudWatchEvents')
awsMock.restore('CloudWatchLogs')
awsMock.restore('Lambda')
}

Expand Down Expand Up @@ -995,6 +1003,20 @@ describe('lib/main', function () {
})
})

describe('_setLogsRetentionPolicy', () => {
const CloudWatchLogs = require(path.join('..', 'lib', 'cloudwatch_logs'))
it('simple test with mock', () => {
const params = lambda._params(program, null)
return lambda._setLogsRetentionPolicy(
new CloudWatchLogs(aws),
program,
params.FunctionName
).then((results) => {
assert.deepEqual(results, { retentionInDays: program.retentionInDays })
})
})
})

describe('check env vars before create sample files', () => {
const filesCreatedBySetup = [
'.env',
Expand Down