0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AWS CloudFormation テンプレート

Posted at
# AWSテンプレートのバージョン。存在する唯一のバージョンが下記なので実質固定。
AWSTemplateFormatVersion: 2010-09-09
# テンプレートの説明。実行時に表示されるので記載がベター。
Description: hello-world-rails Template

Parameters:
  InstanceType:
    Type: String
    Default: t2.micro
    AllowedValues:
      - t2.micro
      - t2.small
      - t2.midium
  AvailabilityZoneType:
    Type: CommaDelimitedList
    Default: ap-northeast-1a, ap-northeast-1c
  KeyPair:
    Type: AWS::EC2::KeyPair::KeyName
    Default: jojotech
    Description: Select Key Pair Name.

# リソースの定義
Resources:
  # VPC定義
  HelloWorldRailsVPC:
    Type: AWS::EC2::VPC
    Properties:
      CidrBlock: 10.0.0.0/24
      EnableDnsHostnames: true

      EnableDnsSupport: true
      Tags:
        - Key: Name
          Value: hello-world-rails-vpc

  # EC2Instance定義
  HelloWorldRailsEC2Instace:
    Type: AWS::EC2::Instance
    Properties:
      InstanceType: !Ref InstanceType
      AvailabilityZone: !Select [0, !Ref AvailabilityZoneType]
      ImageId: ami-0d7ed3ddb85b521a6
      KeyName: !Ref KeyPair
      Monitoring: false
      InstanceInitiatedShutdownBehavior: stop
      NetworkInterfaces:
        - AssociatePublicIpAddress: "true"
          DeviceIndex: "0"
          SubnetId: !Ref HelloWorldRailsSubnetAZ1a      
          GroupSet: 
           - !Ref HelloWorldRailsEc2SecurityGroup
      Tags:
        - Key: Name
          Value: hello-world-rails-ec2

  # ElasticIp定義
  #HelloWorldRailsElasticIp:
  #  Type: AWS::EC2::EIP
  #  Properties:
  #    Domain: 52.197.179.237
  #  DependsOn: HelloWorldRailsEC2Instace

  # Subnet定義
  HelloWorldRailsSubnetAZ1a:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.0.0/27
      VpcId: !Ref HelloWorldRailsVPC
      AvailabilityZone: !Select [0, !Ref AvailabilityZoneType]
      Tags:
        - Key: Name
          Value: hello-world-rails-subnet-az1

  HelloWorldRailsSubnetAZ1c:
    Type: AWS::EC2::Subnet
    Properties:
      CidrBlock: 10.0.0.32/27
      VpcId: !Ref HelloWorldRailsVPC
      #      AvailabilityZone: ap-northeast-1c
      AvailabilityZone: !Select [1, !Ref AvailabilityZoneType]
      Tags:
        - Key: Name
          Value: hello-world-rails-subnet-az2

  # InternetGateway定義
  HelloWorldRailsInternetGateway:
    Type: AWS::EC2::InternetGateway
    Properties:
      Tags:
        - Key: Name
          Value: hello-world-rails.yml-igw

  # VPCGatewayAttachment定義
  HelloWorldRailsGatewayAttachment:
    Type: AWS::EC2::VPCGatewayAttachment
    Properties:
      VpcId: !Ref HelloWorldRailsVPC
      InternetGatewayId: !Ref HelloWorldRailsInternetGateway

  # RouteTable定義
  HelloWorldRailsRouteTable:
    Type: AWS::EC2::RouteTable
    Properties:
      VpcId: !Ref HelloWorldRailsVPC
      Tags:
        - Key: Name
          Value: hello-world-rails-route-table

  HelloWorldRailsRouteLocal:
    Type: AWS::EC2::SubnetRouteTableAssociation
    Properties:
      RouteTableId: !Ref HelloWorldRailsRouteTable
      SubnetId: !Ref HelloWorldRailsSubnetAZ1a

  HelloWorldRailsRouteInternet:
    Type: AWS::EC2::Route
    Properties:
      RouteTableId: !Ref HelloWorldRailsRouteTable
      DestinationCidrBlock: 0.0.0.0/0
      GatewayId: !Ref HelloWorldRailsInternetGateway

  # SecurityGroup
  HelloWorldRailsEc2SecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref HelloWorldRailsVPC
      GroupDescription: This Security Group is for hello-world-rails
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 80
          ToPort: 80
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 111.239.255.229/32
        # Railsのアプリケーションサーバー用
        - IpProtocol: tcp
          FromPort: 3000
          ToPort: 3000
          CidrIp: 0.0.0.0/0
      SecurityGroupEgress:
        - IpProtocol: -1
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: hello-world-rails-ec2-securityGroup

  #  RDSのsecurity-group定義
  HelloWorldRailsRdsSecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      VpcId: !Ref HelloWorldRailsVPC
      GroupDescription: Study AWS RDS
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 3306
          ToPort: 3306
          SourceSecurityGroupId: !Ref HelloWorldRailsEc2SecurityGroup
      SecurityGroupEgress:
        - IpProtocol: -1
          CidrIp: 0.0.0.0/0
      Tags:
        - Key: Name
          Value: hello-world-rails-securityGroup-rds

  #  RDS定義
  HelloWorldRailsRds:
    Type: AWS::RDS::DBInstance
    Properties:
      DBName: HelloWorldRailsMaster
      Port: 3306
      DBInstanceClass: db.t2.micro
      AvailabilityZone: !Select [0, !Ref AvailabilityZoneType]
      VPCSecurityGroups:
        - !Ref HelloWorldRailsRdsSecurityGroup
      Engine: mysql
      EngineVersion: 8.0.13
      LicenseModel: general-public-license
      DBSubnetGroupName: !Ref HelloWorldRailsRdsSubnetGroup
      MasterUsername: root
      MasterUserPassword: password
      AllocatedStorage: 20
      AllowMajorVersionUpgrade: false
      AutoMinorVersionUpgrade: true
      StorageType: gp2
      BackupRetentionPeriod: 1
      PreferredBackupWindow: 19:00-20:00
      PreferredMaintenanceWindow: sun:10:00-sun:11:00

  #  RDSのsubnet定義
  HelloWorldRailsRdsSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    Properties:
      DBSubnetGroupDescription: This DBSubnet Group is for my studying of AWS
      SubnetIds:
        - !Ref HelloWorldRailsSubnetAZ1a
        - !Ref HelloWorldRailsSubnetAZ1c

  # ELB定義
  HelloWorldRailsELB:
    Type: AWS::ElasticLoadBalancingV2::LoadBalancer
    Properties:
      Name: hello-world-rails-elb
      SecurityGroups:
        - !Ref HelloWorldRailsEc2SecurityGroup
      Subnets:
        - !Ref HelloWorldRailsSubnetAZ1a
        - !Ref HelloWorldRailsSubnetAZ1c
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?