forked from hashicorp/terraform-provider-aws
/
resource_aws_lambda_alias.go
188 lines (154 loc) · 5.23 KB
/
resource_aws_lambda_alias.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
package aws
import (
"fmt"
"log"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/lambda"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsLambdaAlias() *schema.Resource {
return &schema.Resource{
Create: resourceAwsLambdaAliasCreate,
Read: resourceAwsLambdaAliasRead,
Update: resourceAwsLambdaAliasUpdate,
Delete: resourceAwsLambdaAliasDelete,
Schema: map[string]*schema.Schema{
"description": {
Type: schema.TypeString,
Optional: true,
},
"function_name": {
Type: schema.TypeString,
Required: true,
},
"function_version": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"arn": {
Type: schema.TypeString,
Computed: true,
},
"invoke_arn": {
Type: schema.TypeString,
Computed: true,
},
"routing_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"additional_version_weights": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeFloat},
},
},
},
},
},
}
}
// resourceAwsLambdaAliasCreate maps to:
// CreateAlias in the API / SDK
func resourceAwsLambdaAliasCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
functionName := d.Get("function_name").(string)
aliasName := d.Get("name").(string)
log.Printf("[DEBUG] Creating Lambda alias: alias %s for function %s", aliasName, functionName)
params := &lambda.CreateAliasInput{
Description: aws.String(d.Get("description").(string)),
FunctionName: aws.String(functionName),
FunctionVersion: aws.String(d.Get("function_version").(string)),
Name: aws.String(aliasName),
RoutingConfig: expandLambdaAliasRoutingConfiguration(d.Get("routing_config").([]interface{})),
}
aliasConfiguration, err := conn.CreateAlias(params)
if err != nil {
return fmt.Errorf("Error creating Lambda alias: %s", err)
}
d.SetId(*aliasConfiguration.AliasArn)
return resourceAwsLambdaAliasRead(d, meta)
}
// resourceAwsLambdaAliasRead maps to:
// GetAlias in the API / SDK
func resourceAwsLambdaAliasRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
log.Printf("[DEBUG] Fetching Lambda alias: %s:%s", d.Get("function_name"), d.Get("name"))
params := &lambda.GetAliasInput{
FunctionName: aws.String(d.Get("function_name").(string)),
Name: aws.String(d.Get("name").(string)),
}
aliasConfiguration, err := conn.GetAlias(params)
if err != nil {
if awsErr, ok := err.(awserr.Error); ok {
if awsErr.Code() == "ResourceNotFoundException" && strings.Contains(awsErr.Message(), "Cannot find alias arn") {
d.SetId("")
return nil
}
}
return err
}
d.Set("description", aliasConfiguration.Description)
d.Set("function_version", aliasConfiguration.FunctionVersion)
d.Set("name", aliasConfiguration.Name)
d.Set("arn", aliasConfiguration.AliasArn)
invokeArn := lambdaFunctionInvokeArn(*aliasConfiguration.AliasArn, meta)
d.Set("invoke_arn", invokeArn)
if err := d.Set("routing_config", flattenLambdaAliasRoutingConfiguration(aliasConfiguration.RoutingConfig)); err != nil {
return fmt.Errorf("error setting routing_config: %s", err)
}
return nil
}
// resourceAwsLambdaAliasDelete maps to:
// DeleteAlias in the API / SDK
func resourceAwsLambdaAliasDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
log.Printf("[INFO] Deleting Lambda alias: %s:%s", d.Get("function_name"), d.Get("name"))
params := &lambda.DeleteAliasInput{
FunctionName: aws.String(d.Get("function_name").(string)),
Name: aws.String(d.Get("name").(string)),
}
_, err := conn.DeleteAlias(params)
if err != nil {
return fmt.Errorf("Error deleting Lambda alias: %s", err)
}
return nil
}
// resourceAwsLambdaAliasUpdate maps to:
// UpdateAlias in the API / SDK
func resourceAwsLambdaAliasUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).lambdaconn
log.Printf("[DEBUG] Updating Lambda alias: %s:%s", d.Get("function_name"), d.Get("name"))
params := &lambda.UpdateAliasInput{
Description: aws.String(d.Get("description").(string)),
FunctionName: aws.String(d.Get("function_name").(string)),
FunctionVersion: aws.String(d.Get("function_version").(string)),
Name: aws.String(d.Get("name").(string)),
RoutingConfig: expandLambdaAliasRoutingConfiguration(d.Get("routing_config").([]interface{})),
}
_, err := conn.UpdateAlias(params)
if err != nil {
return fmt.Errorf("Error updating Lambda alias: %s", err)
}
return nil
}
func expandLambdaAliasRoutingConfiguration(l []interface{}) *lambda.AliasRoutingConfiguration {
aliasRoutingConfiguration := &lambda.AliasRoutingConfiguration{}
if len(l) == 0 || l[0] == nil {
return aliasRoutingConfiguration
}
m := l[0].(map[string]interface{})
if v, ok := m["additional_version_weights"]; ok {
aliasRoutingConfiguration.AdditionalVersionWeights = expandFloat64Map(v.(map[string]interface{}))
}
return aliasRoutingConfiguration
}