プログラミングBlog

AWS CloudFormationメモ①

CloudFormationメモ①

RDS

公式のリファレンスから引用。
AWS::RDS::DBSubnetGroup - AWS CloudFormation

AWSTemplateFormatVersion: "2010-09-09"
Resources: 
  myDBSubnetGroup: 
    Properties: 
      DBSubnetGroupDescription: description
      SubnetIds: 
        - subnet-7b5b4112
        - subnet-7b5b4115
      Tags: 
        - 
          Key: String
          Value: String
    Type: "AWS::RDS::DBSubnetGroup"

SubnetIds:
RDSにアタッチするSubentは異なるリージョン間のSubnetでないとエラーとなるため注意。

AvailabilityZone

AvailabilityZoneを設定する項目にap-north-east1(東京)このようなハードコーディングしていましたが、
マネージメントコンソール右上のAZと連動しているため、右上のAvailabilityZoneを東京以外に選択をしていた場合はAZが見つからないためエラーとなります。
なのでパラメーターとして選べるように。

Parameters:
 AvailabilityZonePublicSubnet:
 Type: AWS::EC2::AvailabilityZone::Name

Resources:
  PublicSubnet:
    Type: AWS::EC2::Subnet
    Properties:
      VpcId:
        Ref: VPC
      CidrBlock: 10.0.0.0/24
      AvailabilityZone: !Ref AvailabilityZonePublicSubnet
      Tags:
        - Key: Name
          Value: PublicSubnet

NetworkACL

  PublicNetworkACLInboundRule01:
    Type: AWS::EC2::NetworkAclEntry
    Properties:
      NetworkAclId:
        Ref: PublicNetworkACL
      Egress: false
      RuleNumber: 100
      Protocol: -1
      RuleAction: allow
      CidrBlock: 0.0.0.0/0

  PublicNetworkACLOutboundRule:
    Type: AWS::EC2::NetworkAclEntry
    Properties:
      NetworkAclId:
        Ref: PublicNetworkACL
      Egress: true
      RuleNumber: 100
      Protocol: -1
      RuleAction: allow
      CidrBlock: 0.0.0.0/0

Egressfalse:インバウンド true:アウトバウンド
Protcol TCP:6 すべて:-1
全許可のルールを追加するために-1を設定。
NetworkACLはデフォルトで全拒否のルールがインバウンドとアウトバウンドに設定されています。

EndPoint

yum updateができなかったときに作成したエンドポイント。
Amazon Linux2のRepositoryにアクセスできるようポリシーを設定しています。

  # Create Endpoint
  S3Endpoint:
    Type: "AWS::EC2::VPCEndpoint"
    Properties:
      PolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Sid: "AmazonLinux2AMIRepositoryAccess"
            Principal: "*"
            Action:
              - "s3:GetObject"
            Resource:
              - "arn:aws:s3:::amazonlinux.*.amazonaws.com/*"
      RouteTableIds:
        - !Ref PublicRouteTable
      ServiceName: !Sub "com.amazonaws.${AWS::Region}.s3"
      VpcId: !Ref VPC

EC2

新規作成の場合、AMIが存在しないため、パラメーターで新しいAMIを発行するように。
KeyPairの設定。password、アクセスキー、などは必ずパラメーターとして設定できるようにする。

Parameters:
  KeyName:
    Description: Name of an existing EC2 KeyPair to enable SSH access to the instances
    Type: "AWS::EC2::KeyPair::KeyName"
  AMIID:
    Type: AWS::SSM::Parameter::Value<String>
    Default: /aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2
  
Resources:
  PublicEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      Tags:
        - Key: Name
          Value: PublicEC2Instance
      ImageId: !Ref AMIID
      KeyName: !Ref KeyName
      NetworkInterfaces:
        - AssociatePublicIpAddress: "true"
          DeviceIndex: "0"
          GroupSet:
            - !Ref PublicSecurityGroup
          SubnetId:
            Ref: PublicSubnet

- AssociatePublicIpAddress: "true" publicIPAddressを設定しています。
これを設定しないと接続できなくなります。