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