Skip to content

Commit 245660a

Browse files
committed
Changes for LSQL to add MySQL EC2 instance
1 parent 7e07bc5 commit 245660a

File tree

1 file changed

+99
-0
lines changed
  • design-patterns/cloudformation

1 file changed

+99
-0
lines changed

design-patterns/cloudformation/C9.yaml

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,17 @@ Parameters:
3737
Type: String
3838
Description: Location of LADV code ZIP
3939
Default: https://amazon-dynamodb-labs.com/assets/workshop.zip
40+
DBLatestAmiId:
41+
Type: 'AWS::SSM::Parameter::Value<AWS::EC2::Image::Id>'
42+
Default: '/aws/service/ami-amazon-linux-latest/amzn2-ami-hvm-x86_64-gp2'
43+
DbMasterUsername:
44+
Description: The datbase master user name
45+
Type: String
46+
Default: dbuser
47+
DbMasterPassword:
48+
Description: The database master password
49+
Type: String
50+
Default: m7de4uwt2eG#
4051

4152
Metadata:
4253
AWS::CloudFormation::Interface:
@@ -716,6 +727,89 @@ Resources:
716727
Value: Active
717728
- Key: Environment
718729
Value: !Ref EnvironmentName
730+
############ RELATIONAL MIGRATION STAGING BUCKET #########
731+
MigrationS3Bucket:
732+
Type: AWS::S3::Bucket
733+
###### RELATIONAL MIGRATION MYSQL EC2 INSTANCE ######
734+
DbSecurityGroup:
735+
Type: AWS::EC2::SecurityGroup
736+
Properties:
737+
GroupDescription: MySQL security group
738+
SecurityGroupIngress:
739+
- CidrIp: 172.31.0.0/16
740+
IpProtocol: tcp
741+
FromPort: 3306
742+
ToPort: 3306
743+
- CidrIp: 172.31.0.0/16
744+
IpProtocol: tcp
745+
FromPort: 22
746+
ToPort: 22
747+
Tags:
748+
- Key: Name
749+
Value: MySQL-SecurityGroup
750+
DBInstanceProfile:
751+
Type: AWS::IAM::InstanceProfile
752+
Properties:
753+
InstanceProfileName: DBInstanceProfile
754+
Path: /
755+
Roles:
756+
- !Ref DBInstanceRole
757+
DBInstanceRole:
758+
Type: AWS::IAM::Role
759+
Properties:
760+
RoleName: DBInstanceRole
761+
AssumeRolePolicyDocument:
762+
Version: 2012-10-17
763+
Statement:
764+
-
765+
Effect: Allow
766+
Principal:
767+
Service:
768+
- ec2.amazonaws.com
769+
Action:
770+
- sts:AssumeRole
771+
Path: /
772+
ManagedPolicyArns:
773+
- arn:aws:iam::aws:policy/AmazonS3FullAccess
774+
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
775+
DbInstance:
776+
Type: AWS::EC2::Instance
777+
Properties:
778+
ImageId: !Ref DBLatestAmiId
779+
InstanceType: !GetAtt Cloud9FindTheInstanceTypeLambda.InstanceType
780+
SecurityGroupIds:
781+
- !GetAtt DbSecurityGroup.GroupId
782+
SubnetId: !GetAtt Cloud9FindTheInstanceTypeLambda.SubnetId
783+
IamInstanceProfile: !Ref DBInstanceProfile
784+
BlockDeviceMappings:
785+
- DeviceName: /dev/xvda
786+
Ebs:
787+
VolumeType: gp2
788+
VolumeSize: 200
789+
DeleteOnTermination: True
790+
Encrypted: True
791+
UserData:
792+
Fn::Base64: !Sub |
793+
#!/bin/bash -ex
794+
sudo su
795+
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
796+
rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
797+
yum install -y mysql-community-server
798+
systemctl enable mysqld
799+
systemctl start mysqld
800+
export DbMasterPassword=${DbMasterPassword}
801+
export DbMasterUsername=dbuser
802+
mysql -u root "-p$(grep -oP '(?<=root@localhost\: )\S+' /var/log/mysqld.log)" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${DbMasterPassword}'" --connect-expired-password
803+
mysql -u root "-p${DbMasterPassword}" -e "CREATE USER '${DbMasterUsername}' IDENTIFIED BY '${DbMasterPassword}'"
804+
mysql -u root "-p${DbMasterPassword}" -e "GRANT ALL PRIVILEGES ON *.* TO '${DbMasterUsername}'"
805+
mysql -u root "-p${DbMasterPassword}" -e "FLUSH PRIVILEGES"
806+
mysql -u root "-p${DbMasterPassword}" -e "CREATE DATABASE app_db;"
807+
Tags:
808+
- Key: Name
809+
Value: MySQL-Instance
810+
S3Bucket:
811+
Type: AWS::S3::Bucket
812+
719813

720814
################## OUTPUTS #####################
721815
Outputs:
@@ -732,6 +826,11 @@ Outputs:
732826
Value: !Ref Cloud9LogBucket
733827
Export:
734828
Name: Cloud9LogBucket
829+
MigrationS3BucketName:
830+
Description: S3 Bucket Name
831+
Value: !Ref MigrationS3Bucket
832+
Export:
833+
Name: MigrationS3Bucket
735834
Cloud9RoleArn:
736835
Description: Role Arn
737836
Value: !GetAtt Cloud9Role.Arn

0 commit comments

Comments
 (0)