Skip to content
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

Adding support for Lambda function updates #3825

Closed
wants to merge 2 commits into from
Closed
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
148 changes: 135 additions & 13 deletions builtin/providers/aws/resource_aws_lambda_function.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package aws

import (
"crypto/sha256"
"encoding/base64"
"fmt"
"io/ioutil"
"log"
Expand Down Expand Up @@ -58,18 +59,15 @@ func resourceAwsLambdaFunction() *schema.Resource {
"handler": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true, // TODO make this editable
},
"memory_size": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
Default: 128,
ForceNew: true, // TODO make this editable
},
"role": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true, // TODO make this editable
},
"runtime": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -81,7 +79,11 @@ func resourceAwsLambdaFunction() *schema.Resource {
Type: schema.TypeInt,
Optional: true,
Default: 3,
ForceNew: true, // TODO make this editable
},
"update_code": &schema.Schema{
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"arn": &schema.Schema{
Type: schema.TypeString,
Expand All @@ -94,7 +96,10 @@ func resourceAwsLambdaFunction() *schema.Resource {
"source_code_hash": &schema.Schema{
Type: schema.TypeString,
Computed: true,
ForceNew: true,
},
"remote_code_hash": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
},
}
Expand All @@ -112,15 +117,11 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e

var functionCode *lambda.FunctionCode
if v, ok := d.GetOk("filename"); ok {
filename, err := homedir.Expand(v.(string))
zipfile, shaSum, err := loadLocalZipFile(v.(string))
if err != nil {
return err
}
zipfile, err := ioutil.ReadFile(filename)
if err != nil {
return err
}
d.Set("source_code_hash", sha256.Sum256(zipfile))
d.Set("source_code_hash", shaSum)
functionCode = &lambda.FunctionCode{
ZipFile: zipfile,
}
Expand Down Expand Up @@ -206,6 +207,26 @@ func resourceAwsLambdaFunctionRead(d *schema.ResourceData, meta interface{}) err
d.Set("runtime", function.Runtime)
d.Set("timeout", function.Timeout)

// Compare code hashes, and see if an update is required to code. If there
// is, set the "update_code" attribute.

remoteSum, err := decodeBase64(*function.CodeSha256)
if err != nil {
return err
}
_, localSum, err := loadLocalZipFile(d.Get("filename").(string))
if err != nil {
return err
}
d.Set("remote_code_hash", remoteSum)
d.Set("source_code_hash", localSum)

if remoteSum != localSum {
d.Set("update_code", true)
} else {
d.Set("update_code", false)
}

return nil
}

Expand Down Expand Up @@ -233,7 +254,108 @@ func resourceAwsLambdaFunctionDelete(d *schema.ResourceData, meta interface{}) e
// resourceAwsLambdaFunctionUpdate maps to:
// UpdateFunctionCode in the API / SDK
func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) error {
// conn := meta.(*AWSClient).lambdaconn
conn := meta.(*AWSClient).lambdaconn

return nil
d.Partial(true)

codeReq := &lambda.UpdateFunctionCodeInput{
FunctionName: aws.String(d.Id()),
}

codeUpdate := false
if sourceHash, ok := d.GetOk("source_code_hash"); ok {
zipfile, shaSum, err := loadLocalZipFile(d.Get("filename").(string))
if err != nil {
return err
}
if sourceHash != shaSum {
d.SetPartial("filename")
d.SetPartial("source_code_hash")
}
codeReq.ZipFile = zipfile
codeUpdate = true
}
if d.HasChange("s3_bucket") || d.HasChange("s3_key") || d.HasChange("s3_object_version") {
d.SetPartial("s3_bucket")
d.SetPartial("s3_key")
d.SetPartial("s3_object_version")

codeReq.S3Bucket = aws.String(d.Get("s3_bucket").(string))
codeReq.S3Key = aws.String(d.Get("s3_key").(string))
codeReq.S3ObjectVersion = aws.String(d.Get("s3_object_version").(string))
codeUpdate = true
}

log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq)
if codeUpdate {
_, err := conn.UpdateFunctionCode(codeReq)
if err != nil {
return fmt.Errorf("Error modifying Lambda Function Code %s: %s", d.Id(), err)
}
}

configReq := &lambda.UpdateFunctionConfigurationInput{
FunctionName: aws.String(d.Id()),
}

configUpdate := false
if d.HasChange("description") {
d.SetPartial("description")
configReq.Description = aws.String(d.Get("description").(string))
configUpdate = true
}
if d.HasChange("handler") {
d.SetPartial("handler")
configReq.Handler = aws.String(d.Get("handler").(string))
configUpdate = true
}
if d.HasChange("memory_size") {
d.SetPartial("memory_size")
configReq.MemorySize = aws.Int64(int64(d.Get("memory_size").(int)))
configUpdate = true
}
if d.HasChange("role") {
d.SetPartial("role")
configReq.Role = aws.String(d.Get("role").(string))
configUpdate = true
}
if d.HasChange("timeout") {
d.SetPartial("timeout")
configReq.Timeout = aws.Int64(int64(d.Get("timeout").(int)))
configUpdate = true
}

log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq)
if configUpdate {
_, err := conn.UpdateFunctionConfiguration(configReq)
if err != nil {
return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err)
}
}
d.Partial(false)

return resourceAwsLambdaFunctionRead(d, meta)
}

// loads the local ZIP data and the SHA sum of the data.
func loadLocalZipFile(v string) ([]byte, string, error) {
filename, err := homedir.Expand(v)
if err != nil {
return nil, "", err
}
zipfile, err := ioutil.ReadFile(filename)
if err != nil {
return nil, "", err
}
sum := sha256.Sum256(zipfile)
return zipfile, fmt.Sprintf("%x", sum), nil
}

// Decodes a base64 string to a string.
func decodeBase64(s string) (string, error) {
sum, err := base64.StdEncoding.DecodeString(s)
if err != nil {
return "", err
}
return fmt.Sprintf("%x", sum), nil
}