Skip to content

Commit 32f335e

Browse files
committed
refactor: improve Lambda function update process with state checks and wait time
- Add Lambda function state checks and logging - Add wait time for function update using WaitUntilFunctionUpdatedV2WithContext() Signed-off-by: Bo-Yi.Wu <appleboy.tw@gmail.com>
1 parent 0596292 commit 32f335e

1 file changed

Lines changed: 31 additions & 0 deletions

File tree

‎plugin.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,24 @@ func (p Plugin) Exec() error {
225225

226226
svc := lambda.New(sess, config)
227227

228+
// Check Lambda function states
229+
// see https://docs.aws.amazon.com/lambda/latest/dg/functions-states.html
230+
if cfg, err := svc.GetFunctionConfiguration(&lambda.GetFunctionConfigurationInput{
231+
FunctionName: aws.String(p.Config.FunctionName),
232+
}); err == nil {
233+
log.Println("Current State:", aws.StringValue(cfg.State))
234+
if aws.StringValue(cfg.State) != lambda.StateActive {
235+
log.Println("Current State Reason:", aws.StringValue(cfg.StateReason))
236+
log.Println("Current State Reason Code:", aws.StringValue(cfg.StateReasonCode))
237+
}
238+
239+
log.Println("Last Update Status:", aws.StringValue(cfg.LastUpdateStatus))
240+
if aws.StringValue(cfg.LastUpdateStatus) != lambda.LastUpdateStatusSuccessful {
241+
log.Println("Last Update Status Reason:", aws.StringValue(cfg.LastUpdateStatusReason))
242+
log.Println("Last Update Status ReasonCode:", aws.StringValue(cfg.LastUpdateStatusReasonCode))
243+
}
244+
}
245+
228246
if isUpdateConfig {
229247
if err := svc.WaitUntilFunctionUpdatedV2WithContext(
230248
aws.BackgroundContext(),
@@ -268,6 +286,17 @@ func (p Plugin) Exec() error {
268286
p.dump(result)
269287
}
270288

289+
if err := svc.WaitUntilFunctionUpdatedV2WithContext(
290+
aws.BackgroundContext(),
291+
&lambda.GetFunctionInput{
292+
FunctionName: aws.String(p.Config.FunctionName),
293+
},
294+
request.WithWaiterMaxAttempts(p.Config.MaxAttempts),
295+
); err != nil {
296+
log.Println(err.Error())
297+
return err
298+
}
299+
271300
result, err := svc.UpdateFunctionCode(input)
272301
if err != nil {
273302
if aerr, ok := err.(awserr.Error); ok {
@@ -282,6 +311,8 @@ func (p Plugin) Exec() error {
282311
log.Println(lambda.ErrCodeTooManyRequestsException, aerr.Error())
283312
case lambda.ErrCodeCodeStorageExceededException:
284313
log.Println(lambda.ErrCodeCodeStorageExceededException, aerr.Error())
314+
case lambda.ErrCodeResourceConflictException:
315+
log.Println(lambda.ErrCodeResourceConflictException, aerr.Error())
285316
default:
286317
log.Println(aerr.Error())
287318
}

0 commit comments

Comments
 (0)
⌂ 换网址