At least one Resources member must be defined ...error in cloud formation ec2

20,258

Solution 1

Your examples don't appear to have defined any AWS::EC2::Instance resources, which are what tell CloudFormation to provision EC2 instances.

Here's a very minimalist CloudFormation template that will create one t2.micro instance. Check out the AWS::EC2::Instance resource definition for details on what properties can be added to customize it.

{
  "AWSTemplateFormatVersion": "2010-09-09",
  "Resources": {
    "ExampleEc2Instance": {
      "Type": "AWS::EC2::Instance",
      "Properties": {
        "InstanceType": "t2.micro",
        "ImageId" : "ami-a0cfeed8"
      }
    }
  }
}

Finding a valid AMI for a particular operating system, configuration, and region can be a bit tricky. This walkthrough discusses a strategy to automate the looking up of AMIs using AWS Lambda.

Solution 2

You asked for a simple template to launch an EC2 instance, so there you go. Please remember that this is only the basic and it can be expanded with so many more options. Please let me know if you need any specific help here. Good luck.

{
  "AWSTemplateFormatVersion" : "2010-09-09",
  "Description" : "AWS CloudFormation Sample Template",
  "Parameters" : {
    "KeyName": {
      "Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
      "Type": "AWS::EC2::KeyPair::KeyName",
      "ConstraintDescription" : "must be the name of an existing EC2 KeyPair."
    },
    "InstanceType": {
      "Description": "EC2 instance type",
      "Type": "String",
      "Default": "t2.micro"
    },
    "ImageID": {
      "Description": "EC2 instance type",
      "Type": "String",
      "Default": "ami-xxxxxxxxxxxxxxx"
    },
    "SecurityGroupId" : {
      "Type" : "String",
      "Description" : "The SecurityGroupId of an existing EC2 SecurityGroup in your Virtual Private Cloud (VPC)",
      "Default": "sg-xxxxxxxx"
    },
    "SubnetID": {
      "Description": "Subnets where logging EC2 instances can be deployed, must be in same VPC as selected above",
      "Type": "String",
      "ConstraintDescription": "must be valid subnet.",
      "Default": "subnet-xxxxxxxxx"
    }
  },
  "Resources" : {
    "EC2Instance" : {
      "Type" : "AWS::EC2::Instance",
     "Properties" : {
        "InstanceType" : { "Ref" : "InstanceType" },
        "SecurityGroupIds" : [{ "Ref" : "SecurityGroupId"}],
        "KeyName" : { "Ref" : "KeyName" },
        "ImageId" : { "Ref" : "ImageID" },
        "InstanceInitiatedShutdownBehavior" : "stop",
        "SubnetId" : { "Ref": "SubnetID" }
      }
    }
  },
  "Outputs" : {
    "InstanceId" : {
      "Description" : "InstanceId of the newly created EC2 instance",
      "Value" : { "Ref" : "EC2Instance" }
    }
  }
}
Share:
20,258
pallavi
Author by

pallavi

Updated on August 14, 2020

