0

I am trying to create an Amazon EC2 instance then create an Amazon EBS volume and attach it to the instance. I am using a CloudFormation template for this. Unfortunately the stack creation is failing when attaching newly created volume to the instance with the following error:

Instance 'i-01eebc8c9c492c035' is not 'running'. (Service: AmazonEC2; Status Code: 400; Error Code: IncorrectState; Request ID: 635572fd-dd25-4a02-9306-6e22f88e13dc)

What I do not understand is, when the instance creation is complete, that means the instance is up and running. How can this error be possible?

I am using the following CloudFormation template:

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Description": "single instance template",
  "Parameters": {
    "InstanceType": {
      "Type": "String",
      "Default": "t2.micro"
    },
    "InstanceName": {
      "Type": "String",
      "Default": "test_CFT"
    },
    "RootVolumeSize": {
      "Type": "String",
      "Default": "50"
    },
    "Volume1Size": {
      "Type": "String",
      "Default": "8"
    },
    "Region": {
      "Type": "String",
      "Default": "us-east-2"
    },
    "AMIID": {
      "Type": "String",
      "Default": "ami-8c122be9"
    },
    "SubnetIds": {
      "Type": "CommaDelimitedList",
      "Default": "subnet-595e7422"
    },
    "SecurityGroupIDs": {
      "Type": "CommaDelimitedList",
      "Default": "sg-082faee8335351537"
    }
  },
  "Resources": {
    "Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "ImageId": {
          "Ref": "AMIID"
        },
        "InstanceType": {
          "Ref": "InstanceType"
        },
        "KeyName": "thehope",
        "NetworkInterfaces": [
          {
            "AssociatePublicIpAddress": "false",
            "DeviceIndex": "0",
            "SubnetId": {
              "Fn::Select": [
                0,
                {
                  "Ref": "SubnetIds"
                }
              ]
            },
            "GroupSet": {
              "Ref": "SecurityGroupIDs"
            }
          }
        ],
        "BlockDeviceMappings": [
          {
            "DeviceName": "/dev/sda1",
            "Ebs": {
              "VolumeSize": {
                "Ref": "RootVolumeSize"
              },
              "DeleteOnTermination": "true",
              "VolumeType": "gp2"
            }
          }
        ],
        "Tags": [
          {
            "Key": "Name",
            "Value": {
              "Ref": "InstanceName"
            }
          }
        ]
      }
    },
    "Volume1": {
      "DeletionPolicy": "Delete",
      "Properties": {
        "AvailabilityZone": {
          "Fn::GetAtt": [
            "Instance",
            "AvailabilityZone"
          ]
        },
        "Encrypted": "False",
        "Size": {
          "Ref": "Volume1Size"
        },
        "Tags": [
          {
            "Key": "Name",
            "Value": "New_volume"
          }
        ],
        "VolumeType": "gp2"
      },
      "Type": "AWS::EC2::Volume"
    },
    "VolumeAttachment1": {
      "Properties": {
        "Device": "/dev/xvdb",
        "InstanceId": {
          "Ref": "Instance"
        },
        "VolumeId": {
          "Ref": "Volume1"
        }
      },
      "Type": "AWS::EC2::VolumeAttachment"
    }
  },
  "Outputs": {
    "InstanceId": {
      "Description": "InstanceId of the instance",
      "Value": {
        "Ref": "Instance"
      }
    },
    "AZ": {
      "Description": "Availability Zone of the instance",
      "Value": {
        "Fn::GetAtt": [
          "Instance",
          "AvailabilityZone"
        ]
      }
    },
    "PrivateIP": {
      "Description": "PrivateIP of the instance",
      "Value": {
        "Fn::GetAtt": [
          "Instance",
          "PrivateIp"
        ]
      }
    }
  }
}

What am I doing wrong?

ryanwebjackson
  • 1,017
  • 6
  • 22
  • 36
varun
  • 11
  • 5
  • 1
    This is my first time posting in the stack over flow.sorry for the view. following is the Cloudformation Template. https://github.com/varun1020/practice_all/blob/master/cloudformation.json – varun Aug 25 '18 at 22:15
  • Have you tried adding a [DependsOn Attribute - AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-attribute-dependson.html) to the Volume, pointing to the EC2 instance? – John Rotenstein Aug 25 '18 at 22:55
  • I tried just now, It did not work and I also noticed that by default the instance is in stopped state when launched. how to change that behavior to running ? – varun Aug 26 '18 at 18:44

3 Answers3

0

Since you are creating new volumes, it would be easier to simply specify the volumes as part of the instance rather than specifying an Amazon EBS volume and then attaching it to the instance.

From Amazon EC2 Block Device Mapping Property - AWS CloudFormation:

This example sets the EBS-backed root device (/dev/sda1) size to 50 GiB, and another EBS-backed device mapped to /dev/sdm that is 100 GiB in size.

"BlockDeviceMappings" : [
   {
      "DeviceName" : "/dev/sda1",
      "Ebs" : { "VolumeSize" : "50" }
   },
   {
      "DeviceName" : "/dev/sdm",
      "Ebs" : { "VolumeSize" : "100" }
   }
] 
John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
  • Thanks for the response john. I want to encrypt the created volume in next step (which is not possible in this way) and also want to test on adding the delete on termination flag at AWS::EC2::VolumeAttachment step. I said test because, I am not sure it can be added at that step. – varun Aug 25 '18 at 22:50
0

That was quite fascinating, seeing how the instance stops!

When using Amazon Linux 2, it can be fixed by changing:

"DeviceName": "/dev/sda1",

into:

"DeviceName": "/dev/xvda",

Or, it can be fixed by using Amazon Linux (version 1) with /dev/sda1.

However, this doesn't fix your VolumeAttachment issue.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
0

I was facing the same issue until I changed the AMI in my template. Initially, I was testing with Linux AMI in the N.Virginia region where it failed but when I used a CENTOS AMI that I had subscribed to it works.