2015-06-01 18:33:22 +02:00
package aws
import (
2016-02-21 21:54:30 +01:00
"archive/zip"
2015-06-01 18:33:22 +02:00
"fmt"
2016-02-21 21:54:30 +01:00
"io/ioutil"
"os"
"path/filepath"
2016-04-13 15:45:47 +02:00
"regexp"
2016-01-21 01:33:14 +01:00
"strings"
2015-06-01 18:33:22 +02:00
"testing"
2015-06-09 21:12:47 +02:00
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/lambda"
2016-01-21 01:33:14 +01:00
"github.com/hashicorp/terraform/helper/acctest"
2015-06-01 18:33:22 +02:00
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
)
2015-07-21 16:42:02 +02:00
func TestAccAWSLambdaFunction_basic ( t * testing . T ) {
2015-06-01 18:33:22 +02:00
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-05-16 17:31:46 +02:00
2015-06-01 18:33:22 +02:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-19 00:48:18 +01:00
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigBasic ( rName , rSt ) ,
2016-02-18 22:45:32 +01:00
Check : resource . ComposeTestCheckFunc (
2016-05-16 17:31:46 +02:00
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
2016-02-18 22:45:32 +01:00
) ,
} ,
} ,
} )
}
2017-03-01 16:30:39 +01:00
func TestAccAWSLambdaFunction_updateRuntime ( t * testing . T ) {
var conf lambda . GetFunctionOutput
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
Config : testAccAWSLambdaConfigBasic ( rName , rSt ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "runtime" , "nodejs4.3" ) ,
) ,
} ,
{
Config : testAccAWSLambdaConfigBasicUpdateRuntime ( rName , rSt ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "runtime" , "nodejs4.3-edge" ) ,
) ,
} ,
} ,
} )
}
2016-12-12 16:12:56 +01:00
func TestAccAWSLambdaFunction_expectFilenameAndS3Attributes ( t * testing . T ) {
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-12-12 16:12:56 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigWithoutFilenameAndS3Attributes ( rName , rSt ) ,
2016-12-12 16:12:56 +01:00
ExpectError : regexp . MustCompile ( ` filename or s3_\* attributes must be set ` ) ,
} ,
} ,
} )
}
2016-11-19 00:48:18 +01:00
func TestAccAWSLambdaFunction_envVariables ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-11-19 00:48:18 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-23 17:36:16 +01:00
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigBasic ( rName , rSt ) ,
2016-11-23 17:36:16 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
2017-01-24 13:09:29 +01:00
resource . TestCheckNoResourceAttr ( "aws_lambda_function.lambda_function_test" , "environment" ) ,
2016-11-23 17:36:16 +01:00
) ,
} ,
2016-11-19 00:48:18 +01:00
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigEnvVariables ( rName , rSt ) ,
2016-11-19 00:48:18 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "environment.0.variables.foo" , "bar" ) ,
) ,
} ,
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigEnvVariablesModified ( rName , rSt ) ,
2016-11-19 00:48:18 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "environment.0.variables.foo" , "baz" ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "environment.0.variables.foo1" , "bar1" ) ,
) ,
} ,
2016-12-02 14:36:55 +01:00
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigEnvVariablesModifiedWithoutEnvironment ( rName , rSt ) ,
2016-12-02 14:36:55 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
2017-01-24 13:09:29 +01:00
resource . TestCheckNoResourceAttr ( "aws_lambda_function.lambda_function_test" , "environment" ) ,
2016-12-02 14:36:55 +01:00
) ,
} ,
2016-11-19 00:48:18 +01:00
} ,
} )
}
func TestAccAWSLambdaFunction_encryptedEnvVariables ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-11-19 00:48:18 +01:00
keyRegex := regexp . MustCompile ( "^arn:aws:kms:" )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigEncryptedEnvVariables ( rName , rSt ) ,
2016-11-19 00:48:18 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "environment.0.variables.foo" , "bar" ) ,
resource . TestMatchResourceAttr ( "aws_lambda_function.lambda_function_test" , "kms_key_arn" , keyRegex ) ,
) ,
} ,
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigEncryptedEnvVariablesModified ( rName , rSt ) ,
2016-11-19 00:48:18 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "environment.0.variables.foo" , "bar" ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "kms_key_arn" , "" ) ,
) ,
} ,
} ,
} )
}
2016-09-04 10:55:13 +02:00
func TestAccAWSLambdaFunction_versioned ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-09-04 10:55:13 +02:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-19 00:48:18 +01:00
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigVersioned ( rName , rSt ) ,
2016-09-04 10:55:13 +02:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
resource . TestMatchResourceAttr ( "aws_lambda_function.lambda_function_test" , "version" ,
regexp . MustCompile ( "^[0-9]+$" ) ) ,
resource . TestMatchResourceAttr ( "aws_lambda_function.lambda_function_test" , "qualified_arn" ,
regexp . MustCompile ( ":" + rName + ":[0-9]+$" ) ) ,
) ,
} ,
} ,
} )
}
2017-02-23 02:50:10 +01:00
func TestAccAWSLambdaFunction_DeadLetterConfig ( t * testing . T ) {
var conf lambda . GetFunctionOutput
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
Config : testAccAWSLambdaConfigWithDeadLetterConfig ( rName , rSt ) ,
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
func ( s * terraform . State ) error {
if ! strings . HasSuffix ( * conf . Configuration . DeadLetterConfig . TargetArn , ":" + rName ) {
return fmt . Errorf (
"Expected DeadLetterConfig.TargetArn %s to have suffix %s" , * conf . Configuration . DeadLetterConfig . TargetArn , ":" + rName ,
)
}
return nil
} ,
) ,
} ,
} ,
} )
}
2016-02-18 22:45:32 +01:00
func TestAccAWSLambdaFunction_VPC ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-02-18 22:45:32 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-19 00:48:18 +01:00
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigWithVPC ( rName , rSt ) ,
2015-06-01 18:33:22 +02:00
Check : resource . ComposeTestCheckFunc (
2016-05-16 17:31:46 +02:00
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
2016-01-21 01:33:14 +01:00
testAccCheckAWSLambdaFunctionVersion ( & conf , "$LATEST" ) ,
2016-04-13 15:45:47 +02:00
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "vpc_config.#" , "1" ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "vpc_config.0.subnet_ids.#" , "1" ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "vpc_config.0.security_group_ids.#" , "1" ) ,
resource . TestMatchResourceAttr ( "aws_lambda_function.lambda_function_test" , "vpc_config.0.vpc_id" , regexp . MustCompile ( "^vpc-" ) ) ,
2016-01-21 01:33:14 +01:00
) ,
} ,
} ,
} )
}
func TestAccAWSLambdaFunction_s3 ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-01-21 01:33:14 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-19 00:48:18 +01:00
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigS3 ( rName , rSt ) ,
2016-01-21 01:33:14 +01:00
Check : resource . ComposeTestCheckFunc (
2016-05-16 17:31:46 +02:00
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_s3test" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , ":" + rName ) ,
2016-01-21 01:33:14 +01:00
testAccCheckAWSLambdaFunctionVersion ( & conf , "$LATEST" ) ,
2015-06-01 18:33:22 +02:00
) ,
} ,
} ,
} )
}
2016-02-21 21:54:30 +01:00
func TestAccAWSLambdaFunction_localUpdate ( t * testing . T ) {
var conf lambda . GetFunctionOutput
path , zipFile , err := createTempFile ( "lambda_localUpdate" )
if err != nil {
t . Fatal ( err )
}
defer os . Remove ( path )
2017-03-13 19:42:15 +01:00
rInt := acctest . RandInt ( )
2016-02-21 21:54:30 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-19 00:48:18 +01:00
{
2016-02-21 21:54:30 +01:00
PreConfig : func ( ) {
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func.js" : "lambda.js" } , zipFile )
} ,
2017-03-13 19:42:15 +01:00
Config : genAWSLambdaFunctionConfig_local ( path , rInt ) ,
2016-02-21 21:54:30 +01:00
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" ) ,
2016-08-17 16:45:35 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY=" ) ,
2016-02-21 21:54:30 +01:00
) ,
} ,
2016-11-19 00:48:18 +01:00
{
2016-02-21 21:54:30 +01:00
PreConfig : func ( ) {
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func_modified.js" : "lambda.js" } , zipFile )
} ,
2017-03-13 19:42:15 +01:00
Config : genAWSLambdaFunctionConfig_local ( path , rInt ) ,
2016-02-21 21:54:30 +01:00
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" ) ,
2016-08-17 16:45:35 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg=" ) ,
2016-02-21 21:54:30 +01:00
) ,
} ,
} ,
} )
}
2016-06-09 23:44:10 +02:00
func TestAccAWSLambdaFunction_localUpdate_nameOnly ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2017-02-02 16:57:44 +01:00
rName := fmt . Sprintf ( "tf_test_iam_%d" , acctest . RandInt ( ) )
2016-06-09 23:44:10 +02:00
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 {
2016-11-19 00:48:18 +01:00
{
2016-06-09 23:44:10 +02:00
PreConfig : func ( ) {
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func.js" : "lambda.js" } , zipFile )
} ,
2017-02-02 16:57:44 +01:00
Config : genAWSLambdaFunctionConfig_local_name_only ( path , rName ) ,
2016-06-09 23:44:10 +02:00
Check : resource . ComposeTestCheckFunc (
2017-02-02 16:57:44 +01:00
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_local" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , rName ) ,
2016-08-17 16:45:35 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY=" ) ,
2016-06-09 23:44:10 +02:00
) ,
} ,
2016-11-19 00:48:18 +01:00
{
2016-06-09 23:44:10 +02:00
PreConfig : func ( ) {
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func_modified.js" : "lambda.js" } , updatedZipFile )
} ,
2017-02-02 16:57:44 +01:00
Config : genAWSLambdaFunctionConfig_local_name_only ( updatedPath , rName ) ,
2016-06-09 23:44:10 +02:00
Check : resource . ComposeTestCheckFunc (
2017-02-02 16:57:44 +01:00
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_local" , rName , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , rName ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , rName ) ,
2016-08-17 16:45:35 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg=" ) ,
2016-06-09 23:44:10 +02:00
) ,
} ,
} ,
} )
}
2016-02-21 21:54:30 +01:00
func TestAccAWSLambdaFunction_s3Update ( t * testing . T ) {
var conf lambda . GetFunctionOutput
path , zipFile , err := createTempFile ( "lambda_s3Update" )
if err != nil {
t . Fatal ( err )
}
defer os . Remove ( path )
bucketName := fmt . Sprintf ( "tf-acc-lambda-s3-deployments-%d" , randomInteger )
key := "lambda-func.zip"
2017-03-13 19:42:15 +01:00
rInt := acctest . RandInt ( )
2016-02-21 21:54:30 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-19 00:48:18 +01:00
{
2016-02-21 21:54:30 +01:00
PreConfig : func ( ) {
// Upload 1st version
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func.js" : "lambda.js" } , zipFile )
} ,
2017-03-13 19:42:15 +01:00
Config : genAWSLambdaFunctionConfig_s3 ( bucketName , key , path , rInt ) ,
2016-02-21 21:54:30 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_s3" , "tf_acc_lambda_name_s3" , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , "tf_acc_lambda_name_s3" ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , "tf_acc_lambda_name_s3" ) ,
2016-08-17 16:45:35 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY=" ) ,
2016-02-21 21:54:30 +01:00
) ,
} ,
2016-11-19 00:48:18 +01:00
{
2016-02-21 21:54:30 +01:00
ExpectNonEmptyPlan : true ,
PreConfig : func ( ) {
// Upload 2nd version
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func_modified.js" : "lambda.js" } , zipFile )
} ,
2017-03-13 19:42:15 +01:00
Config : genAWSLambdaFunctionConfig_s3 ( bucketName , key , path , rInt ) ,
2016-02-21 21:54:30 +01:00
} ,
// Extra step because of missing ComputedWhen
// See https://github.com/hashicorp/terraform/pull/4846 & https://github.com/hashicorp/terraform/pull/5330
2016-12-16 13:24:45 +01:00
{
2017-03-13 19:42:15 +01:00
Config : genAWSLambdaFunctionConfig_s3 ( bucketName , key , path , rInt ) ,
2016-02-21 21:54:30 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_s3" , "tf_acc_lambda_name_s3" , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , "tf_acc_lambda_name_s3" ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , "tf_acc_lambda_name_s3" ) ,
2016-08-17 16:45:35 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg=" ) ,
2016-02-21 21:54:30 +01:00
) ,
} ,
} ,
} )
}
2016-05-24 23:46:18 +02:00
func TestAccAWSLambdaFunction_s3Update_unversioned ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2017-02-02 16:57:44 +01:00
rName := fmt . Sprintf ( "tf_iam_lambda_%d" , acctest . RandInt ( ) )
2016-05-24 23:46:18 +02:00
path , zipFile , err := createTempFile ( "lambda_s3Update" )
if err != nil {
t . Fatal ( err )
}
defer os . Remove ( path )
bucketName := fmt . Sprintf ( "tf-acc-lambda-s3-deployments-%d" , randomInteger )
key := "lambda-func.zip"
key2 := "lambda-func-modified.zip"
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
2016-11-19 00:48:18 +01:00
{
2016-05-24 23:46:18 +02:00
PreConfig : func ( ) {
// Upload 1st version
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func.js" : "lambda.js" } , zipFile )
} ,
2017-02-02 16:57:44 +01:00
Config : testAccAWSLambdaFunctionConfig_s3_unversioned_tpl ( rName , bucketName , key , path ) ,
2016-05-24 23:46:18 +02:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_s3" , "tf_acc_lambda_name_s3_unversioned" , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , "tf_acc_lambda_name_s3_unversioned" ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , "tf_acc_lambda_name_s3_unversioned" ) ,
2016-09-04 14:10:13 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "8DPiX+G1l2LQ8hjBkwRchQFf1TSCEvPrYGRKlM9UoyY=" ) ,
2016-05-24 23:46:18 +02:00
) ,
} ,
2016-11-19 00:48:18 +01:00
{
2016-05-24 23:46:18 +02:00
PreConfig : func ( ) {
// Upload 2nd version
testAccCreateZipFromFiles ( map [ string ] string { "test-fixtures/lambda_func_modified.js" : "lambda.js" } , zipFile )
} ,
2017-02-02 16:57:44 +01:00
Config : testAccAWSLambdaFunctionConfig_s3_unversioned_tpl ( rName , bucketName , key2 , path ) ,
2016-05-24 23:46:18 +02:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_s3" , "tf_acc_lambda_name_s3_unversioned" , & conf ) ,
testAccCheckAwsLambdaFunctionName ( & conf , "tf_acc_lambda_name_s3_unversioned" ) ,
testAccCheckAwsLambdaFunctionArnHasSuffix ( & conf , "tf_acc_lambda_name_s3_unversioned" ) ,
2016-09-04 14:10:13 +02:00
testAccCheckAwsLambdaSourceCodeHash ( & conf , "0tdaP9H9hsk9c2CycSwOG/sa/x5JyAmSYunA/ce99Pg=" ) ,
2016-05-24 23:46:18 +02:00
) ,
} ,
} ,
} )
}
2016-12-16 13:24:45 +01:00
func TestAccAWSLambdaFunction_runtimeValidation_noRuntime ( t * testing . T ) {
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-12-16 13:24:45 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigNoRuntime ( rName , rSt ) ,
2016-12-16 13:24:45 +01:00
ExpectError : regexp . MustCompile ( ` \\"runtime\\": required field is not set ` ) ,
} ,
} ,
} )
}
func TestAccAWSLambdaFunction_runtimeValidation_nodeJs ( t * testing . T ) {
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-12-16 13:24:45 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigNodeJsRuntime ( rName , rSt ) ,
2016-12-16 13:24:45 +01:00
ExpectError : regexp . MustCompile ( fmt . Sprintf ( "%s has reached end of life since October 2016 and has been deprecated in favor of %s" , lambda . RuntimeNodejs , lambda . RuntimeNodejs43 ) ) ,
} ,
} ,
} )
}
func TestAccAWSLambdaFunction_runtimeValidation_nodeJs43 ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-12-16 13:24:45 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigNodeJs43Runtime ( rName , rSt ) ,
2016-12-16 13:24:45 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "runtime" , lambda . RuntimeNodejs43 ) ,
) ,
} ,
} ,
} )
}
func TestAccAWSLambdaFunction_runtimeValidation_python27 ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-12-16 13:24:45 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigPython27Runtime ( rName , rSt ) ,
2016-12-16 13:24:45 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "runtime" , lambda . RuntimePython27 ) ,
) ,
} ,
} ,
} )
}
func TestAccAWSLambdaFunction_runtimeValidation_java8 ( t * testing . T ) {
var conf lambda . GetFunctionOutput
2016-12-16 17:42:37 +01:00
rSt := acctest . RandString ( 5 )
rName := fmt . Sprintf ( "tf_test_%s" , rSt )
2016-12-16 13:24:45 +01:00
resource . Test ( t , resource . TestCase {
PreCheck : func ( ) { testAccPreCheck ( t ) } ,
Providers : testAccProviders ,
CheckDestroy : testAccCheckLambdaFunctionDestroy ,
Steps : [ ] resource . TestStep {
{
2016-12-16 17:42:37 +01:00
Config : testAccAWSLambdaConfigJava8Runtime ( rName , rSt ) ,
2016-12-16 13:24:45 +01:00
Check : resource . ComposeTestCheckFunc (
testAccCheckAwsLambdaFunctionExists ( "aws_lambda_function.lambda_function_test" , rName , & conf ) ,
resource . TestCheckResourceAttr ( "aws_lambda_function.lambda_function_test" , "runtime" , lambda . RuntimeJava8 ) ,
) ,
} ,
} ,
} )
}
2015-06-01 18:33:22 +02:00
func testAccCheckLambdaFunctionDestroy ( s * terraform . State ) error {
conn := testAccProvider . Meta ( ) . ( * AWSClient ) . lambdaconn
for _ , rs := range s . RootModule ( ) . Resources {
if rs . Type != "aws_lambda_function" {
continue
}
_ , err := conn . GetFunction ( & lambda . GetFunctionInput {
FunctionName : aws . String ( rs . Primary . ID ) ,
} )
if err == nil {
return fmt . Errorf ( "Lambda Function still exists" )
}
}
return nil
}
2016-01-21 01:33:14 +01:00
func testAccCheckAwsLambdaFunctionExists ( res , funcName string , function * lambda . GetFunctionOutput ) resource . TestCheckFunc {
2015-06-01 18:33:22 +02:00
// Wait for IAM role
return func ( s * terraform . State ) error {
2016-01-21 01:33:14 +01:00
rs , ok := s . RootModule ( ) . Resources [ res ]
2015-06-01 18:33:22 +02:00
if ! ok {
2016-01-21 01:33:14 +01:00
return fmt . Errorf ( "Lambda function not found: %s" , res )
2015-06-01 18:33:22 +02:00
}
if rs . Primary . ID == "" {
return fmt . Errorf ( "Lambda function ID not set" )
}
conn := testAccProvider . Meta ( ) . ( * AWSClient ) . lambdaconn
params := & lambda . GetFunctionInput {
2016-01-21 01:33:14 +01:00
FunctionName : aws . String ( funcName ) ,
2015-06-01 18:33:22 +02:00
}
getFunction , err := conn . GetFunction ( params )
if err != nil {
return err
}
* function = * getFunction
return nil
}
}
2016-01-21 01:33:14 +01:00
func testAccCheckAwsLambdaFunctionName ( function * lambda . GetFunctionOutput , expectedName string ) resource . TestCheckFunc {
2015-06-01 18:33:22 +02:00
return func ( s * terraform . State ) error {
c := function . Configuration
if * c . FunctionName != expectedName {
return fmt . Errorf ( "Expected function name %s, got %s" , expectedName , * c . FunctionName )
}
2016-01-21 01:33:14 +01:00
return nil
}
}
func testAccCheckAWSLambdaFunctionVersion ( function * lambda . GetFunctionOutput , expectedVersion string ) resource . TestCheckFunc {
return func ( s * terraform . State ) error {
c := function . Configuration
if * c . Version != expectedVersion {
return fmt . Errorf ( "Expected version %s, got %s" , expectedVersion , * c . Version )
}
return nil
}
}
func testAccCheckAwsLambdaFunctionArnHasSuffix ( function * lambda . GetFunctionOutput , arnSuffix string ) resource . TestCheckFunc {
return func ( s * terraform . State ) error {
c := function . Configuration
if ! strings . HasSuffix ( * c . FunctionArn , arnSuffix ) {
return fmt . Errorf ( "Expected function ARN %s to have suffix %s" , * c . FunctionArn , arnSuffix )
2015-06-01 18:33:22 +02:00
}
return nil
}
}
2016-02-21 21:54:30 +01:00
func testAccCheckAwsLambdaSourceCodeHash ( function * lambda . GetFunctionOutput , expectedHash string ) resource . TestCheckFunc {
return func ( s * terraform . State ) error {
c := function . Configuration
if * c . CodeSha256 != expectedHash {
return fmt . Errorf ( "Expected code hash %s, got %s" , expectedHash , * c . CodeSha256 )
}
return nil
}
}
func testAccCreateZipFromFiles ( files map [ string ] string , zipFile * os . File ) error {
zipFile . Truncate ( 0 )
zipFile . Seek ( 0 , 0 )
w := zip . NewWriter ( zipFile )
for source , destination := range files {
f , err := w . Create ( destination )
if err != nil {
return err
}
fileContent , err := ioutil . ReadFile ( source )
if err != nil {
return err
}
_ , err = f . Write ( fileContent )
if err != nil {
return err
}
}
err := w . Close ( )
if err != nil {
return err
}
return w . Flush ( )
}
func createTempFile ( prefix string ) ( string , * os . File , error ) {
f , err := ioutil . TempFile ( os . TempDir ( ) , prefix )
if err != nil {
return "" , nil , err
}
pathToFile , err := filepath . Abs ( f . Name ( ) )
if err != nil {
return "" , nil , err
}
return pathToFile , f , nil
}
2016-12-16 17:42:37 +01:00
func baseAccAWSLambdaConfig ( rst string ) string {
return fmt . Sprintf ( `
2016-02-16 06:38:17 +01:00
resource "aws_iam_role_policy" "iam_policy_for_lambda" {
2016-12-16 17:42:37 +01:00
name = "iam_policy_for_lambda_%s"
2016-02-16 06:38:17 +01:00
role = "${aws_iam_role.iam_for_lambda.id}"
policy = << EOF
{
"Version" : "2012-10-17" ,
"Statement" : [
{
"Effect" : "Allow" ,
"Action" : [
"logs:CreateLogGroup" ,
"logs:CreateLogStream" ,
"logs:PutLogEvents"
] ,
"Resource" : "arn:aws:logs:*:*:*"
} ,
{
"Effect" : "Allow" ,
"Action" : [
2017-02-01 22:59:40 +01:00
"ec2:CreateNetworkInterface" ,
"ec2:DescribeNetworkInterfaces" ,
"ec2:DeleteNetworkInterface"
2016-02-16 06:38:17 +01:00
] ,
"Resource" : [
"*"
]
2017-02-23 02:50:10 +01:00
} ,
{
"Effect" : "Allow" ,
"Action" : [
"SNS:Publish"
] ,
"Resource" : [
"*"
]
2016-02-16 06:38:17 +01:00
}
]
}
EOF
}
2015-06-01 18:33:22 +02:00
resource "aws_iam_role" "iam_for_lambda" {
2016-12-16 17:42:37 +01:00
name = "iam_for_lambda_%s"
2015-06-01 18:33:22 +02:00
assume_role_policy = << EOF
{
"Version" : "2012-10-17" ,
"Statement" : [
{
"Action" : "sts:AssumeRole" ,
"Principal" : {
"Service" : "lambda.amazonaws.com"
} ,
"Effect" : "Allow" ,
"Sid" : ""
}
]
}
EOF
}
2016-02-16 06:38:17 +01:00
resource "aws_vpc" "vpc_for_lambda" {
cidr_block = "10.0.0.0/16"
}
resource "aws_subnet" "subnet_for_lambda" {
vpc_id = "${aws_vpc.vpc_for_lambda.id}"
cidr_block = "10.0.1.0/24"
tags {
Name = "lambda"
}
}
resource "aws_security_group" "sg_for_lambda" {
2016-12-16 17:42:37 +01:00
name = "sg_for_lambda_%s"
2016-02-16 06:38:17 +01:00
description = "Allow all inbound traffic for lambda test"
vpc_id = "${aws_vpc.vpc_for_lambda.id}"
ingress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [ "0.0.0.0/0" ]
}
egress {
from_port = 0
to_port = 0
protocol = "-1"
cidr_blocks = [ "0.0.0.0/0" ]
}
2016-12-16 17:42:37 +01:00
} ` , rst , rst , rst )
2016-02-16 06:38:17 +01:00
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigBasic ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-02-18 22:45:32 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
2016-05-16 17:31:46 +02:00
function_name = "%s"
2016-02-18 22:45:32 +01:00
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2016-02-18 22:45:32 +01:00
}
2016-05-16 17:31:46 +02:00
` , rName )
}
2016-02-18 22:45:32 +01:00
2017-03-01 16:30:39 +01:00
func testAccAWSLambdaConfigBasicUpdateRuntime ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "nodejs4.3-edge"
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigWithoutFilenameAndS3Attributes ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-12-12 16:12:56 +01:00
resource "aws_lambda_function" "lambda_function_test" {
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-12-12 16:12:56 +01:00
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigEnvVariables ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-11-19 00:48:18 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-11-19 00:48:18 +01:00
environment {
variables = {
foo = "bar"
}
}
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigEnvVariablesModified ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-11-19 00:48:18 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-11-19 00:48:18 +01:00
environment {
variables = {
foo = "baz"
foo1 = "bar1"
}
}
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigEnvVariablesModifiedWithoutEnvironment ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-12-02 14:36:55 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-12-02 14:36:55 +01:00
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigEncryptedEnvVariables ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-11-19 00:48:18 +01:00
resource "aws_kms_key" "foo" {
description = "Terraform acc test %s"
policy = << POLICY
{
"Version" : "2012-10-17" ,
"Id" : "kms-tf-1" ,
"Statement" : [
{
"Sid" : "Enable IAM User Permissions" ,
"Effect" : "Allow" ,
"Principal" : {
"AWS" : "*"
} ,
"Action" : "kms:*" ,
"Resource" : "*"
}
]
}
POLICY
}
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
kms_key_arn = "${aws_kms_key.foo.arn}"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-11-19 00:48:18 +01:00
environment {
variables = {
foo = "bar"
}
}
}
` , rName , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigEncryptedEnvVariablesModified ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-11-19 00:48:18 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-11-19 00:48:18 +01:00
environment {
variables = {
foo = "bar"
}
}
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigVersioned ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-09-04 10:55:13 +02:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
publish = true
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2016-09-04 10:55:13 +02:00
}
` , rName )
}
2017-02-23 02:50:10 +01:00
func testAccAWSLambdaConfigWithDeadLetterConfig ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "nodejs4.3"
dead_letter_config {
target_arn = "${aws_sns_topic.lambda_function_test.arn}"
}
}
resource "aws_sns_topic" "lambda_function_test" {
name = "%s"
}
` , rName , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigWithVPC ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2015-06-01 18:33:22 +02:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
2016-05-16 17:31:46 +02:00
function_name = "%s"
2015-06-01 18:33:22 +02:00
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2016-02-16 06:38:17 +01:00
vpc_config = {
subnet_ids = [ "${aws_subnet.subnet_for_lambda.id}" ]
security_group_ids = [ "${aws_security_group.sg_for_lambda.id}" ]
}
2016-05-16 17:31:46 +02:00
} ` , rName )
2015-06-01 18:33:22 +02:00
}
2016-01-21 01:33:14 +01:00
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigS3 ( rName , rSt string ) string {
2016-05-16 17:31:46 +02:00
return fmt . Sprintf ( `
2016-01-21 01:33:14 +01:00
resource "aws_s3_bucket" "lambda_bucket" {
bucket = "tf-test-bucket-%d"
}
resource "aws_s3_bucket_object" "lambda_code" {
bucket = "${aws_s3_bucket.lambda_bucket.id}"
key = "lambdatest.zip"
source = "test-fixtures/lambdatest.zip"
}
resource "aws_iam_role" "iam_for_lambda" {
2016-12-16 17:42:37 +01:00
name = "iam_for_lambda_%s"
2016-01-21 01:33:14 +01:00
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_s3test" {
s3_bucket = "${aws_s3_bucket.lambda_bucket.id}"
s3_key = "${aws_s3_bucket_object.lambda_code.id}"
2016-05-16 17:31:46 +02:00
function_name = "%s"
2016-01-21 01:33:14 +01:00
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2016-01-21 01:33:14 +01:00
}
2016-12-16 18:36:19 +01:00
` , acctest . RandInt ( ) , rSt , rName )
2016-05-16 17:31:46 +02:00
}
2016-02-21 21:54:30 +01:00
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigNoRuntime ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-12-16 13:24:45 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-12-16 13:24:45 +01:00
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigNodeJsRuntime ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-12-16 13:24:45 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 18:36:19 +01:00
runtime = "nodejs4.3"
2016-12-16 13:24:45 +01:00
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigNodeJs43Runtime ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-12-16 13:24:45 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "nodejs4.3"
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigPython27Runtime ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-12-16 13:24:45 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "python2.7"
}
` , rName )
}
2016-12-16 17:42:37 +01:00
func testAccAWSLambdaConfigJava8Runtime ( rName , rSt string ) string {
return fmt . Sprintf ( baseAccAWSLambdaConfig ( rSt ) + `
2016-12-16 13:24:45 +01:00
resource "aws_lambda_function" "lambda_function_test" {
filename = "test-fixtures/lambdatest.zip"
function_name = "%s"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
runtime = "java8"
}
` , rName )
}
2016-02-21 21:54:30 +01:00
const testAccAWSLambdaFunctionConfig_local_tpl = `
resource "aws_iam_role" "iam_for_lambda" {
2017-03-13 19:42:15 +01:00
name = "iam_for_lambda_%d"
2016-02-21 21:54:30 +01:00
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"
source_code_hash = "${base64sha256(file(" % s "))}"
function_name = "tf_acc_lambda_name_local"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2016-02-21 21:54:30 +01:00
}
`
2017-03-13 19:42:15 +01:00
func genAWSLambdaFunctionConfig_local ( filePath string , rInt int ) string {
return fmt . Sprintf ( testAccAWSLambdaFunctionConfig_local_tpl , rInt ,
2016-02-21 21:54:30 +01:00
filePath , filePath )
}
2017-02-02 16:57:44 +01:00
func genAWSLambdaFunctionConfig_local_name_only ( filePath , rName string ) string {
return testAccAWSLambdaFunctionConfig_local_name_only_tpl ( filePath , rName )
2016-06-09 23:44:10 +02:00
}
2017-02-02 16:57:44 +01:00
func testAccAWSLambdaFunctionConfig_local_name_only_tpl ( filePath , rName string ) string {
return fmt . Sprintf ( `
2016-06-09 23:44:10 +02:00
resource "aws_iam_role" "iam_for_lambda" {
2017-02-02 16:57:44 +01:00
name = "%s"
2016-06-09 23:44:10 +02:00
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"
2017-02-02 16:57:44 +01:00
function_name = "%s"
2016-06-09 23:44:10 +02:00
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2017-02-02 16:57:44 +01:00
} ` , rName , filePath , rName )
2016-06-09 23:44:10 +02:00
}
2016-02-21 21:54:30 +01:00
const testAccAWSLambdaFunctionConfig_s3_tpl = `
resource "aws_s3_bucket" "artifacts" {
2016-12-16 13:24:45 +01:00
bucket = "%s"
acl = "private"
force_destroy = true
versioning {
enabled = true
}
2016-02-21 21:54:30 +01:00
}
resource "aws_s3_bucket_object" "o" {
2016-12-16 13:24:45 +01:00
bucket = "${aws_s3_bucket.artifacts.bucket}"
key = "%s"
source = "%s"
etag = "${md5(file(" % s "))}"
2016-02-21 21:54:30 +01:00
}
resource "aws_iam_role" "iam_for_lambda" {
2017-03-13 19:42:15 +01:00
name = "iam_for_lambda_%d"
2016-02-21 21:54:30 +01:00
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_s3" {
2016-12-16 13:24:45 +01:00
s3_bucket = "${aws_s3_bucket_object.o.bucket}"
s3_key = "${aws_s3_bucket_object.o.key}"
s3_object_version = "${aws_s3_bucket_object.o.version_id}"
2016-02-21 21:54:30 +01:00
function_name = "tf_acc_lambda_name_s3"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2016-02-21 21:54:30 +01:00
}
`
2017-03-13 19:42:15 +01:00
func genAWSLambdaFunctionConfig_s3 ( bucket , key , path string , rInt int ) string {
2016-02-21 21:54:30 +01:00
return fmt . Sprintf ( testAccAWSLambdaFunctionConfig_s3_tpl ,
2017-03-13 19:42:15 +01:00
bucket , key , path , path , rInt )
2016-02-21 21:54:30 +01:00
}
2016-05-24 23:46:18 +02:00
2017-02-02 16:57:44 +01:00
func testAccAWSLambdaFunctionConfig_s3_unversioned_tpl ( rName , bucketName , key , path string ) string {
return fmt . Sprintf ( `
2016-05-24 23:46:18 +02:00
resource "aws_s3_bucket" "artifacts" {
2016-12-16 13:24:45 +01:00
bucket = "%s"
acl = "private"
force_destroy = true
2016-05-24 23:46:18 +02:00
}
resource "aws_s3_bucket_object" "o" {
2016-12-16 13:24:45 +01:00
bucket = "${aws_s3_bucket.artifacts.bucket}"
key = "%s"
source = "%s"
etag = "${md5(file(" % s "))}"
2016-05-24 23:46:18 +02:00
}
resource "aws_iam_role" "iam_for_lambda" {
2017-02-02 17:04:24 +01:00
name = "%s"
2016-05-24 23:46:18 +02:00
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_s3" {
2016-12-16 13:24:45 +01:00
s3_bucket = "${aws_s3_bucket_object.o.bucket}"
s3_key = "${aws_s3_bucket_object.o.key}"
2016-05-24 23:46:18 +02:00
function_name = "tf_acc_lambda_name_s3_unversioned"
role = "${aws_iam_role.iam_for_lambda.arn}"
handler = "exports.example"
2016-12-16 13:24:45 +01:00
runtime = "nodejs4.3"
2017-02-02 16:57:44 +01:00
} ` , bucketName , key , path , path , rName )
2016-05-24 23:46:18 +02:00
}