From fe119f82382e080aa672a9d5a3d435594e24a2b1 Mon Sep 17 00:00:00 2001 From: Radek Simko Date: Mon, 15 May 2017 06:50:41 +0200 Subject: [PATCH] provider/aws: Show state reason when EC2 instance fails to launch --- .../providers/aws/resource_aws_instance.go | 21 ++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/builtin/providers/aws/resource_aws_instance.go b/builtin/providers/aws/resource_aws_instance.go index 153334ded36f..022611f11a20 100644 --- a/builtin/providers/aws/resource_aws_instance.go +++ b/builtin/providers/aws/resource_aws_instance.go @@ -1018,10 +1018,29 @@ func InstanceStateRefreshFunc(conn *ec2.EC2, instanceID string) resource.StateRe } i := resp.Reservations[0].Instances[0] - return i, *i.State.Name, nil + state := *i.State.Name + + if state == "terminated" { + return i, state, fmt.Errorf("Failed to launch instance. Reason: %s", + stringifyStateReason(i.StateReason)) + + } + + return i, state, nil } } +func stringifyStateReason(sr *ec2.StateReason) string { + if sr.Message != nil { + return *sr.Message + } + if sr.Code != nil { + return *sr.Code + } + + return sr.String() +} + func readBlockDevices(d *schema.ResourceData, instance *ec2.Instance, conn *ec2.EC2) error { ibds, err := readBlockDevicesFromInstance(instance, conn) if err != nil {