diff --git a/aws/resource_aws_autoscaling_lifecycle_hook.go b/aws/resource_aws_autoscaling_lifecycle_hook.go index 500924eac48..7508a9d0f25 100644 --- a/aws/resource_aws_autoscaling_lifecycle_hook.go +++ b/aws/resource_aws_autoscaling_lifecycle_hook.go @@ -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, @@ -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 /", 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 +} diff --git a/aws/resource_aws_autoscaling_lifecycle_hook_test.go b/aws/resource_aws_autoscaling_lifecycle_hook_test.go index 4a28cd6a26c..a6eee055f7d 100644 --- a/aws/resource_aws_autoscaling_lifecycle_hook_test.go +++ b/aws/resource_aws_autoscaling_lifecycle_hook_test.go @@ -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, + }, }, }) } @@ -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" { diff --git a/website/docs/r/autoscaling_lifecycle_hooks.html.markdown b/website/docs/r/autoscaling_lifecycle_hooks.html.markdown index 7a3e7b0e188..9880734906c 100644 --- a/website/docs/r/autoscaling_lifecycle_hooks.html.markdown +++ b/website/docs/r/autoscaling_lifecycle_hooks.html.markdown @@ -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 +```