provider/aws: Added Lambda function guard when needed attributes are not set (#10663)

This commit is contained in:
Ninir 2016-12-12 16:12:56 +01:00 committed by Paul Stack
parent a01a93067f
commit 048a932abd
2 changed files with 38 additions and 6 deletions

View File

@ -174,24 +174,30 @@ func resourceAwsLambdaFunctionCreate(d *schema.ResourceData, meta interface{}) e
log.Printf("[DEBUG] Creating Lambda Function %s with role %s", functionName, iamRole) log.Printf("[DEBUG] Creating Lambda Function %s with role %s", functionName, iamRole)
filename, hasFilename := d.GetOk("filename")
s3Bucket, bucketOk := d.GetOk("s3_bucket")
s3Key, keyOk := d.GetOk("s3_key")
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")
if !hasFilename && !bucketOk && !keyOk && !versionOk {
return errors.New("filename or s3_* attributes must be set")
}
var functionCode *lambda.FunctionCode var functionCode *lambda.FunctionCode
if v, ok := d.GetOk("filename"); ok { if hasFilename {
// Grab an exclusive lock so that we're only reading one function into // Grab an exclusive lock so that we're only reading one function into
// memory at a time. // memory at a time.
// See https://github.com/hashicorp/terraform/issues/9364 // See https://github.com/hashicorp/terraform/issues/9364
awsMutexKV.Lock(awsMutexLambdaKey) awsMutexKV.Lock(awsMutexLambdaKey)
defer awsMutexKV.Unlock(awsMutexLambdaKey) defer awsMutexKV.Unlock(awsMutexLambdaKey)
file, err := loadFileContent(v.(string)) file, err := loadFileContent(filename.(string))
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", filename.(string), err)
} }
functionCode = &lambda.FunctionCode{ functionCode = &lambda.FunctionCode{
ZipFile: file, ZipFile: file,
} }
} else { } else {
s3Bucket, bucketOk := d.GetOk("s3_bucket")
s3Key, keyOk := d.GetOk("s3_key")
s3ObjectVersion, versionOk := d.GetOk("s3_object_version")
if !bucketOk || !keyOk { if !bucketOk || !keyOk {
return errors.New("s3_bucket and s3_key must all be set while using S3 code source") return errors.New("s3_bucket and s3_key must all be set while using S3 code source")
} }

View File

@ -39,6 +39,22 @@ func TestAccAWSLambdaFunction_basic(t *testing.T) {
}) })
} }
func TestAccAWSLambdaFunction_expectFilenameAndS3Attributes(t *testing.T) {
rName := fmt.Sprintf("tf_test_%s", acctest.RandString(5))
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckLambdaFunctionDestroy,
Steps: []resource.TestStep{
{
Config: testAccAWSLambdaConfigWithoutFilenameAndS3Attributes(rName),
ExpectError: regexp.MustCompile(`filename or s3_\* attributes must be set`),
},
},
})
}
func TestAccAWSLambdaFunction_envVariables(t *testing.T) { func TestAccAWSLambdaFunction_envVariables(t *testing.T) {
var conf lambda.GetFunctionOutput var conf lambda.GetFunctionOutput
@ -625,6 +641,16 @@ resource "aws_lambda_function" "lambda_function_test" {
`, rName) `, rName)
} }
func testAccAWSLambdaConfigWithoutFilenameAndS3Attributes(rName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig+`
resource "aws_lambda_function" "lambda_function_test" {
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
}
`, rName)
}
func testAccAWSLambdaConfigEnvVariables(rName string) string { func testAccAWSLambdaConfigEnvVariables(rName string) string {
return fmt.Sprintf(baseAccAWSLambdaConfig+` return fmt.Sprintf(baseAccAWSLambdaConfig+`
resource "aws_lambda_function" "lambda_function_test" { resource "aws_lambda_function" "lambda_function_test" {