-
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
Conversation
Hey @radeksimko, I hope I don't sound impatient but, is there any chance I could get some feedback on this submission please? Cheers. |
When will this feature be released? |
Any idea when this feature will be rolled out? We would like to use it for some blue/green deployments. |
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.
Hi @eggsbenjamin 👋 Thanks for submitting this!
I left you some initial feedback below which will require some code changes to properly handle drift detection. Please let us know if you have any questions or do not have time to implement these changes. Thanks!
aws/resource_aws_lambda_alias.go
Outdated
"additional_version_weights": { | ||
Type: schema.TypeMap, | ||
Optional: true, | ||
Elem: schema.TypeFloat, |
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:
Elem: &schema.Schema{Type: schema.TypeFloat},
aws/resource_aws_lambda_alias.go
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
Including the optional and empty AliasRoutingConfiguration{}
here seems extraneous during the create function -- more generally though, we prefer to move the logic to populate a parameter like this into an "expandServiceThing()" type method, which can be reused in create and update functions. e.g.
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: RoutingConfig: expandLambdaAliasRoutingConfiguration(d.Get("routing_config").([]interface{}))
aws/resource_aws_lambda_alias.go
Outdated
@@ -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 comment
The reason will be displayed to describe this comment to others. Learn more.
When using d.Set()
with aggregate types (TypeList, TypeSet, TypeMap), we should perform error checking to prevent issues where the code is not properly able to set the Terraform state. e.g.
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 aliasConfiguration.RoutingConfig
is a SDK object and not a []interface{}
. We generally handle this via a "flattenServiceObject()" function. e.g.
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:
if err := d.Set("routing_config", flattenLambdaAliasRoutingConfiguration(aliasConfiguration.RoutingConfig)); err != nil {
return fmt.Errorf("error setting routing_config: %s", err)
}
|
||
## 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 comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like this documentation reference was added but never referenced above.
Hi @bflad , thanks for the response. Cool, I'll update the PR! |
@bflad updated with the suggested changes. Cheers. |
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.
Great work, @eggsbenjamin! 🚀
make testacc TEST=./aws TESTARGS='-run=TestAccAWSLambdaAlias'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSLambdaAlias -timeout 120m
=== RUN TestAccAWSLambdaAlias_basic
--- PASS: TestAccAWSLambdaAlias_basic (24.96s)
=== RUN TestAccAWSLambdaAlias_nameupdate
--- PASS: TestAccAWSLambdaAlias_nameupdate (43.52s)
PASS
ok github.com/terraform-providers/terraform-provider-aws/aws 68.534s
This has been released in version 1.26.0 of the AWS provider. Please see the Terraform documentation on provider versioning or reach out if you need any assistance upgrading. |
I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues. If you feel this issue should be reopened, we encourage creating a new issue linking back to this one for added context. Thanks! |
Added
routing_config
property toaws_lambda_alias
which containsadditional_version_weights
to afford lambda alias traffic shifting.Acceptance tests: