provider/aws: Update Lambda functions on name change (#7081)

Allows the updating of Lambda functions on name change alone
This commit is contained in:
Clint 2016-06-09 16:44:10 -05:00
parent 253a46b573
commit 5e26cb9960
2 changed files with 86 additions and 5 deletions

View File

@ -297,10 +297,11 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
} }
codeUpdate := false codeUpdate := false
if v, ok := d.GetOk("filename"); ok && d.HasChange("source_code_hash") { if d.HasChange("filename") || d.HasChange("source_code_hash") {
file, err := loadFileContent(v.(string)) name := d.Get("filename").(string)
file, err := loadFileContent(name)
if err != nil { if err != nil {
return fmt.Errorf("Unable to load %q: %s", v.(string), err) return fmt.Errorf("Unable to load %q: %s", name, err)
} }
codeReq.ZipFile = file codeReq.ZipFile = file
codeUpdate = true codeUpdate = true
@ -312,8 +313,8 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
codeUpdate = true codeUpdate = true
} }
log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq)
if codeUpdate { if codeUpdate {
log.Printf("[DEBUG] Send Update Lambda Function Code request: %#v", codeReq)
_, err := conn.UpdateFunctionCode(codeReq) _, err := conn.UpdateFunctionCode(codeReq)
if err != nil { if err != nil {
return fmt.Errorf("Error modifying Lambda Function Code %s: %s", d.Id(), err) return fmt.Errorf("Error modifying Lambda Function Code %s: %s", d.Id(), err)
@ -352,8 +353,8 @@ func resourceAwsLambdaFunctionUpdate(d *schema.ResourceData, meta interface{}) e
configUpdate = true configUpdate = true
} }
log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq)
if configUpdate { if configUpdate {
log.Printf("[DEBUG] Send Update Lambda Function Configuration request: %#v", configReq)
_, err := conn.UpdateFunctionConfiguration(configReq) _, err := conn.UpdateFunctionConfiguration(configReq)
if err != nil { if err != nil {
return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err) return fmt.Errorf("Error modifying Lambda Function Configuration %s: %s", d.Id(), err)

View File

@ -129,6 +129,54 @@ func TestAccAWSLambdaFunction_localUpdate(t *testing.T) {
}) })
} }
func TestAccAWSLambdaFunction_localUpdate_nameOnly(t *testing.T) {
var conf lambda.GetFunctionOutput
path, zipFile, err := createTempFile("lambda_localUpdate")
if err != nil {
t.Fatal(err)
}
defer os.Remove(path)
updatedPath, updatedZipFile, err := createTempFile("lambda_localUpdate_name_change")
if err != nil {
t.Fatal(err)
}
defer os.Remove(updatedPath)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
resource.TestStep{
PreConfig: func() {
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func.js": "lambda.js"}, zipFile)
},
Config: genAWSLambdaFunctionConfig_local_name_only(path),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"),
testAccCheckAwsLambdaSourceCodeHash(&conf, "un6qF9S9hKvXbWwJ6m2EYaVCWjcr0PCZWiTV3h4zB0I="),
),
},
resource.TestStep{
PreConfig: func() {
testAccCreateZipFromFiles(map[string]string{"test-fixtures/lambda_func_modified.js": "lambda.js"}, updatedZipFile)
},
Config: genAWSLambdaFunctionConfig_local_name_only(updatedPath),
Check: resource.ComposeTestCheckFunc(
testAccCheckAwsLambdaFunctionExists("aws_lambda_function.lambda_function_local", "tf_acc_lambda_name_local", &conf),
testAccCheckAwsLambdaFunctionName(&conf, "tf_acc_lambda_name_local"),
testAccCheckAwsLambdaFunctionArnHasSuffix(&conf, "tf_acc_lambda_name_local"),
testAccCheckAwsLambdaSourceCodeHash(&conf, "Y5Jf4Si63UDy1wKNfPs+U56ZL0NxsieKPt9EwRl4GQM="),
),
},
},
})
}
func TestAccAWSLambdaFunction_s3Update(t *testing.T) { func TestAccAWSLambdaFunction_s3Update(t *testing.T) {
var conf lambda.GetFunctionOutput var conf lambda.GetFunctionOutput
@ -505,6 +553,38 @@ func genAWSLambdaFunctionConfig_local(filePath string) string {
filePath, filePath) filePath, filePath)
} }
func genAWSLambdaFunctionConfig_local_name_only(filePath string) string {
return fmt.Sprintf(testAccAWSLambdaFunctionConfig_local_name_only_tpl,
filePath)
}
const testAccAWSLambdaFunctionConfig_local_name_only_tpl = `
resource "aws_iam_role" "iam_for_lambda" {
name = "iam_for_lambda"
assume_role_policy = <<EOF
{
"Version": "2012-10-17",
"Statement": [
{
"Action": "sts:AssumeRole",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Effect": "Allow",
"Sid": ""
}
]
}
EOF
}
resource "aws_lambda_function" "lambda_function_local" {
filename = "%s"
function_name = "tf_acc_lambda_name_local"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
}
`
const testAccAWSLambdaFunctionConfig_s3_tpl = ` const testAccAWSLambdaFunctionConfig_s3_tpl = `
resource "aws_s3_bucket" "artifacts" { resource "aws_s3_bucket" "artifacts" {
bucket = "%s" bucket = "%s"