package aws import ( "fmt" "testing" "github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/service/lambda" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" ) func TestAccAWSLambdaAlias_basic(t *testing.T) { var conf lambda.AliasConfiguration resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testAccCheckAwsLambdaAliasDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: testAccAwsLambdaAliasConfig, Check: resource.ComposeTestCheckFunc( testAccCheckAwsLambdaAliasExists("aws_lambda_alias.lambda_alias_test", &conf), testAccCheckAwsLambdaAttributes(&conf), ), }, }, }) } func testAccCheckAwsLambdaAliasDestroy(s *terraform.State) error { conn := testAccProvider.Meta().(*AWSClient).lambdaconn for _, rs := range s.RootModule().Resources { if rs.Type != "aws_lambda_alias" { continue } _, err := conn.GetAlias(&lambda.GetAliasInput{ FunctionName: aws.String(rs.Primary.ID), }) if err == nil { return fmt.Errorf("Lambda alias was not deleted") } } return nil } func testAccCheckAwsLambdaAliasExists(n string, mapping *lambda.AliasConfiguration) resource.TestCheckFunc { return func(s *terraform.State) error { rs, ok := s.RootModule().Resources[n] if !ok { return fmt.Errorf("Lambda alias not found: %s", n) } if rs.Primary.ID == "" { return fmt.Errorf("Lambda alias not set") } conn := testAccProvider.Meta().(*AWSClient).lambdaconn params := &lambda.GetAliasInput{ FunctionName: aws.String(rs.Primary.ID), Name: aws.String("testalias"), } getAliasConfiguration, err := conn.GetAlias(params) if err != nil { return err } *mapping = *getAliasConfiguration return nil } } func testAccCheckAwsLambdaAttributes(mapping *lambda.AliasConfiguration) resource.TestCheckFunc { return func(s *terraform.State) error { name := *mapping.Name arn := *mapping.AliasArn if arn == "" { return fmt.Errorf("Could not read Lambda alias ARN") } if name == "" { return fmt.Errorf("Could not read Lambda alias name") } return nil } } const testAccAwsLambdaAliasConfig = ` resource "aws_iam_role" "iam_for_lambda" { name = "iam_for_lambda" assume_role_policy = <