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

r/aws_autoscaling_group: Add support for launch template #4305

Merged
merged 1 commit into from
Apr 26, 2018
Merged

r/aws_autoscaling_group: Add support for launch template #4305

merged 1 commit into from
Apr 26, 2018

Conversation

gordonbondon
Copy link
Contributor

@gordonbondon gordonbondon commented Apr 21, 2018

Adds support to launch ASG from template

I'll file separate PRs for #4267 and #4264 . They are using the same structure.

Tests:

→ vaulted -n my -- make testacc TEST=./aws TESTARGS='-run=TestAccAWSAutoScalingGroup_launchTemplate'
Password:
Enter your MFA code: 272073
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAutoScalingGroup_launchTemplate -timeout 120m
=== RUN   TestAccAWSAutoScalingGroup_launchTemplate
--- PASS: TestAccAWSAutoScalingGroup_launchTemplate (103.86s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	104.296s

→ vaulted -n my -- make testacc TEST=./aws TESTARGS='-run=TestAccAWSAutoScalingGroup_WithLoadBalancer'
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAutoScalingGroup_WithLoadBalancer -timeout 120m
=== RUN   TestAccAWSAutoScalingGroup_WithLoadBalancer
--- PASS: TestAccAWSAutoScalingGroup_WithLoadBalancer (535.68s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	536.105s

→ vaulted -n my -- make testacc TEST=./aws TESTARGS='-run=TestAccAWSAutoScalingGroup_launchTemplate_toLaunchConfig'
Password:
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAutoScalingGroup_launchTemplate_toLaunchConfig -timeout 120m
=== RUN   TestAccAWSAutoScalingGroup_launchTemplate_toLaunchConfig
--- PASS: TestAccAWSAutoScalingGroup_launchTemplate_toLaunchConfig (144.08s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	144.516s

@ghost ghost added size/L Managed by automation to categorize the size of a PR. labels Apr 21, 2018
@gordonbondon gordonbondon changed the title r/aws_autoscaling_group: Add support for launch template [WIP] r/aws_autoscaling_group: Add support for launch template Apr 22, 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.

@gordonbondon is this still work in progress? 😄 Looks in pretty good shape.

// need an AMI that listens on :80 at boot, this is:
// bitnami-nginxstack-1.6.1-0-linux-ubuntu-14.04.1-x86_64-hvm-ebs-ami-99f5b1a9-3
image_id = "ami-b5b3fc85"
image_id = "${data.aws_ami.test_ami.id}"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😍

@ghost ghost added size/XL Managed by automation to categorize the size of a PR. and removed size/L Managed by automation to categorize the size of a PR. labels Apr 23, 2018
@gordonbondon
Copy link
Contributor Author

@bflad almost done. I found a couple of edge cases and tried to resolve them.

After my last changes acceptance test is failing like this:

→ vaulted -n my -- make testacc TEST=./aws TESTARGS='-run=TestAccAWSAutoScalingGroup_launchTemplate_update'
Password:
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAutoScalingGroup_launchTemplate_update -timeout 120m
=== RUN   TestAccAWSAutoScalingGroup_launchTemplate_update
--- FAIL: TestAccAWSAutoScalingGroup_launchTemplate_update (60.55s)
	testing.go:518: Step 2 error: Check failed: Check 2/3 error: aws_autoscaling_group.bar: Attribute 'launch_configuration' found when not expected
FAIL
exit status 1
FAIL	github.com/terraform-providers/terraform-provider-aws/aws	61.000s
make: *** [testacc] Error 1

But when I run the same steps manually I see "launch_configuration": "" in terraform.tfstate. Maybe you can help me to sort this out.

My other concern is that I don't really like how expandLaunchTemplateSpecification is done, but I don't have an idea on how to do it better. Basic flow should be like this:

  • Only one of name or id can be set for LaunchTemplateSpecification but DescribeAustoScaling group returns both of them.
  • We need to check that only one of them is set
  • When resource is updated check which one has changed and send it
  • When none of them has changed - send first available

@ghost ghost added size/L Managed by automation to categorize the size of a PR. and removed size/XL Managed by automation to categorize the size of a PR. labels Apr 23, 2018
@gordonbondon
Copy link
Contributor Author

And maybe expandLaunchTemplateSpecification should return interface instead of *autoscaling.LaunchTemplateSpecification so I can somehow make it work for https://docs.aws.amazon.com/sdk-for-go/api/service/ec2/#FleetLaunchTemplateSpecification which has the exact structure but another type.

@bflad
Copy link
Contributor

bflad commented Apr 23, 2018

I wouldn't try to get fancy merging types. We prefer (mostly) duplicated code over magic/indirection as it makes community maintenance easier.

@bflad
Copy link
Contributor

bflad commented Apr 23, 2018

Only one of name or id can be set for LaunchTemplateSpecification but DescribeAustoScaling group returns both of them.

Optional: true and Computed: true should handle this situation fine.

We need to check that only one of them is set

Why? Does the API return an error when both are specified or is this an issue during updates? If it does not error when both are specified, I would assume we're fine passing whichever is defined. If it does error on update because of ID/name mismatch, then we might need to introduce a CustomizeDiff function that calls d.SetNewComputed() on the opposite attribute

@gordonbondon
Copy link
Contributor Author

gordonbondon commented Apr 23, 2018

On describe it returns:

"LaunchTemplate": {
                "LaunchTemplateId": "lt-057f89ebf426f3765",
                "LaunchTemplateName": "test_template",
                "Version": "$Default"
            }

But if you set both on Create/Update request it will return error:

An error occurred (ValidationError) when calling the UpdateAutoScalingGroup operation: You must use a valid fully-formed launch template. A launch template ID and a launch template name cannot be specified in the same request.

Even if id matches the name.

@gordonbondon
Copy link
Contributor Author

CustomizedDiff and SetnewComputed is great! I was looking for something like this but did not succeeded. I'll try to switch to this approach

@bflad bflad added enhancement Requests to existing resources that expand the functionality or scope. service/autoscaling Issues and PRs that pertain to the autoscaling service. labels Apr 23, 2018
@ghost ghost added the size/L Managed by automation to categorize the size of a PR. label Apr 24, 2018
@gordonbondon gordonbondon changed the title [WIP] r/aws_autoscaling_group: Add support for launch template r/aws_autoscaling_group: Add support for launch template Apr 24, 2018
@gordonbondon
Copy link
Contributor Author

I've added CustomizedDiff and now everything works fine.
Tests after fix:

→ vaulted -n my -- make testacc TEST=./aws TESTARGS='-run=TestAccAWSAutoScalingGroup_launchTemplate'
Password:
==> Checking that code complies with gofmt requirements...
TF_ACC=1 go test ./aws -v -run=TestAccAWSAutoScalingGroup_launchTemplate -timeout 120m
=== RUN   TestAccAWSAutoScalingGroup_launchTemplate
--- PASS: TestAccAWSAutoScalingGroup_launchTemplate (58.47s)
=== RUN   TestAccAWSAutoScalingGroup_launchTemplate_update
--- PASS: TestAccAWSAutoScalingGroup_launchTemplate_update (109.88s)
PASS
ok  	github.com/terraform-providers/terraform-provider-aws/aws	168.801s

@gordonbondon
Copy link
Contributor Author

gordonbondon commented Apr 24, 2018

I've also tried to add plan time validation for attributes like this:

func(diff *schema.ResourceDiff, v interface{}) error {
                // Plan time validation for asg instance configuration
                if diff.Id() == "" || !diff.HasChange("launch_configuration") && !diff.HasChange("launch_template") {
                    return nil
                }
                launchConfigurationValue, launchConfigurationOk := diff.GetOk("launch_configuration")
                launchTemplateValue, launchTemplateOk := diff.GetOk("launch_template")
                lc := launchConfigurationOk && launchConfigurationValue.(string) != ""
                lt := launchTemplateOk && len(launchTemplateValue.([]interface{})) > 0
                if lc || lt {
                    return nil
                }
                // return nil
                return errors.New("One of `launch_configuration` or `launch_template` must be set for an autoscaling group")
            },

But it does not seem to work when attributes are passed from resources that does not exist yet. For example launch_configuration = "${aws_launch_configuration.test.name}"

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.

Looks good! 🚀

20 tests passed (all tests)
=== RUN   TestAccAWSAutoScalingGroup_autoGeneratedName
--- PASS: TestAccAWSAutoScalingGroup_autoGeneratedName (59.68s)
=== RUN   TestAccAWSAutoScalingGroup_serviceLinkedRoleARN
--- PASS: TestAccAWSAutoScalingGroup_serviceLinkedRoleARN (80.59s)
=== RUN   TestAccAWSAutoScalingGroup_classicVpcZoneIdentifier
--- PASS: TestAccAWSAutoScalingGroup_classicVpcZoneIdentifier (88.10s)
=== RUN   TestAccAWSAutoScalingGroup_namePrefix
--- PASS: TestAccAWSAutoScalingGroup_namePrefix (88.62s)
=== RUN   TestAccAWSAutoScalingGroup_launchTemplate
--- PASS: TestAccAWSAutoScalingGroup_launchTemplate (90.12s)
=== RUN   TestAccAWSAutoScalingGroup_emptyAvailabilityZones
--- PASS: TestAccAWSAutoScalingGroup_emptyAvailabilityZones (96.52s)
=== RUN   TestAccAWSAutoScalingGroup_withMetrics
--- PASS: TestAccAWSAutoScalingGroup_withMetrics (105.35s)
=== RUN   TestAccAWSAutoScalingGroup_withPlacementGroup
--- PASS: TestAccAWSAutoScalingGroup_withPlacementGroup (153.29s)
=== RUN   TestAccAWSAutoScalingGroup_VpcUpdates
--- PASS: TestAccAWSAutoScalingGroup_VpcUpdates (160.19s)
=== RUN   TestAccAWSAutoScalingGroup_importBasic
--- PASS: TestAccAWSAutoScalingGroup_importBasic (166.02s)
=== RUN   TestAccAWSAutoScalingGroup_launchTemplate_update
--- PASS: TestAccAWSAutoScalingGroup_launchTemplate_update (182.77s)
=== RUN   TestAccAWSAutoScalingGroup_ALB_TargetGroups
--- PASS: TestAccAWSAutoScalingGroup_ALB_TargetGroups (185.33s)
=== RUN   TestAccAWSAutoScalingGroup_enablingMetrics
--- PASS: TestAccAWSAutoScalingGroup_enablingMetrics (189.68s)
=== RUN   TestAccAWSAutoScalingGroup_terminationPolicies
--- PASS: TestAccAWSAutoScalingGroup_terminationPolicies (195.38s)
=== RUN   TestAccAWSAutoScalingGroup_tags
--- PASS: TestAccAWSAutoScalingGroup_tags (234.31s)
=== RUN   TestAccAWSAutoScalingGroup_suspendingProcesses
--- PASS: TestAccAWSAutoScalingGroup_suspendingProcesses (262.35s)
=== RUN   TestAccAWSAutoScalingGroup_initialLifecycleHook
--- PASS: TestAccAWSAutoScalingGroup_initialLifecycleHook (270.01s)
=== RUN   TestAccAWSAutoScalingGroup_basic
--- PASS: TestAccAWSAutoScalingGroup_basic (345.06s)
=== RUN   TestAccAWSAutoScalingGroup_ALB_TargetGroups_ELBCapacity
--- PASS: TestAccAWSAutoScalingGroup_ALB_TargetGroups_ELBCapacity (354.08s)
=== RUN   TestAccAWSAutoScalingGroup_WithLoadBalancer
--- PASS: TestAccAWSAutoScalingGroup_WithLoadBalancer (697.77s)

@bflad bflad added this to the v1.17.0 milestone Apr 26, 2018
@bflad bflad merged commit 0862f2a into hashicorp:master Apr 26, 2018
bflad added a commit that referenced this pull request Apr 26, 2018
@gordonbondon gordonbondon deleted the asg-launch-template branch April 26, 2018 10:49
@bflad
Copy link
Contributor

bflad commented May 2, 2018

This has been released in version 1.17.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 6, 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 6, 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/autoscaling Issues and PRs that pertain to the autoscaling 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.

2 participants