CloudFormationとAWS CLIでEC2インスタンスをつくるメモ(YAML版)

foo.yaml

AWSTemplateFormatVersion: "2010-09-09"
Description: "Foo Server Template"

Parameters:
  UserDefinedHostName:
    Type: String

Resources:
  MyEC2Instance: #An inline comment
    Type: "AWS::EC2::Instance"
    Properties:
      ImageId: "ami-8422ebe2" # Ubuntu xenial hvm:ebs-ssd
      InstanceType: t2.micro
      BlockDeviceMappings:
        - DeviceName: /dev/sdm
          Ebs:
            VolumeType: gp2
            Iops: 200
            DeleteOnTermination: True
            VolumeSize: 8
      SubnetId: "subnet-xxx"
      KeyName: "xxx"
      SecurityGroupIds:
        - "sg-xxx"
        - "sg-xxx"
      Tags:
        - Key: "Name"
          Value: !Ref UserDefinedHostName

AWS CLI

aws cloudformation create-stack --stack-name foo-server --template-body file://foo.yml --parameters ParameterKey="UserDefinedHostName",ParameterValue="foo-server"