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?

AWSでCloudformationでRDBとEC2構築

Posted at

RDSではまったのでここに備忘録としてで書いておく

AllocatedStorage: 20 を追記すること!

AWSTemplateFormatVersion: '2010-09-09'
Description: Create an EC2 instance and RDS database

Parameters:
  InstanceType:
    Type: String
    Description: EC2 instance type
    Default: t2.micro
  DBInstanceClass:
    Type: String
    Description: RDS database instance class
    Default: db.t3.micro
  KeyName:
    Type: String
    Description: EC2 Key Pair name for SSH access
    Default: my-key-pair # Replace with your actual key pair name

Resources:
  MyEC2Instance:
    Type: AWS::EC2::Instance
    Properties:
      ImageId: ami-08ce76bae392de7dc  # Update based on your region
      InstanceType: !Ref InstanceType
      KeyName: !Ref KeyName
      SecurityGroupIds:
        - !Ref MySecurityGroup

  MySecurityGroup:
    Type: AWS::EC2::SecurityGroup
    Properties:
      GroupDescription: Allow SSH and DB access
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: 22
          ToPort: 22
          CidrIp: 0.0.0.0/0  # Restrict to specific IP range for security
        - IpProtocol: tcp
          FromPort: 3306
          ToPort: 3306
          CidrIp: 0.0.0.0/0  # Restrict to specific IP range for security
      VpcId: vpc-026a7387dd6843290  # Replace with your actual VPC ID

  MyRDSInstance:
    Type: AWS::RDS::DBInstance
    Properties:
      DBInstanceClass: !Ref DBInstanceClass
      Engine: mysql
      MasterUsername: myuser
      MasterUserPassword: b2h2an0cbn  # Use AWS Secrets Manager or Parameters for sensitive values
      DBName: mydatabase
      AllocatedStorage: 20 
      VPCSecurityGroups:
        - !GetAtt MySecurityGroup.GroupId
      DBSubnetGroupName: !Ref MyDBSubnetGroup

  MyDBSubnetGroup:
    Type: AWS::RDS::DBSubnetGroup
    Properties:
      DBSubnetGroupDescription: Subnet group for RDS
      SubnetIds:
        - subnet-0c0b17fe43556d272 # Replace with actual Subnet IDs
        - subnet-0e490e7b684f3b4aa # Replace with actual Subnet IDs

これでEC2とRDSの疎通ができます。

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?