Skip to content
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

Merged
merged 5 commits into from
Jul 2, 2018

Conversation

eggsbenjamin
Copy link
Contributor

@eggsbenjamin eggsbenjamin commented Feb 10, 2018

Added routing_configproperty to aws_lambda_alias which contains additional_version_weights to afford lambda alias traffic shifting.

Acceptance tests:

=== RUN   TestAccAWSLambdaAlias_basic
--- PASS: TestAccAWSLambdaAlias_basic (37.61s)
=== RUN   TestAccAWSLambdaAlias_routingConfig
--- PASS: TestAccAWSLambdaAlias_routingConfig (69.14s)
PASS
ok      github.com/terraform-providers/terraform-provider-aws/aws       106.787s

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Feb 10, 2018
@radeksimko radeksimko changed the title Support Lambda Alias Traffic Shifting resource/aws_lambda_alias: Support Traffic Shifting Feb 11, 2018
@radeksimko radeksimko added enhancement Requests to existing resources that expand the functionality or scope. service/lambda Issues and PRs that pertain to the lambda service. labels Feb 11, 2018
@eggsbenjamin
Copy link
Contributor Author

Hey @radeksimko, I hope I don't sound impatient but, is there any chance I could get some feedback on this submission please? Cheers.

@alikor
Copy link

alikor commented Mar 16, 2018

When will this feature be released?

@kylegalbraith
Copy link

Any idea when this feature will be rolled out? We would like to use it for some blue/green deployments.

Copy link
Contributor

@bflad bflad left a 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!

"additional_version_weights": {
Type: schema.TypeMap,
Optional: true,
Elem: schema.TypeFloat,
Copy link
Contributor

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},

@@ -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{},
Copy link
Contributor

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{}))

@@ -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)
Copy link
Contributor

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
Copy link
Contributor

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.

@bflad bflad added the waiting-response Maintainers are waiting on response from community or contributor. label Jun 28, 2018
@eggsbenjamin
Copy link
Contributor Author

Hi @bflad , thanks for the response. Cool, I'll update the PR!

@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Jun 29, 2018
@eggsbenjamin
Copy link
Contributor Author

@bflad updated with the suggested changes. Cheers.

@bflad bflad removed the waiting-response Maintainers are waiting on response from community or contributor. label Jun 30, 2018
@bflad bflad added this to the v1.26.0 milestone Jul 2, 2018
Copy link
Contributor

@bflad bflad left a 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

@bflad
Copy link
Contributor

bflad commented Jul 4, 2018

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.

@ghost
Copy link

ghost commented Apr 4, 2020

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!

@ghost ghost locked and limited conversation to collaborators Apr 4, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement Requests to existing resources that expand the functionality or scope. service/lambda Issues and PRs that pertain to the lambda service. size/L Managed by automation to categorize the size of a PR.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants