-
Notifications
You must be signed in to change notification settings - Fork 9.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
resource/aws_lambda_alias: Support Traffic Shifting #3316
Changes from 4 commits
0df85b8
88e72c8
9410ea5
6086430
d3b6f1c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package aws | ||
|
||
import ( | ||
"errors" | ||
"fmt" | ||
"log" | ||
"strings" | ||
|
@@ -39,6 +40,20 @@ func resourceAwsLambdaAlias() *schema.Resource { | |
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.TypeFloat, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
@@ -58,6 +73,20 @@ func resourceAwsLambdaAliasCreate(d *schema.ResourceData, meta interface{}) erro | |
FunctionName: aws.String(functionName), | ||
FunctionVersion: aws.String(d.Get("function_version").(string)), | ||
Name: aws.String(aliasName), | ||
RoutingConfig: &lambda.AliasRoutingConfiguration{}, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Including the optional and empty 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(weights)
}
return aliasRoutingConfiguration
}
// this should live in structure.go near expandStringList and replace readAdditionalVersionWeights
func expandFloat64Map(m map[string]interface{}) map[string]*float64 {
float64Map := make(map[string]*float64, len(m))
for k, v := range m {
float64Map[k] = aws.Float64(v.(float64))
}
return float64Map
} and called in create/update via: |
||
} | ||
|
||
if v, ok := d.GetOk("routing_config"); ok { | ||
routingConfigs := v.([]interface{}) | ||
routingConfig, ok := routingConfigs[0].(map[string]interface{}) | ||
if !ok { | ||
return errors.New("At least one field is expected inside routing_config") | ||
} | ||
|
||
if additionalVersionWeights, ok := routingConfig["additional_version_weights"]; ok { | ||
weights := readAdditionalVersionWeights(additionalVersionWeights.(map[string]interface{})) | ||
params.RoutingConfig.AdditionalVersionWeights = aws.Float64Map(weights) | ||
} | ||
} | ||
|
||
aliasConfiguration, err := conn.CreateAlias(params) | ||
|
@@ -97,6 +126,7 @@ func resourceAwsLambdaAliasRead(d *schema.ResourceData, meta interface{}) error | |
d.Set("function_version", aliasConfiguration.FunctionVersion) | ||
d.Set("name", aliasConfiguration.Name) | ||
d.Set("arn", aliasConfiguration.AliasArn) | ||
d.Set("routing_config", aliasConfiguration.RoutingConfig) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When using if err := d.Set("routing_config", aliasConfiguration.RoutingConfig); err != nil {
return fmt.Errorf("error setting routing_config: %s", err)
} Without this check and if there is a type conversion error, Terraform is silently accepting the configuration "as-is" into the Terraform state and unable to detect drift from the API should changes occur outside Terraform. After adding this error checking, you should start receiving type errors as func flattenLambdaAliasRoutingConfiguration(arc *lambda.AliasRoutingConfiguration) []interface{} {
if arc == nil {
return []interface{}{}
}
m := map[string]interface{}{
"additional_version_weights": aws.Float64ValueMap(arc.AdditionalVersionWeights)
}
return []interface{}{m}
} Which can be referenced via:
|
||
|
||
return nil | ||
} | ||
|
@@ -135,6 +165,20 @@ func resourceAwsLambdaAliasUpdate(d *schema.ResourceData, meta interface{}) erro | |
FunctionName: aws.String(d.Get("function_name").(string)), | ||
FunctionVersion: aws.String(d.Get("function_version").(string)), | ||
Name: aws.String(d.Get("name").(string)), | ||
RoutingConfig: &lambda.AliasRoutingConfiguration{}, | ||
} | ||
|
||
if v, ok := d.GetOk("routing_config"); ok { | ||
routingConfigs := v.([]interface{}) | ||
routingConfig, ok := routingConfigs[0].(map[string]interface{}) | ||
if !ok { | ||
return errors.New("At least one field is expected inside routing_config") | ||
} | ||
|
||
if additionalVersionWeights, ok := routingConfig["additional_version_weights"]; ok { | ||
weights := readAdditionalVersionWeights(additionalVersionWeights.(map[string]interface{})) | ||
params.RoutingConfig.AdditionalVersionWeights = aws.Float64Map(weights) | ||
} | ||
} | ||
|
||
_, err := conn.UpdateAlias(params) | ||
|
@@ -144,3 +188,12 @@ func resourceAwsLambdaAliasUpdate(d *schema.ResourceData, meta interface{}) erro | |
|
||
return nil | ||
} | ||
|
||
func readAdditionalVersionWeights(avw map[string]interface{}) map[string]float64 { | ||
weights := make(map[string]float64) | ||
for k, v := range avw { | ||
weights[k] = v.(float64) | ||
} | ||
|
||
return weights | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -20,7 +20,12 @@ resource "aws_lambda_alias" "test_alias" { | |
name = "testalias" | ||
description = "a sample description" | ||
function_name = "${aws_lambda_function.lambda_function_test.arn}" | ||
function_version = "$LATEST" | ||
function_version = "1" | ||
routing_config = { | ||
additional_version_weights = { | ||
"2" = 0.5 | ||
} | ||
} | ||
} | ||
``` | ||
|
||
|
@@ -30,10 +35,17 @@ resource "aws_lambda_alias" "test_alias" { | |
* `description` - (Optional) Description of the alias. | ||
* `function_name` - (Required) The function ARN of the Lambda function for which you want to create an alias. | ||
* `function_version` - (Required) Lambda function version for which you are creating the alias. Pattern: `(\$LATEST|[0-9]+)`. | ||
* `routing_config` - (Optional) The Lambda alias' route configuration settings. Fields documented below | ||
|
||
For **routing_config** the following attributes are supported: | ||
|
||
* `additional_version_weights` - (Optional) A map that defines the proportion of events that should be sent to different versions of a lambda function. | ||
|
||
## Attributes Reference | ||
|
||
* `arn` - The Amazon Resource Name (ARN) identifying your Lambda function alias. | ||
|
||
[1]: http://docs.aws.amazon.com/lambda/latest/dg/welcome.html | ||
[2]: http://docs.aws.amazon.com/lambda/latest/dg/API_CreateAlias.html | ||
[3]: https://docs.aws.amazon.com/lambda/latest/dg/API_AliasRoutingConfiguration.html | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It looks like this documentation reference was added but never referenced above. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While this still works today,
Elem
should only contain*schema.Schema
or*schema.Resource
👍 In this case, it can be fixed with: