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

Add support import Autoscaling Lifecycle Hook #9336

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions aws/resource_aws_autoscaling_lifecycle_hook.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ func resourceAwsAutoscalingLifecycleHook() *schema.Resource {
Update: resourceAwsAutoscalingLifecycleHookPut,
Delete: resourceAwsAutoscalingLifecycleHookDelete,

Importer: &schema.ResourceImporter{
State: resourceAwsAutoscalingLifecycleHookImport,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Expand Down Expand Up @@ -192,3 +196,19 @@ func getAwsAutoscalingLifecycleHook(d *schema.ResourceData, meta interface{}) (*
// lifecycle hook not found
return nil, nil
}

func resourceAwsAutoscalingLifecycleHookImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
idParts := strings.SplitN(d.Id(), "/", 2)
if len(idParts) != 2 || idParts[0] == "" || idParts[1] == "" {
return nil, fmt.Errorf("unexpected format (%q), expected <asg-name>/<lifecycle-hook-name>", d.Id())
}

asgName := idParts[0]
lifecycleHookName := idParts[1]

d.Set("name", lifecycleHookName)
d.Set("autoscaling_group_name", asgName)
d.SetId(lifecycleHookName)

return []*schema.ResourceData{d}, nil
}
17 changes: 17 additions & 0 deletions aws/resource_aws_autoscaling_lifecycle_hook_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,12 @@ func TestAccAWSAutoscalingLifecycleHook_basic(t *testing.T) {
resource.TestCheckResourceAttr("aws_autoscaling_lifecycle_hook.foobar", "lifecycle_transition", "autoscaling:EC2_INSTANCE_LAUNCHING"),
),
},
{
ResourceName: "aws_autoscaling_lifecycle_hook.foobar",
ImportState: true,
ImportStateIdFunc: testAccAWSAutoscalingLifecycleHookImportStateIdFunc("aws_autoscaling_lifecycle_hook.foobar"),
ImportStateVerify: true,
},
},
})
}
Expand Down Expand Up @@ -107,6 +113,17 @@ func testAccCheckAWSAutoscalingLifecycleHookDestroy(s *terraform.State) error {
return nil
}

func testAccAWSAutoscalingLifecycleHookImportStateIdFunc(resourceName string) resource.ImportStateIdFunc {
return func(s *terraform.State) (string, error) {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return "", fmt.Errorf("Not found: %s", resourceName)
}

return fmt.Sprintf("%s/%s", rs.Primary.Attributes["autoscaling_group_name"], rs.Primary.Attributes["name"]), nil
}
}

func testAccAWSAutoscalingLifecycleHookConfig(name string) string {
return fmt.Sprintf(`
resource "aws_launch_configuration" "foobar" {
Expand Down
8 changes: 8 additions & 0 deletions website/docs/r/autoscaling_lifecycle_hooks.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,11 @@ The following arguments are supported:
* `notification_metadata` - (Optional) Contains additional information that you want to include any time Auto Scaling sends a message to the notification target.
* `notification_target_arn` - (Optional) The ARN of the notification target that Auto Scaling will use to notify you when an instance is in the transition state for the lifecycle hook. This ARN target can be either an SQS queue or an SNS topic.
* `role_arn` - (Optional) The ARN of the IAM role that allows the Auto Scaling group to publish to the specified notification target.

## Import

AutoScaling Lifecycle Hook can be imported using the role autoscaling_group_name and name separated by `/`.

```
$ terraform import aws_aws_autoscaling_lifecycle_hook.test-lifecycle-hook asg-name/lifecycle-hook-name
```