-
Notifications
You must be signed in to change notification settings - Fork 9.6k
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
provider/aws: aws_autoscaling_attachment resource #9146
provider/aws: aws_autoscaling_attachment resource #9146
Conversation
Delete: resourceAwsAutoscalingAttachmentDelete, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"group_name": &schema.Schema{ |
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.
I would call the attribute autoscaling_group_name
to be consistent with other resources in the same group.
func resourceAwsAutoscalingAttachmentCreate(d *schema.ResourceData, meta interface{}) error { | ||
asgconn := meta.(*AWSClient).autoscalingconn | ||
asgName := d.Get("group_name").(string) | ||
|
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.
Extra new line, I think.
|
||
_, err := asgconn.AttachLoadBalancers(attachElbInput) | ||
if err != nil { | ||
return fmt.Errorf("Failure registering asg with ELBs: %s", err) |
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.
We are endorsing usage of errwrap.Wrapf
as much as possible, thus this could be replaced with e.g.
return errwrap.Wrapf(fmt.Sprintf(
"Failure attaching Elastic Load Balancer %q to Auto Scaling Group %q: {{err}}",
elbName, asgName), err)
You can keep it simple too (like the original message).
|
||
d.SetId(resource.PrefixedUniqueId(fmt.Sprintf("%s-", asgName))) | ||
|
||
return nil |
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.
Often, as a customary thing, we would invoke the ReadFunc
so for example it would be:
return resourceAwsAutoscalingAttachmentRead(d, meta)
This is so that it would re-read the resource back ensuring that AWS already created it, etc. Primarily as a precaution (or guard, if you wish) against the eventually consistent nature of the AWS.
for _, i := range asg.LoadBalancerNames { | ||
if elbName == *i { | ||
d.Set("elb", elbName) | ||
found = true |
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.
I would break here, so that you don't check rest of them, as there will be no need at this point, I believe.
func resourceAwsAutoscalingAttachmentDelete(d *schema.ResourceData, meta interface{}) error { | ||
asgconn := meta.(*AWSClient).autoscalingconn | ||
asgName := d.Get("group_name").(string) | ||
|
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.
Extra new line, I think.
|
||
_, err := asgconn.DetachLoadBalancers(detachOpts) | ||
if err != nil { | ||
return fmt.Errorf("Failure detaching ELB from ASG: %s", err) |
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.
See comment above about the errwrap.Wrapf
.
LoadBalancerNames: []*string{aws.String(elbName)}, | ||
} | ||
|
||
_, err := asgconn.DetachLoadBalancers(detachOpts) |
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.
You can inline this to:
if _, err := asgconn.DetachLoadBalancers(detachOpts); err != nil {
Since the err
variable is not used below this line.
The good thing is, there isn't much in terms of needing to pool for state changes, which makes a lot of this much easier. |
@jrnt30 as per the conversation on Slack, we might need to update the name of the resource to be something like e.g. |
I held off on the renaming of the resource due to the conversation and potential expansion to also support an AutoScale group's attachment to the ALB Target Group resource. Before I make those changes, I need to read up on that resource more so I am going to leave the name and resource attributes alone for right now. If it's decided that we would prefer to use two distinct resources for the attachment (which as a user I'm not sure I would expect) I can make the name more explicit. |
I do think |
@simonvanderveldt I was a bit on the fence about this and am open to change. There are several resources that also provide the ability to attach to an Application Load Balancer. The idea was, since the resource's "api" footprint is essentially the same we may want to have those be the resource. Looking back at the other resources which support both of these (the autoscaling resource itself as an example), one common thread I see between them is that they seem to all call the same AWS API call. That might be a good enough litmus test for me to say "ALB and ELB resource associations should be two distinct resources, since they require different underlying AWS API calls for the attachment and detachment. Honestly I don't feel very strongly one way or another, but would appreciate if there were some guidelines. In general I think the question is, is it more explicit/maintainable/easier to approach if we break out the resources (alb vs. elb attachments being different in this case) or to keep them as a single resource? |
@jrnt30 afaik Terraform's basic way of working is to mimic the API's it wraps as closely as possible. I guess that should be documented somewhere, but can't really find it. |
So I have been looking through this. I believe this is the resource name we want. This will allow us the flexibility to use ELB and ALB target groups (going forward!) Tests are looking good:
Thanks for all the work here @jrnt30 :) P. |
* hashicorpGH-8755 - Adding in support to attach ASG to ELB as independent action * hashicorpGH-8755 - Adding in docs * hashicorpGH-8755 - Adjusting attribute name and responding to other PR feedback
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 have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further. |
Fixes #8755
Context:
Adds support for associating an ASG with an ELB without having to declare this when the ASG is created. The primary use case we wanted this for is to allow dynamically allocating ASGs to an ELB for Blue/Green deployments. With the current process, there is not a clean way for us to do this without some hacking of variables.
NOTE: I really struggled with the decision of keeping this as a unique resource or trying to branch the logic in the
aws_elb_attachment
resource. In the end, I opted for the later because it seemed cleaner, as the internals of the two resources are fairly different and it made the implementation and testing cleaner. That being said, if you would prefer it merged with the other resource, I can take a stab at that.@kwilczynski This is what we were discussing in slack the other day. Would love your 2 cents