Skip to content

Instantly share code, notes, and snippets.

@chandra-prakash-meghwal
Created June 4, 2024 13:18
Show Gist options
  • Save chandra-prakash-meghwal/ec2474ecd64f2f7704246952298b0933 to your computer and use it in GitHub Desktop.
Save chandra-prakash-meghwal/ec2474ecd64f2f7704246952298b0933 to your computer and use it in GitHub Desktop.
AWS Cloudformation template to create RDS MySQL Database in specific VPC
AWSTemplateFormatVersion: '2010-09-09'
Description: Creates RDS MySQL Database in specific VPC
Parameters:
ResourceNamePrefix:
Type: String
Default: testing # You can change the default prefix as needed
RDSDBMasterUsername:
Type: String
Description: RDS db master username
RDSDBMasterUserPassword:
Type: String
Description: RDS db master user password
NoEcho: true
RDSDBName:
Type: String
Description: RDS db name
ResourceVPC:
Type: String
Description: existing vpc id
SubnetId1:
Type: String
Description: Subnet ID 1 (us-east-2a) of existing vpc
SubnetId2:
Type: String
Description: Subnet ID 2 (us-east-2b) of existing vpc
SubnetId3:
Type: String
Description: Subnet ID 3 (us-east-2c) of existing vpc
SecurityGroup:
Type: String
Description: existing Security Group ID
Resources:
# Security Group
DBSecurityGroup:
Type: 'AWS::EC2::SecurityGroup'
Properties:
VpcId: !Ref VPC
GroupDescription: 'DB Security Group'
SecurityGroupIngress:
- IpProtocol: tcp
FromPort: '3306'
ToPort: '3306'
CidrIp: '0.0.0.0/0'
SecurityGroupEgress:
- IpProtocol: -1
CidrIp: '0.0.0.0/0'
Tags:
- Key: 'MyService'
Value: 'true'
# RDS MySQL Database
DBInstance:
Type: 'AWS::RDS::DBInstance'
Properties:
Engine: 'mysql'
DBInstanceClass: 'db.t2.medium'
MasterUsername: !Ref RDSDBMasterUsername
MasterUserPassword: !Ref RDSDBMasterUserPassword
AllocatedStorage: '20'
VPCSecurityGroups:
- !Ref DBSecurityGroup
DBSubnetGroupName: !Ref DBSubnetGroup
Tags:
- Key: 'MyService'
Value: 'true'
# DB Subnet Group
DBSubnetGroup:
Type: 'AWS::RDS::DBSubnetGroup'
Properties:
DBSubnetGroupDescription: 'DB Subnet Group'
SubnetIds:
- !Ref SubnetId1
- !Ref SubnetId3
Tags:
- Key: 'MyService'
Value: 'true'
# Secrets Manager Resource to Store RDS Endpoint
RDSecret:
Type: 'AWS::SecretsManager::Secret'
Properties:
Name: !Sub ${ResourceNamePrefix}-rds-db-secrets
Description: 'This secret contains the RDS endpoint'
SecretString: !Sub |
{
"RDS_HOST": "${DBInstance.Endpoint.Address}",
"RDS_USER": "${RDSDBMasterUsername}",
"RDS_PASSWORD": "${RDSDBMasterUserPassword}",
"RDS_DBNAME": "${ResourceNamePrefix}_${RDSDBName}"
}
Tags:
- Key: 'MyService'
Value: 'true'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment