【AmazonLinux2】【gp3】EC2を最速でローンチするためのCloudFormationテンプレートを書いてみた

記事タイトルとURLをコピーする

はじめに

EC2(AmazonLinux2) + SG を作成する CloudFormation テンプレートを書きました。
最新の EBS タイプ gp3 に対応しています!

検証などの際に是非お使いください!

記事目安...5分

cfnテンプレートの詳細

# Yml file that builds the following AWS resources
# |Resource|Number|
# |---|---|
# |EC2|1|
# |Security Group|1|

Parameters:
# EC2
  AmiId:
    Description: Input an ami-id. Default is the latest AmazonLinux2 AMI.
    Type: AWS::SSM::Parameter::Value<String>
    Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
  InstanceType:
    Description: Input an Instance type.
    Type: String
    Default: t3.micro
  KeyPair:
    Description: Select your key pair.
    Type: "AWS::EC2::KeyPair::KeyName"
  InstanceName:
    Description: Input a instance name.
    Type: String
    Default: test
  SubnetId:
    Description: Select your SubnetId.
    Type: AWS::EC2::Subnet::Id
  AttachPublicIpAddress:
    Description: Set to true if you want to assign a public IP address, set to false if you don't.
    Type: String
    Default: false
    AllowedValues:
      - "true"
      - "false"
# SG
  VpcId:
    Description: Select your VpcId.
    Type: AWS::EC2::VPC::Id
  SshInboundIp:
    Description: Input an IP address you want to access with SSH. e.g. 0.0.0.0/0
    Type: String
    Default: "0.0.0.0/0"


Metadata:
  AWS::CloudFormation::Interface:
    ParameterGroups:
      -
        Label:
          default: EC2
        Parameters: 
          - AmiId
          - SubnetId
          - AttachPublicIpAddress
          - InstanceType
          - KeyPair
          - InstanceName
      -
        Label:
          default: SG
        Parameters: 
          - VpcId
          - SshInboundIp

Resources:
# EC2
  MyInstance:
    Type: AWS::EC2::Instance
    Properties: 
      EbsOptimized: true
      DisableApiTermination: false
      InstanceInitiatedShutdownBehavior: stop
      Monitoring: false
      Tenancy: default
      ImageId: !Ref AmiId
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyPair
      BlockDeviceMappings: 
        - DeviceName: /dev/xvda
          Ebs: 
            VolumeSize: 8
            VolumeType: gp3
            Iops: 3000
            DeleteOnTermination: true
      NetworkInterfaces:
        - AssociatePublicIpAddress: !Ref AttachPublicIpAddress
          DeleteOnTermination: true
          GroupSet: 
            - !Ref MySG
          DeviceIndex: 0
          SubnetId: !Ref SubnetId
      Tags: 
        - Key: Name
          Value: !Sub ${InstanceName}
      # UserData: String

# SG
  MySG:
    Type: AWS::EC2::SecurityGroup
    Properties: 
      VpcId: !Ref VpcId
      GroupDescription: !Sub Security Group attached to ${InstanceName}
      GroupName: !Sub sg_${InstanceName}
      SecurityGroupIngress:
        -   CidrIp: !Ref SshInboundIp
            Description: SSH port
            FromPort: 22
            IpProtocol: tcp
            # SourceSecurityGroupId: String
            # SourceSecurityGroupName: String
            ToPort: 22


Outputs:
  OutputsInstanceId:
    Description: EC2 Instance ID
    Value: !Ref MyInstance
  OutputsSgId:
    Description: SG ID
    Value: !Ref MySG

テンプレートで構築されるリソースの詳細は以下です。

〇 EC2 × 1

Key Value
OS AmazonLinux2
EBS type gp3

〇 SG × 1

Key Value
Inbound SSH(22)
Outbound ALL

構築物の ID は、アウトプットセクションに記載されるので、合わせてご確認ください

注意事項

  • AMI は、SSM 公開パラメータストアから最新のものを取得します。

  • このテンプレートで構築した SG は EC2 に自動的にアタッチされます。

菅谷 歩 (記事一覧)