👨‍💻

How to create ec2 on exist vpc by cloud formation

2022/07/01に公開

How to create ec2 on exist vpc by cloud formation

  • This is demo about how to create ec2 on exist vpc by cloud formation

agenda

  • Prerequisite
  • Preparing template by yaml
  • Create ec2 by cloud formation
  • Delete the cloud formation stack

Prerequisite

  • you need to create under the resources before try demo
    • vpc
    • key pair
    • AMI
  • This demo created by ap-northeast-1

Preparing template by yaml

  • Most Parameter value should be valid resources
  • you need to prepare template in your local enviroment
AWSTemplateFormatVersion: "2010-09-09"
Description: Create EC2 Instance
Parameters:
  InstanceType:
    Description: WebServer EC2 instance type
    Type: String
    Default: t2.micro
    AllowedValues:
    - t1.micro
    - t2.nano
    - t2.micro
    - t2.small
    - t2.medium
    - t2.large
    ConstraintDescription: must be a valid EC2 instance type
  SubnetId:
    Type: String
    Default: your SubnetId
    AllowedValues:
    - your SubnetId
    - your SubnetId
    ConstraintDescription: must be a valid SbunetID
  EnviromentType:
    Description: the enviroment type
    Type: String
    Default: test
    AllowedValues:
      - test
      - prod
    ConstraintDescription: must be a test or prod
  KeyName: 
    Description : Name of an existing EC2 KeyPair.
    Type: AWS::EC2::KeyPair::KeyName
    ConstraintDescription : Can contain only ASCII characters.
  SSHLocation:
    Description: 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:
  RegionMap:
    ap-northeast-1:
      test: your amiID
      prod: your amiID
    ap-southeast-2:
      prod: your amiID
      test: your amiID


Description: Create EC2 Instance
Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties: 
      ImageId: !FindInMap [RegionMap, !Ref "AWS::Region", !Ref EnviromentType]
      InstanceType: !Ref InstanceType
      SubnetId: !Ref SubnetId
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeType: gp2
            VolumeSize: 10
      Tags:
      - Key: Name
        Value: zenn-ec2-test
      KeyName: !Ref KeyName
      SecurityGroupIds:
         - !GetAtt "InstanceSecurityGroup.GroupId"

  InstanceSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: connect with ssh 
      VpcId: vpc-03d05352402ca114e
      SecurityGroupIngress:
        -
          IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: !Ref SSHLocation

Create ec2 by cloud formation

  • Create cloud formation stack

  • Access to ap-northeast-1.console.aws.amazon.com/cloudformation/home?region=ap-northeast-1

  • Click the 「スタックの作成」

  • Select the 「新しいリソースを使用(標準)」

  • Select the 「テンプレートの準備完了」

  • Select the 「テンプレートファイルのアップロード」

  • Click the 「ファイルの選択」

    • then chose your template
  • Chose the parameter value

  • SubnetId is masked should be selceted your subnetid

  • This stage you don't have to change any value

  • Scroll to the bottom of page

  • Click the 「次へ」

  • This stage is checked value which is your selected

  • If there is fine click the 「スタックの作成」  bottom of page

  • Cloud Formation start to create resource

  • Wait few miniutes

  • Complete to create resource

  • Access to https://ap-northeast-1.console.aws.amazon.com/ec2/v2/home?region=ap-northeast-1#Instances:v=3

  • you can see the instance

Delete the cloud formation stack

  • Access to ap-northeast-1.console.aws.amazon.com/cloudformation/home?region=ap-northeast-1

  • Check the cloud formation stack

  • Click the 「削除」

  • Click the 「スタックの削除」

  • Cloud Formation start to delete
  • Click the refresh button

  • Cloud Formation stack is deleted

done

you can delete resource which is created by cloud formation just delete the cloud formation stack

Don't delete resource by hand it is possible to miss to delete the some resource

Discussion