Skip to content

Commit

Permalink
Merge pull request #9336 from chaspy/chaspy/import_aws_autoscaling_li…
Browse files Browse the repository at this point in the history
…fecycle_hook

Add support import Autoscaling Lifecycle Hook
  • Loading branch information
bflad authored Jul 16, 2019
2 parents feacc80 + b656507 commit b9af2eb
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
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
```

0 comments on commit b9af2eb

Please sign in to comment.