创建Aws Cloudformation Ec2 t2.micro模板

时间:2016-05-11 13:47:51

标签: amazon-ec2 amazon-cloudformation aws-ec2

我想创建一个CloudFormation模板,其实例类型为" t2.micro"。但是,我无法找到有关此实例类型的任何示例。 Ec2的类型是" t2.micro"需要VPC等。

感谢。

3 个答案:

答案 0 :(得分:2)

您可以使用以下模板代码段 它将输出EC2实例公共DNS名称。

注意:我还没有测试过这个模板!

{
    "AWSTemplateFormatVersion": "2010-09-09",
    "Description": "CloudFormation template for creating an ec2 instance",
    "Parameters": {
        "KeyName": {
            "Description": "Key Pair name",
            "Type": "AWS::EC2::KeyPair::KeyName",
            "Default": "my_keypair_name"
        },
        "VPC": {
            "Type": "AWS::EC2::VPC",
            "Properties":{
                "CidrBlock": "10.0.0.0/16",
                "EnableDnsHostnames": "true"
            }
        },
        "Subnet":{
            "Type": "AWS::EC2::Subnet",
            "Properties": {
                "VpcId": {"Ref": "VPC"},
                "CidrBlock": "10.0.0.0/24",
                "AvailabilityZone": "us-east-1a"
            }
        },
        "InstanceType": {
            "Description": "Select one of the possible instance types",
            "Type": "String",
            "Default": "t2.micro",
            "AllowedValues": ["t2.micro", "t2.small", "t2.medium"]
        }
    },
    "Resources":{
        "SecurityGroup":{
            "Type": "AWS::EC2::SecurityGroup",
            "Properties": {
                "GroupDescription": "My security group",
                "VpcId": {"Ref": "VPC"},
                "SecurityGroupIngress": [{
                    "CidrIp": "0.0.0.0/0",
                    "FromPort": 22,
                    "IpProtocol": "tcp",
                    "ToPort": 22
                }]
            }
        },
        "Server": {
            "Type": "AWS::EC2::Instance",
            "Properties": {
                "ImageId": "ami-123456",
                "InstanceType": {"Ref": "InstanceType"},
                "KeyName": {"Ref": "KeyName"},
                "SecurityGroupIds": [{"Ref": "SecurityGroup"}],
                "SubnetId": {"Ref": "Subnet"}
            }
        }
    },
    "Outputs": {
        "PublicName": {
            "Value": {"Fn::GetAtt": ["Server", "PublicDnsName"]},
            "Description": "Public name (connect via SSH)"
        }   
    }
}

有关详细信息,请参阅:http://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html

答案 1 :(得分:1)

下面的堆栈允许创建EC2实例,只需指定诸如VPC id,子网ID,SG ID,实例类型以及ami id之类的参数,例如u可以在其中定义的实例类型默认为t2,micro。这已经成功测试并且没有错误。如果有任何疑问,请注释一下。######由MARK Machines创建  这样吧:

    {"Description": "CloudFormation template for creating an ec2 instance",
"Parameters": {
    "KeyName": {
        "Description": "Key Pair name",
        "Type": "AWS::EC2::KeyPair::KeyName",
        "Default": "xxx-xxx"
    },
    "VPC": {
        "Type": "AWS::EC2::VPC::Id",
        "Default":"givevpcid"
    },
    "Subnet":{
        "Type": "AWS::EC2::Subnet::Id",
        "Default": "givesubnetid"
    },
    "InstanceType": {
        "Description": "Select one of the possible instance types",
        "Type": "String",
        "Default": "t2.micro",
        "AllowedValues": ["t2.micro", "t2.small", "t2.medium"]
    },
    "SecurityGroup":{
         "Type": "AWS::EC2::SecurityGroup::Id",
         "Default" : "givesecuritygroupid",
         "AllowedValues": ["sg-xxxxx", "sg-yyy", "sg-zzz"]
    } 
},
"Resources":{
    "Server": {
        "Type": "AWS::EC2::Instance",
        "Properties": {
            "ImageId": "ami-098789xxxxxxxxx",
            "InstanceType": {"Ref": "InstanceType"},
            "KeyName": {"Ref": "KeyName"},
            "SecurityGroupIds": [{"Ref": "SecurityGroup"}],
            "SubnetId": {"Ref": "Subnet"}
        }
    }
},
"Outputs": {
    "PublicName": {
        "Value": {"Fn::GetAtt": ["Server", "PublicDnsName"]},
        "Description": "Public name (connect via SSH)"
    }   
}

}

答案 2 :(得分:1)

您可以使用下面的模板,效果很好。

single-instance.yml

AWSTemplateFormatVersion: 2010-09-09
Description: >-
  AWS CloudFormation Sample Template EC2InstanceWithSecurityGroupSample: Create
  an Amazon EC2 instance running the Amazon Linux AMI. The AMI is chosen based
  on the region in which the stack is run. This example creates an EC2 security
  group for the instance to give you SSH access. **WARNING** This template
  creates an Amazon EC2 instance. You will be billed for the AWS resources used
  if you create a stack from this 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: WebServer EC2 instance type
    Type: String
    Default: t2.micro
    ConstraintDescription: must be a valid EC2 instance type.
  SSHLocation:
    Description: The IP address range that can be used to SSH to the EC2 instances
    Type: String
    MinLength: '9'
    MaxLength: '18'
    Default: 0.0.0.0/0
    AllowedPattern: '(\d{1,3})\.(\d{1,3})\.(\d{1,3})\.(\d{1,3})/(\d{1,2})'
    ConstraintDescription: must be a valid IP CIDR range of the form x.x.x.x/x.
Mappings:
  AWSInstanceType2Arch:
    t2.micro:
      Arch: HVM64
  AWSInstanceType2NATArch:
    t2.micro:
      Arch: NATHVM64
  AWSRegionArch2AMI:
    us-east-1:
      HVM64: ami-0080e4c5bc078760e
      HVMG2: ami-0aeb704d503081ea6
Resources:
  EC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: !Ref InstanceType
      SecurityGroups:
        - !Ref InstanceSecurityGroup
      KeyName: !Ref KeyName
      ImageId: !FindInMap 
        - AWSRegionArch2AMI
        - !Ref 'AWS::Region'
        - !FindInMap 
          - AWSInstanceType2Arch
          - !Ref InstanceType
          - Arch
  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          CidrIp: !Ref SSHLocation
Outputs:
  InstanceId:
    Description: InstanceId of the newly created EC2 instance
    Value: !Ref EC2Instance
  AZ:
    Description: Availability Zone of the newly created EC2 instance
    Value: !GetAtt 
      - EC2Instance
      - AvailabilityZone
  PublicDNS:
    Description: Public DNSName of the newly created EC2 instance
    Value: !GetAtt 
      - EC2Instance
      - PublicDnsName
  PublicIP:
    Description: Public IP address of the newly created EC2 instance
    Value: !GetAtt 
      - EC2Instance
      - PublicIp 

然后运行以下命令:

aws cloudformation create-stack --template-body file://single-instance.yml --stack-name single-instance --parameters ParameterKey=KeyName,ParameterValue=sample ParameterKey=InstanceType,ParameterValue=t2.micro