github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/aws/resource_aws_lambda_alias.go (about) 1 package aws 2 3 import ( 4 "fmt" 5 "log" 6 "strings" 7 8 "github.com/aws/aws-sdk-go/aws" 9 "github.com/aws/aws-sdk-go/aws/awserr" 10 "github.com/aws/aws-sdk-go/service/lambda" 11 "github.com/hashicorp/terraform/helper/schema" 12 ) 13 14 func resourceAwsLambdaAlias() *schema.Resource { 15 return &schema.Resource{ 16 Create: resourceAwsLambdaAliasCreate, 17 Read: resourceAwsLambdaAliasRead, 18 Update: resourceAwsLambdaAliasUpdate, 19 Delete: resourceAwsLambdaAliasDelete, 20 21 Schema: map[string]*schema.Schema{ 22 "description": &schema.Schema{ 23 Type: schema.TypeString, 24 Optional: true, 25 }, 26 "function_name": &schema.Schema{ 27 Type: schema.TypeString, 28 Required: true, 29 }, 30 "function_version": &schema.Schema{ 31 Type: schema.TypeString, 32 Required: true, 33 }, 34 "name": &schema.Schema{ 35 Type: schema.TypeString, 36 Required: true, 37 }, 38 "arn": &schema.Schema{ 39 Type: schema.TypeString, 40 Computed: true, 41 }, 42 }, 43 } 44 } 45 46 // resourceAwsLambdaAliasCreate maps to: 47 // CreateAlias in the API / SDK 48 func resourceAwsLambdaAliasCreate(d *schema.ResourceData, meta interface{}) error { 49 conn := meta.(*AWSClient).lambdaconn 50 51 functionName := d.Get("function_name").(string) 52 aliasName := d.Get("name").(string) 53 54 log.Printf("[DEBUG] Creating Lambda alias: alias %s for function %s", aliasName, functionName) 55 56 params := &lambda.CreateAliasInput{ 57 Description: aws.String(d.Get("description").(string)), 58 FunctionName: aws.String(functionName), 59 FunctionVersion: aws.String(d.Get("function_version").(string)), 60 Name: aws.String(aliasName), 61 } 62 63 aliasConfiguration, err := conn.CreateAlias(params) 64 if err != nil { 65 return fmt.Errorf("Error creating Lambda alias: %s", err) 66 } 67 68 d.SetId(*aliasConfiguration.AliasArn) 69 70 return resourceAwsLambdaAliasRead(d, meta) 71 } 72 73 // resourceAwsLambdaAliasRead maps to: 74 // GetAlias in the API / SDK 75 func resourceAwsLambdaAliasRead(d *schema.ResourceData, meta interface{}) error { 76 conn := meta.(*AWSClient).lambdaconn 77 78 log.Printf("[DEBUG] Fetching Lambda alias: %s:%s", d.Get("function_name"), d.Get("name")) 79 80 params := &lambda.GetAliasInput{ 81 FunctionName: aws.String(d.Get("function_name").(string)), 82 Name: aws.String(d.Get("name").(string)), 83 } 84 85 aliasConfiguration, err := conn.GetAlias(params) 86 if err != nil { 87 if awsErr, ok := err.(awserr.Error); ok { 88 if awsErr.Code() == "ResourceNotFoundException" && strings.Contains(awsErr.Message(), "Cannot find alias arn") { 89 d.SetId("") 90 return nil 91 } 92 } 93 return err 94 } 95 96 d.Set("description", aliasConfiguration.Description) 97 d.Set("function_version", aliasConfiguration.FunctionVersion) 98 d.Set("name", aliasConfiguration.Name) 99 d.Set("arn", aliasConfiguration.AliasArn) 100 101 return nil 102 } 103 104 // resourceAwsLambdaAliasDelete maps to: 105 // DeleteAlias in the API / SDK 106 func resourceAwsLambdaAliasDelete(d *schema.ResourceData, meta interface{}) error { 107 conn := meta.(*AWSClient).lambdaconn 108 109 log.Printf("[INFO] Deleting Lambda alias: %s:%s", d.Get("function_name"), d.Get("name")) 110 111 params := &lambda.DeleteAliasInput{ 112 FunctionName: aws.String(d.Get("function_name").(string)), 113 Name: aws.String(d.Get("name").(string)), 114 } 115 116 _, err := conn.DeleteAlias(params) 117 if err != nil { 118 return fmt.Errorf("Error deleting Lambda alias: %s", err) 119 } 120 121 d.SetId("") 122 123 return nil 124 } 125 126 // resourceAwsLambdaAliasUpdate maps to: 127 // UpdateAlias in the API / SDK 128 func resourceAwsLambdaAliasUpdate(d *schema.ResourceData, meta interface{}) error { 129 conn := meta.(*AWSClient).lambdaconn 130 131 log.Printf("[DEBUG] Updating Lambda alias: %s:%s", d.Get("function_name"), d.Get("name")) 132 133 params := &lambda.UpdateAliasInput{ 134 Description: aws.String(d.Get("description").(string)), 135 FunctionName: aws.String(d.Get("function_name").(string)), 136 FunctionVersion: aws.String(d.Get("function_version").(string)), 137 Name: aws.String(d.Get("name").(string)), 138 } 139 140 _, err := conn.UpdateAlias(params) 141 if err != nil { 142 return fmt.Errorf("Error updating Lambda alias: %s", err) 143 } 144 145 return nil 146 }