Skip to content

Refactoring _deployToRegion() #784

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
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
189 changes: 91 additions & 98 deletions lib/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -973,115 +973,108 @@ they may not work as expected in the Lambda environment.
!!err.message.match(/^Function not found:/)
}

_deployToRegion (program, params, region, buffer) {
async _deployToRegion (program, params, region, buffer) {
aws.updateConfig(program, region)

console.log('=> Reading event source file to memory')
const eventSourceList = this._eventSourceList(program)

return Promise.resolve().then(() => {
if (this._isUseS3(program)) {
const s3Deploy = new S3Deploy(aws.sdk, region)
return s3Deploy.putPackage(params, region, buffer)
}
return null
}).then((code) => {
if (code != null) params.Code = code
}).then(() => {
if (!this._isUseS3(program)) {
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
} else {
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
}
console.log(params)

const lambda = new aws.sdk.Lambda({
region,
apiVersion: '2015-03-31'
})
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
const s3Events = new S3Events(aws.sdk, region)
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)
if (this._isUseS3(program)) {
const s3Deploy = new S3Deploy(aws.sdk, region)
params.Code = await s3Deploy.putPackage(params, region, buffer)
console.log(`=> Uploading AWS Lambda ${region} with parameters:`)
} else {
console.log(`=> Uploading zip file to AWS Lambda ${region} with parameters:`)
}
console.log(params)
const lambda = new aws.sdk.Lambda({
region,
apiVersion: '2015-03-31'
})
const scheduleEvents = new ScheduleEvents(aws.sdk, region)
const s3Events = new S3Events(aws.sdk, region)
const cloudWatchLogs = new CloudWatchLogs(aws.sdk, region)

// Checking function
return lambda.getFunction({
FunctionName: params.FunctionName
}).promise().then(() => {
// Function exists
return this._listEventSourceMappings(lambda, {
const existsFunction = await (async () => {
try {
await lambda.getFunction({
FunctionName: params.FunctionName
}).then((existingEventSourceList) => {
return Promise.all([
this._uploadExisting(lambda, params).then((results) => {
console.log('=> Done uploading. Results follow: ')
console.log(results)
return results
}).then(results => {
return Promise.all([
this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
),
this._updateS3Events(
s3Events,
results.FunctionArn,
eventSourceList.S3Events
),
this._updateTags(
lambda,
results.FunctionArn,
params.Tags)
])
}),
this._updateEventSources(
lambda,
params.FunctionName,
existingEventSourceList,
eventSourceList.EventSourceMappings
),
this._setLogsRetentionPolicy(
cloudWatchLogs,
program,
params.FunctionName
)
])
})
}).catch((err) => {
}).promise()
return true
} catch (err) {
if (!this._isFunctionDoesNotExist(err)) {
throw err
}
// Function does not exist
return this._uploadNew(lambda, params).then((results) => {
console.log('=> Done uploading. Results follow: ')
console.log(results)

return Promise.all([
this._updateEventSources(
lambda,
params.FunctionName,
[],
eventSourceList.EventSourceMappings
),
this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
),
this._updateS3Events(
s3Events,
results.FunctionArn,
eventSourceList.S3Events
),
this._setLogsRetentionPolicy(
cloudWatchLogs,
program,
params.FunctionName
)
])
})
return false
}
})()

if (existsFunction) {
const existingEventSourceList = await this._listEventSourceMappings(lambda, {
FunctionName: params.FunctionName
})
})
const results = await this._uploadExisting(lambda, params)
console.log('=> Done uploading. Results follow: ')
console.log(results)

return Promise.all([
Promise.all([
this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
),
this._updateS3Events(
s3Events,
results.FunctionArn,
eventSourceList.S3Events
),
this._updateTags(
lambda,
results.FunctionArn,
params.Tags)
]),
this._updateEventSources(
lambda,
params.FunctionName,
existingEventSourceList,
eventSourceList.EventSourceMappings
),
this._setLogsRetentionPolicy(
cloudWatchLogs,
program,
params.FunctionName
)
])
} else {
const results = await this._uploadNew(lambda, params)
console.log('=> Done uploading. Results follow: ')
console.log(results)

return Promise.all([
this._updateEventSources(
lambda,
params.FunctionName,
[],
eventSourceList.EventSourceMappings
),
this._updateScheduleEvents(
scheduleEvents,
results.FunctionArn,
eventSourceList.ScheduleEvents
),
this._updateS3Events(
s3Events,
results.FunctionArn,
eventSourceList.S3Events
),
this._setLogsRetentionPolicy(
cloudWatchLogs,
program,
params.FunctionName
)
])
}
}

_printDeployResults (results, isFirst) {
Expand Down