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の疎通ができます。