@@ -955,119 +955,113 @@ they may not work as expected in the Lambda environment.
955955 ! ! err . message . match ( / ^ F u n c t i o n n o t f o u n d : / )
956956 }
957957
958- _deployToRegion ( program , params , region , buffer ) {
958+ async _deployToRegion ( program , params , region , buffer ) {
959959 // sdk v3 todo: Migration of aws.updateConfig.
960960 aws . updateConfig ( program , region )
961961
962962 console . log ( '=> Reading event source file to memory' )
963963 const eventSourceList = this . _eventSourceList ( program )
964964
965- return Promise . resolve ( ) . then ( ( ) => {
966- if ( this . _isUseS3 ( program ) ) {
967- const s3Deploy = new S3Deploy ( aws . sdk , region )
968- return s3Deploy . putPackage ( params , region , buffer )
969- }
970- return null
971- } ) . then ( ( code ) => {
972- if ( code != null ) params . Code = code
973- } ) . then ( ( ) => {
974- if ( ! this . _isUseS3 ( program ) ) {
975- console . log ( `=> Uploading zip file to AWS Lambda ${ region } with parameters:` )
976- } else {
977- console . log ( `=> Uploading AWS Lambda ${ region } with parameters:` )
978- }
979- console . log ( params )
965+ if ( this . _isUseS3 ( program ) ) {
966+ const s3Deploy = new S3Deploy ( aws . sdk , region )
967+ params . Code = await s3Deploy . putPackage ( params , region , buffer )
968+ console . log ( `=> Uploading AWS Lambda ${ region } with parameters:` )
969+ } else {
970+ console . log ( `=> Uploading zip file to AWS Lambda ${ region } with parameters:` )
971+ }
972+ console . log ( params )
980973
981- // Migrating to v3.
982- const lambda = new aws . sdk . Lambda ( {
983- region,
984- apiVersion : '2015-03-31'
985- } )
986- const lambdaClient = new LambdaClient ( { region } )
974+ // Migrating to v3.
975+ const lambda = new aws . sdk . Lambda ( {
976+ region,
977+ apiVersion : '2015-03-31'
978+ } )
979+ const lambdaClient = new LambdaClient ( { region } )
987980
988- const scheduleEvents = new ScheduleEvents ( aws . sdk , region )
989- const s3Events = new S3Events ( aws . sdk , region )
990- const cloudWatchLogs = new CloudWatchLogs ( aws . sdk , region )
981+ const scheduleEvents = new ScheduleEvents ( aws . sdk , region )
982+ const s3Events = new S3Events ( aws . sdk , region )
983+ const cloudWatchLogs = new CloudWatchLogs ( aws . sdk , region )
991984
992- // Checking function
993- return lambda . getFunction ( {
994- FunctionName : params . FunctionName
995- } ) . promise ( ) . then ( ( ) => {
996- // Function exists
997- return this . _listEventSourceMappings ( lambdaClient , {
985+ const existsFunction = await ( async ( ) => {
986+ try {
987+ await lambda . getFunction ( {
998988 FunctionName : params . FunctionName
999- } ) . then ( ( existingEventSourceList ) => {
1000- return Promise . all ( [
1001- this . _uploadExisting ( lambdaClient , params ) . then ( ( results ) => {
1002- console . log ( '=> Done uploading. Results follow: ' )
1003- console . log ( results )
1004- return results
1005- } ) . then ( results => {
1006- return Promise . all ( [
1007- this . _updateScheduleEvents (
1008- scheduleEvents ,
1009- results . FunctionArn ,
1010- eventSourceList . ScheduleEvents
1011- ) ,
1012- this . _updateS3Events (
1013- s3Events ,
1014- results . FunctionArn ,
1015- eventSourceList . S3Events
1016- ) ,
1017- this . _updateTags (
1018- lambdaClient ,
1019- results . FunctionArn ,
1020- params . Tags )
1021- ] )
1022- } ) ,
1023- this . _updateEventSources (
1024- lambdaClient ,
1025- params . FunctionName ,
1026- existingEventSourceList ,
1027- eventSourceList . EventSourceMappings
1028- ) ,
1029- this . _setLogsRetentionPolicy (
1030- cloudWatchLogs ,
1031- program ,
1032- params . FunctionName
1033- )
1034- ] )
1035- } )
1036- } ) . catch ( ( err ) => {
989+ } ) . promise ( )
990+ return true
991+ } catch ( err ) {
1037992 if ( ! this . _isFunctionDoesNotExist ( err ) ) {
1038993 throw err
1039994 }
1040- // Function does not exist
1041- return this . _uploadNew ( lambdaClient , params ) . then ( ( results ) => {
1042- console . log ( '=> Done uploading. Results follow: ' )
1043- console . log ( results )
1044-
1045- return Promise . all ( [
1046- this . _updateEventSources (
1047- lambdaClient ,
1048- params . FunctionName ,
1049- [ ] ,
1050- eventSourceList . EventSourceMappings
1051- ) ,
1052- this . _updateScheduleEvents (
1053- scheduleEvents ,
1054- results . FunctionArn ,
1055- eventSourceList . ScheduleEvents
1056- ) ,
1057- this . _updateS3Events (
1058- s3Events ,
1059- results . FunctionArn ,
1060- eventSourceList . S3Events
1061- ) ,
1062- this . _setLogsRetentionPolicy (
1063- cloudWatchLogs ,
1064- program ,
1065- params . FunctionName
1066- )
1067- ] )
1068- } )
995+ return false
996+ }
997+ } ) ( )
998+
999+ if ( existsFunction ) {
1000+ const existingEventSourceList = await this . _listEventSourceMappings ( lambdaClient , {
1001+ FunctionName : params . FunctionName
10691002 } )
1070- } )
1003+ const results = await this . _uploadExisting ( lambdaClient , params )
1004+ console . log ( '=> Done uploading. Results follow: ' )
1005+ console . log ( results )
1006+
1007+ return Promise . all ( [
1008+ Promise . all ( [
1009+ this . _updateScheduleEvents (
1010+ scheduleEvents ,
1011+ results . FunctionArn ,
1012+ eventSourceList . ScheduleEvents
1013+ ) ,
1014+ this . _updateS3Events (
1015+ s3Events ,
1016+ results . FunctionArn ,
1017+ eventSourceList . S3Events
1018+ ) ,
1019+ this . _updateTags (
1020+ lambdaClient ,
1021+ results . FunctionArn ,
1022+ params . Tags )
1023+ ] ) ,
1024+ this . _updateEventSources (
1025+ lambdaClient ,
1026+ params . FunctionName ,
1027+ existingEventSourceList ,
1028+ eventSourceList . EventSourceMappings
1029+ ) ,
1030+ this . _setLogsRetentionPolicy (
1031+ cloudWatchLogs ,
1032+ program ,
1033+ params . FunctionName
1034+ )
1035+ ] )
1036+ } else {
1037+ const results = await this . _uploadNew ( lambdaClient , params )
1038+ console . log ( '=> Done uploading. Results follow: ' )
1039+ console . log ( results )
1040+
1041+ return Promise . all ( [
1042+ this . _updateEventSources (
1043+ lambdaClient ,
1044+ params . FunctionName ,
1045+ [ ] ,
1046+ eventSourceList . EventSourceMappings
1047+ ) ,
1048+ this . _updateScheduleEvents (
1049+ scheduleEvents ,
1050+ results . FunctionArn ,
1051+ eventSourceList . ScheduleEvents
1052+ ) ,
1053+ this . _updateS3Events (
1054+ s3Events ,
1055+ results . FunctionArn ,
1056+ eventSourceList . S3Events
1057+ ) ,
1058+ this . _setLogsRetentionPolicy (
1059+ cloudWatchLogs ,
1060+ program ,
1061+ params . FunctionName
1062+ )
1063+ ] )
1064+ }
10711065 }
10721066
10731067 _printDeployResults ( results , isFirst ) {
0 commit comments