github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_lambda_alias_test.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "regexp" 6 "testing" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/service/lambda" 10 "github.com/hashicorp/terraform/helper/acctest" 11 "github.com/hashicorp/terraform/helper/resource" 12 "github.com/hashicorp/terraform/terraform" 13 ) 14 15 func TestAccAWSLambdaAlias_basic(t *testing.T) { 16 var conf lambda.AliasConfiguration 17 rInt := acctest.RandInt() 18 19 resource.Test(t, resource.TestCase{ 20 PreCheck: func() { testAccPreCheck(t) }, 21 Providers: testAccProviders, 22 CheckDestroy: testAccCheckAwsLambdaAliasDestroy, 23 Steps: []resource.TestStep{ 24 resource.TestStep{ 25 Config: testAccAwsLambdaAliasConfig(rInt), 26 Check: resource.ComposeTestCheckFunc( 27 testAccCheckAwsLambdaAliasExists("aws_lambda_alias.lambda_alias_test", &conf), 28 testAccCheckAwsLambdaAttributes(&conf), 29 resource.TestMatchResourceAttr("aws_lambda_alias.lambda_alias_test", "arn", regexp.MustCompile(`^arn:aws:lambda:[a-z]+-[a-z]+-[0-9]+:\d{12}:function:example_lambda_name_create:testalias$`)), 30 ), 31 }, 32 }, 33 }) 34 } 35 36 func testAccCheckAwsLambdaAliasDestroy(s *terraform.State) error { 37 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 38 39 for _, rs := range s.RootModule().Resources { 40 if rs.Type != "aws_lambda_alias" { 41 continue 42 } 43 44 _, err := conn.GetAlias(&lambda.GetAliasInput{ 45 FunctionName: aws.String(rs.Primary.ID), 46 }) 47 48 if err == nil { 49 return fmt.Errorf("Lambda alias was not deleted") 50 } 51 52 } 53 54 return nil 55 } 56 57 func testAccCheckAwsLambdaAliasExists(n string, mapping *lambda.AliasConfiguration) resource.TestCheckFunc { 58 return func(s *terraform.State) error { 59 rs, ok := s.RootModule().Resources[n] 60 if !ok { 61 return fmt.Errorf("Lambda alias not found: %s", n) 62 } 63 64 if rs.Primary.ID == "" { 65 return fmt.Errorf("Lambda alias not set") 66 } 67 68 conn := testAccProvider.Meta().(*AWSClient).lambdaconn 69 70 params := &lambda.GetAliasInput{ 71 FunctionName: aws.String(rs.Primary.ID), 72 Name: aws.String("testalias"), 73 } 74 75 getAliasConfiguration, err := conn.GetAlias(params) 76 if err != nil { 77 return err 78 } 79 80 *mapping = *getAliasConfiguration 81 82 return nil 83 } 84 } 85 86 func testAccCheckAwsLambdaAttributes(mapping *lambda.AliasConfiguration) resource.TestCheckFunc { 87 return func(s *terraform.State) error { 88 name := *mapping.Name 89 arn := *mapping.AliasArn 90 if arn == "" { 91 return fmt.Errorf("Could not read Lambda alias ARN") 92 } 93 if name == "" { 94 return fmt.Errorf("Could not read Lambda alias name") 95 } 96 return nil 97 } 98 } 99 100 func testAccAwsLambdaAliasConfig(rInt int) string { 101 return fmt.Sprintf(` 102 resource "aws_iam_role" "iam_for_lambda" { 103 name = "iam_for_lambda_%d" 104 105 assume_role_policy = <<EOF 106 { 107 "Version": "2012-10-17", 108 "Statement": [ 109 { 110 "Action": "sts:AssumeRole", 111 "Principal": { 112 "Service": "lambda.amazonaws.com" 113 }, 114 "Effect": "Allow", 115 "Sid": "" 116 } 117 ] 118 } 119 EOF 120 } 121 122 resource "aws_iam_policy" "policy_for_role" { 123 name = "policy_for_role_%d" 124 path = "/" 125 description = "IAM policy for for Lamda alias testing" 126 127 policy = <<EOF 128 { 129 "Version": "2012-10-17", 130 "Statement": [ 131 { 132 "Effect": "Allow", 133 "Action": [ 134 "lambda:*" 135 ], 136 "Resource": "*" 137 } 138 ] 139 } 140 EOF 141 } 142 143 resource "aws_iam_policy_attachment" "policy_attachment_for_role" { 144 name = "policy_attachment_for_role_%d" 145 roles = ["${aws_iam_role.iam_for_lambda.name}"] 146 policy_arn = "${aws_iam_policy.policy_for_role.arn}" 147 } 148 149 resource "aws_lambda_function" "lambda_function_test_create" { 150 filename = "test-fixtures/lambdatest.zip" 151 function_name = "example_lambda_name_create" 152 role = "${aws_iam_role.iam_for_lambda.arn}" 153 handler = "exports.example" 154 runtime = "nodejs4.3" 155 } 156 157 resource "aws_lambda_alias" "lambda_alias_test" { 158 name = "testalias" 159 description = "a sample description" 160 function_name = "${aws_lambda_function.lambda_function_test_create.arn}" 161 function_version = "$LATEST" 162 }`, rInt, rInt, rInt) 163 }