Comments

  • pallavi
    pallavi over 3 years
    I tried other templates from the net but still getting the same error. Error
    

    message: Template contains errors.: Template format error: At least one Resources member must be defined.

    {
    "AWSTemplateFormatVersion" : "2010-09-09",
    
    "Description" : "TTND AWS CloudFormation template to launch first instance",
    
    "Parameters" : {
    
    "KeyName" : {
    "Description" : "EC2 Key Pair for SSH Access",
    "Default" : "sample",
    "MinLength": "1",
    "MaxLength": "64",
    "AllowedPattern" : "[-_ a-zA-Z0-9]*",
    "ConstraintDescription" : "can contain only alphanumeric characters, spaces, dashes and underscores."
    },
    "InstanceType" : {
    "Description" : "Instance1 EC2 instance type",
    "Type" : "String",
    "Default" : "t2.micro",
    "AllowedValues" : [ "t2.micro","m1.small","m1.medium","m1.large"],
    "ConstraintDescription" : "must be a valid EC2 instance type."
    }
    },
    "Mappings" : {
        "AWSInstanceMapping" : {
          "t2.micro"    : { "Arch" : "64" },
          "t2.small"    : { "Arch" : "64" },
          "t2.medium"   : { "Arch" : "64" },
          "t2.large"    : { "Arch" : "64" },
          "m3.medium"   : { "Arch" : "64" },
          "m4.large"   : { "Arch" : "64" },
          "m4.xlarge"  : { "Arch" : "64" },
          "m4.2xlarge"  : { "Arch" : "64" }
        }
        },
    
        "InstanceAMI" : {
          "us-east-1"      : { "64" : "ami-09ca8e1e" }
        },
    

    I tried other templates for net but same error I am getting

    "Resources" : {
    
        "VPC" : {
          "Type" : "AWS::EC2::VPC",
          "Properties" : {
            "CidrBlock" : "10.0.0.0/16",
            "Tags" : [
              {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
              { "Key": "Name", "Value": "Project_VPC"},
              {"Key" : "Network", "Value" : "Public" }
            ]
          }
        },
    
        "PublicSubnet" : {
          "Type" : "AWS::EC2::Subnet",
          "Properties" : {
            "VpcId" : { "Ref" : "VPC" },
            "CidrBlock" : "10.0.0.0/24",
            "Tags" : [
              {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
              {"Key" : "Network", "Value" : "Public" },
              { "Key": "Name", "Value": "Project_Public_Subnet"}
            ]
          }
        },
    
        "InternetGateway" : {
          "Type" : "AWS::EC2::InternetGateway",
          "Properties" : {
            "Tags" : [
              {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
              {"Key" : "Network", "Value" : "Public" },
              { "Key": "Name", "Value": "Project_Internetgateway"}
            ]
          }
        },
    
        "AttachGateway" : {
           "Type" : "AWS::EC2::VPCGatewayAttachment",
           "Properties" : {
             "VpcId" : { "Ref" : "VPC" },
             "InternetGatewayId" : { "Ref" : "InternetGateway" }
           }
        },
    "PublicRouteTable" : {
          "Type" : "AWS::EC2::RouteTable",
          "Properties" : {
            "VpcId" : {"Ref" : "VPC"},
            "Tags" : [
              {"Key" : "Application", "Value" : { "Ref" : "AWS::StackId"} },
              {"Key" : "Network", "Value" : "Public" },
              { "Key": "Name", "Value": "cloudwords_public_routetable"}
            ]
          }
        },                                                           
    

    some of code I deleted for posting it is giving large code error so

    "Outputs" : {
     "InstanceId" : {
     "Description" : "InstanceId of the newly created instance",
     "Value" : { "Ref" : "Instance" }
     },
        }
    }
    

    If anyone has a simple template of Launching an AWS EC2 instance using CloudFormation Template please post

  • pallavi
    pallavi over 5 years
    still getting same error "At least one Resources member must be defined." "ImageId" : "ami-a0cfeed8" is this ami I need to create 1st
  • pallavi
    pallavi over 5 years
    same error getting . "ImageId" : "ami-a0cfeed8" confused about this . ami which I created I need to take that id or else??
  • Tom
    Tom over 5 years
    The AMI "ami-a0cfeed8" is already created in the Oregon region. You can see some default ones in EC2 -> Launch Instance (the ami-[hex] to the right of the AMI names are the AMI ids). It should work if you set that AMI to a valid one in your region, save the template to a file, and go to CloudFormation -> Create Stack -> "Upload a template to Amazon S3" and then proceed with naming/creating the stack.
  • kenlukas
    kenlukas over 5 years
    Replace that with an AMI id of your choice. Either one you created or one from the Marketplace.
  • pallavi
    pallavi over 5 years
    able to create Ec2 by your steps but when I choose design template in Json this error coming ... "At least one Resources member must be defined"