Skip to content

Commit

Permalink
Merge pull request #1797 from mspaulding06/fix-lambda-function-publish
Browse files Browse the repository at this point in the history
Fix Lambda Function Updates When Published
  • Loading branch information
Ninir authored Oct 11, 2017
2 parents 963ac8e + af208c0 commit 2546ecc
Showing 1 changed file with 44 additions and 43 deletions.
87 changes: 44 additions & 43 deletions aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -508,49 +508,6 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
}
d.SetPartial("tags")

if d.HasChange("filename") || d.HasChange("source_code_hash") || d.HasChange("s3_bucket") || d.HasChange("s3_key") || d.HasChange("s3_object_version") {
codeReq := &lambda.UpdateFunctionCodeInput{
FunctionName: aws.String(d.Id()),
Publish: aws.Bool(d.Get("publish").(bool)),
}

if v, ok := d.GetOk("filename"); ok {
// Grab an exclusive lock so that we're only reading one function into
// memory at a time.
// See https://github.com/hashicorp/terraform/issues/9364
awsMutexKV.Lock(awsMutexLambdaKey)
defer awsMutexKV.Unlock(awsMutexLambdaKey)
file, err := loadFileContent(v.(string))
if err != nil {
return fmt.Errorf("Unable to load %q: %s", v.(string), err)
}
codeReq.ZipFile = file
} else {
s3Bucket, _ := d.GetOk("s3_bucket")
s3Key, _ := d.GetOk("s3_key")
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")

codeReq.S3Bucket = aws.String(s3Bucket.(string))
codeReq.S3Key = aws.String(s3Key.(string))
if versionOk {
codeReq.S3ObjectVersion = aws.String(s3ObjectVersion.(string))
}
}

log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq)

_, err := conn.UpdateFunctionCode(codeReq)
if err != nil {
return fmt.Errorf("Error modifying Lambda Function Code %s: %s", d.Id(), err)
}

d.SetPartial("filename")
d.SetPartial("source_code_hash")
d.SetPartial("s3_bucket")
d.SetPartial("s3_key")
d.SetPartial("s3_object_version")
}

configReq := &lambda.UpdateFunctionConfigurationInput{
FunctionName: aws.String(d.Id()),
}
Expand Down Expand Up @@ -666,6 +623,50 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
d.SetPartial("role")
d.SetPartial("timeout")
}

if d.HasChange("filename") || d.HasChange("source_code_hash") || d.HasChange("s3_bucket") || d.HasChange("s3_key") || d.HasChange("s3_object_version") {
codeReq := &lambda.UpdateFunctionCodeInput{
FunctionName: aws.String(d.Id()),
Publish: aws.Bool(d.Get("publish").(bool)),
}

if v, ok := d.GetOk("filename"); ok {
// Grab an exclusive lock so that we're only reading one function into
// memory at a time.
// See https://github.com/hashicorp/terraform/issues/9364
awsMutexKV.Lock(awsMutexLambdaKey)
defer awsMutexKV.Unlock(awsMutexLambdaKey)
file, err := loadFileContent(v.(string))
if err != nil {
return fmt.Errorf("Unable to load %q: %s", v.(string), err)
}
codeReq.ZipFile = file
} else {
s3Bucket, _ := d.GetOk("s3_bucket")
s3Key, _ := d.GetOk("s3_key")
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")

codeReq.S3Bucket = aws.String(s3Bucket.(string))
codeReq.S3Key = aws.String(s3Key.(string))
if versionOk {
codeReq.S3ObjectVersion = aws.String(s3ObjectVersion.(string))
}
}

log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq)

_, err := conn.UpdateFunctionCode(codeReq)
if err != nil {
return fmt.Errorf("Error modifying Lambda Function Code %s: %s", d.Id(), err)
}

d.SetPartial("filename")
d.SetPartial("source_code_hash")
d.SetPartial("s3_bucket")
d.SetPartial("s3_key")
d.SetPartial("s3_object_version")
}

d.Partial(false)

return resourceAwsLambdaFunctionRead(d, meta)
Expand Down

0 comments on commit 2546ecc

Please sign in to comment.