@ -182,16 +182,93 @@ func resourceAwsAutoscalingGroup() *schema.Resource {
Computed : true ,
} ,
"initial_lifecycle_hook" : & schema . Schema {
Type : schema . TypeSet ,
Optional : true ,
Elem : & schema . Resource {
Schema : map [ string ] * schema . Schema {
"name" : {
Type : schema . TypeString ,
Required : true ,
} ,
"default_result" : {
Type : schema . TypeString ,
Optional : true ,
Computed : true ,
} ,
"heartbeat_timeout" : {
Type : schema . TypeInt ,
Optional : true ,
} ,
"lifecycle_transition" : {
Type : schema . TypeString ,
Required : true ,
} ,
"notification_metadata" : {
Type : schema . TypeString ,
Optional : true ,
} ,
"notification_target_arn" : {
Type : schema . TypeString ,
Optional : true ,
} ,
"role_arn" : {
Type : schema . TypeString ,
Optional : true ,
} ,
} ,
} ,
} ,
"tag" : autoscalingTagsSchema ( ) ,
} ,
}
}
func generatePutLifecycleHookInputs ( asgName string , cfgs [ ] interface { } ) [ ] autoscaling . PutLifecycleHookInput {
res := make ( [ ] autoscaling . PutLifecycleHookInput , 0 , len ( cfgs ) )
for _ , raw := range cfgs {
cfg := raw . ( map [ string ] interface { } )
input := autoscaling . PutLifecycleHookInput {
AutoScalingGroupName : & asgName ,
LifecycleHookName : aws . String ( cfg [ "name" ] . ( string ) ) ,
}
if v , ok := cfg [ "default_result" ] ; ok && v . ( string ) != "" {
input . DefaultResult = aws . String ( v . ( string ) )
}
if v , ok := cfg [ "heartbeat_timeout" ] ; ok && v . ( int ) > 0 {
input . HeartbeatTimeout = aws . Int64 ( int64 ( v . ( int ) ) )
}
if v , ok := cfg [ "lifecycle_transition" ] ; ok && v . ( string ) != "" {
input . LifecycleTransition = aws . String ( v . ( string ) )
}
if v , ok := cfg [ "notification_metadata" ] ; ok && v . ( string ) != "" {
input . NotificationMetadata = aws . String ( v . ( string ) )
}
if v , ok := cfg [ "notification_target_arn" ] ; ok && v . ( string ) != "" {
input . NotificationTargetARN = aws . String ( v . ( string ) )
}
if v , ok := cfg [ "role_arn" ] ; ok && v . ( string ) != "" {
input . RoleARN = aws . String ( v . ( string ) )
}
res = append ( res , input )
}
return res
}
func resourceAwsAutoscalingGroupCreate ( d * schema . ResourceData , meta interface { } ) error {
conn := meta . ( * AWSClient ) . autoscalingconn
var autoScalingGroupOpts autoscaling . CreateAutoScalingGroupInput
var asgName string
if v , ok := d . GetOk ( "name" ) ; ok {
asgName = v . ( string )
@ -200,69 +277,106 @@ func resourceAwsAutoscalingGroupCreate(d *schema.ResourceData, meta interface{})
d . Set ( "name" , asgName )
}
autoScalingGroupOpts . AutoScalingGroupName = aws . String ( asgName )
autoScalingGroupOpts . LaunchConfigurationName = aws . String ( d . Get ( "launch_configuration" ) . ( string ) )
autoScalingGroupOpts . MinSize = aws . Int64 ( int64 ( d . Get ( "min_size" ) . ( int ) ) )
autoScalingGroupOpts . MaxSize = aws . Int64 ( int64 ( d . Get ( "max_size" ) . ( int ) ) )
autoScalingGroupOpts . NewInstancesProtectedFromScaleIn = aws . Bool ( d . Get ( "protect_from_scale_in" ) . ( bool ) )
createOpts := autoscaling . CreateAutoScalingGroupInput {
AutoScalingGroupName : aws . String ( asgName ) ,
LaunchConfigurationName : aws . String ( d . Get ( "launch_configuration" ) . ( string ) ) ,
NewInstancesProtectedFromScaleIn : aws . Bool ( d . Get ( "protect_from_scale_in" ) . ( bool ) ) ,
}
updateOpts := autoscaling . UpdateAutoScalingGroupInput {
AutoScalingGroupName : aws . String ( asgName ) ,
}
initialLifecycleHooks := d . Get ( "initial_lifecycle_hook" ) . ( * schema . Set ) . List ( )
twoPhases := len ( initialLifecycleHooks ) > 0
minSize := aws . Int64 ( int64 ( d . Get ( "min_size" ) . ( int ) ) )
maxSize := aws . Int64 ( int64 ( d . Get ( "max_size" ) . ( int ) ) )
if twoPhases {
createOpts . MinSize = aws . Int64 ( int64 ( 0 ) )
createOpts . MaxSize = aws . Int64 ( int64 ( 0 ) )
updateOpts . MinSize = minSize
updateOpts . MaxSize = maxSize
if v , ok := d . GetOk ( "desired_capacity" ) ; ok {
updateOpts . DesiredCapacity = aws . Int64 ( int64 ( v . ( int ) ) )
}
} else {
createOpts . MinSize = minSize
createOpts . MaxSize = maxSize
if v , ok := d . GetOk ( "desired_capacity" ) ; ok {
createOpts . DesiredCapacity = aws . Int64 ( int64 ( v . ( int ) ) )
}
}
// Availability Zones are optional if VPC Zone Identifer(s) are specified
if v , ok := d . GetOk ( "availability_zones" ) ; ok && v . ( * schema . Set ) . Len ( ) > 0 {
autoScalingGroupOpts . AvailabilityZones = expandStringList ( v . ( * schema . Set ) . List ( ) )
create Opts. AvailabilityZones = expandStringList ( v . ( * schema . Set ) . List ( ) )
}
if v , ok := d . GetOk ( "tag" ) ; ok {
autoScalingGroupOpts . Tags = autoscalingTagsFromMap (
create Opts. Tags = autoscalingTagsFromMap (
setToMapByKey ( v . ( * schema . Set ) , "key" ) , d . Get ( "name" ) . ( string ) )
}
if v , ok := d . GetOk ( "default_cooldown" ) ; ok {
autoScalingGroupOpts . DefaultCooldown = aws . Int64 ( int64 ( v . ( int ) ) )
create Opts. DefaultCooldown = aws . Int64 ( int64 ( v . ( int ) ) )
}
if v , ok := d . GetOk ( "health_check_type" ) ; ok && v . ( string ) != "" {
autoScalingGroupOpts . HealthCheckType = aws . String ( v . ( string ) )
}
if v , ok := d . GetOk ( "desired_capacity" ) ; ok {
autoScalingGroupOpts . DesiredCapacity = aws . Int64 ( int64 ( v . ( int ) ) )
createOpts . HealthCheckType = aws . String ( v . ( string ) )
}
if v , ok := d . GetOk ( "health_check_grace_period" ) ; ok {
autoScalingGroupOpts . HealthCheckGracePeriod = aws . Int64 ( int64 ( v . ( int ) ) )
createOpts . HealthCheckGracePeriod = aws . Int64 ( int64 ( v . ( int ) ) )
}
if v , ok := d . GetOk ( "placement_group" ) ; ok {
autoScalingGroup Opts. PlacementGroup = aws . String ( v . ( string ) )
create Opts. PlacementGroup = aws . String ( v . ( string ) )
}
if v , ok := d . GetOk ( "load_balancers" ) ; ok && v . ( * schema . Set ) . Len ( ) > 0 {
autoScalingGroup Opts. LoadBalancerNames = expandStringList (
create Opts. LoadBalancerNames = expandStringList (
v . ( * schema . Set ) . List ( ) )
}
if v , ok := d . GetOk ( "vpc_zone_identifier" ) ; ok && v . ( * schema . Set ) . Len ( ) > 0 {
autoScalingGroup Opts. VPCZoneIdentifier = expandVpcZoneIdentifiers ( v . ( * schema . Set ) . List ( ) )
create Opts. VPCZoneIdentifier = expandVpcZoneIdentifiers ( v . ( * schema . Set ) . List ( ) )
}
if v , ok := d . GetOk ( "termination_policies" ) ; ok && len ( v . ( [ ] interface { } ) ) > 0 {
autoScalingGroup Opts. TerminationPolicies = expandStringList ( v . ( [ ] interface { } ) )
create Opts. TerminationPolicies = expandStringList ( v . ( [ ] interface { } ) )
}
if v , ok := d . GetOk ( "target_group_arns" ) ; ok && len ( v . ( * schema . Set ) . List ( ) ) > 0 {
autoScalingGroup Opts. TargetGroupARNs = expandStringList ( v . ( * schema . Set ) . List ( ) )
create Opts. TargetGroupARNs = expandStringList ( v . ( * schema . Set ) . List ( ) )
}
log . Printf ( "[DEBUG] AutoScaling Group create configuration: %#v" , autoScalingGroup Opts)
_ , err := conn . CreateAutoScalingGroup ( & autoScalingGroup Opts)
log . Printf ( "[DEBUG] AutoScaling Group create configuration: %#v" , create Opts)
_ , err := conn . CreateAutoScalingGroup ( & create Opts)
if err != nil {
return fmt . Errorf ( "Error creating Auto s caling Group: %s", err )
return fmt . Errorf ( "Error creating Auto S caling Group: %s", err )
}
d . SetId ( d . Get ( "name" ) . ( string ) )
log . Printf ( "[INFO] AutoScaling Group ID: %s" , d . Id ( ) )
if err := waitForASGCapacity ( d , meta , capacitySatifiedCreate ) ; err != nil {
if twoPhases {
for _ , hook := range generatePutLifecycleHookInputs ( asgName , initialLifecycleHooks ) {
if err = resourceAwsAutoscalingLifecycleHookPutOp ( conn , & hook ) ; err != nil {
return fmt . Errorf ( "Error creating initial lifecycle hooks: %s" , err )
}
}
_ , err = conn . UpdateAutoScalingGroup ( & updateOpts )
if err != nil {
return fmt . Errorf ( "Error setting AutoScaling Group initial capacity: %s" , err )
}
}
if err := waitForASGCapacity ( d , meta , capacitySatisfiedCreate ) ; err != nil {
return err
}
@ -480,7 +594,7 @@ func resourceAwsAutoscalingGroupUpdate(d *schema.ResourceData, meta interface{})
}
if shouldWaitForCapacity {
if err := waitForASGCapacity ( d , meta , capacitySati fiedUpdate) ; err != nil {
if err := waitForASGCapacity ( d , meta , capacitySati s fiedUpdate) ; err != nil {
return errwrap . Wrapf ( "Error waiting for AutoScaling Group Capacity: {{err}}" , err )
}
}