Skip to content

Commit 03f2c12

Browse files
authored
Setup env for LSQL (#122)
* Setup env for LSQL * Adding C9 template too
1 parent e2c2ff5 commit 03f2c12

File tree

2 files changed

+103
-3
lines changed

2 files changed

+103
-3
lines changed

design-patterns/cloudformation/C9.yaml

Lines changed: 96 additions & 1 deletion
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:
@@ -658,7 +669,7 @@ Resources:
658669
" if [[ \"$RESULT\" -ne 0 ]]; then",
659670
" sleep_delay",
660671
" else",
661-
" /bin/bash /tmp/dynamodbworkshop.sh ${SUB_VERSION} ${AWS::AccountId} ${AWS::Region} \"${WorkshopZIP}\" \"${SUB_REPL_ROLE}\" &&",
672+
" /bin/bash /tmp/dynamodbworkshop.sh ${SUB_VERSION} ${AWS::AccountId} ${AWS::Region} \"${WorkshopZIP}\" \"${SUB_REPL_ROLE}\" \"${SUB_DB_USER}\" \"${SUB_DB_PASSWORD}\" &&",
662673
" exit 0",
663674
" fi",
664675
"done"
@@ -672,6 +683,8 @@ Resources:
672683
SUB_USERDATA_URL: !FindInMap [DesignPatterns, options, UserDataURL],
673684
SUB_VERSION: !FindInMap [DesignPatterns, options, version],
674685
SUB_REPL_ROLE: !GetAtt ['DDBReplicationRole', 'Arn'],
686+
SUB_DB_USER: !Ref 'DbMasterUsername',
687+
SUB_DB_PASSWORD: !Ref 'DbMasterPassword',
675688
}
676689
Cloud9BootstrapAssociation:
677690
Type: AWS::SSM::Association
@@ -716,6 +729,83 @@ Resources:
716729
Value: Active
717730
- Key: Environment
718731
Value: !Ref EnvironmentName
732+
############ RELATIONAL MIGRATION STAGING BUCKET #########
733+
MigrationS3Bucket:
734+
Type: AWS::S3::Bucket
735+
###### RELATIONAL MIGRATION MYSQL EC2 PUBLIC INSTANCE ######
736+
DbSecurityGroup:
737+
Type: AWS::EC2::SecurityGroup
738+
Properties:
739+
GroupDescription: MySQL security group
740+
SecurityGroupIngress:
741+
- CidrIp: 172.31.0.0/16
742+
IpProtocol: tcp
743+
FromPort: 3306
744+
ToPort: 3306
745+
Tags:
746+
- Key: Name
747+
Value: MySQL-SecurityGroup
748+
DBInstanceProfile:
749+
Type: AWS::IAM::InstanceProfile
750+
Properties:
751+
InstanceProfileName: DBInstanceProfile
752+
Path: /
753+
Roles:
754+
- !Ref DBInstanceRole
755+
DBInstanceRole:
756+
Type: AWS::IAM::Role
757+
Properties:
758+
RoleName: DBInstanceRole
759+
AssumeRolePolicyDocument:
760+
Version: 2012-10-17
761+
Statement:
762+
-
763+
Effect: Allow
764+
Principal:
765+
Service:
766+
- ec2.amazonaws.com
767+
Action:
768+
- sts:AssumeRole
769+
Path: /
770+
ManagedPolicyArns:
771+
- arn:aws:iam::aws:policy/AmazonS3FullAccess
772+
- arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore
773+
DbInstance:
774+
Type: AWS::EC2::Instance
775+
Properties:
776+
ImageId: !Ref DBLatestAmiId
777+
InstanceType: !GetAtt Cloud9FindTheInstanceTypeLambda.InstanceType
778+
SecurityGroupIds:
779+
- !GetAtt DbSecurityGroup.GroupId
780+
SubnetId: !GetAtt Cloud9FindTheInstanceTypeLambda.SubnetId
781+
IamInstanceProfile: !Ref DBInstanceProfile
782+
BlockDeviceMappings:
783+
- DeviceName: /dev/xvda
784+
Ebs:
785+
VolumeType: gp2
786+
VolumeSize: 50
787+
DeleteOnTermination: True
788+
Encrypted: True
789+
UserData:
790+
Fn::Base64: !Sub |
791+
#!/bin/bash -ex
792+
sudo su
793+
rpm --import https://repo.mysql.com/RPM-GPG-KEY-mysql-2023
794+
rpm -Uvh https://repo.mysql.com/mysql80-community-release-el7-3.noarch.rpm
795+
yum install -y mysql-community-server
796+
systemctl enable mysqld
797+
systemctl start mysqld
798+
export DbMasterPassword=${DbMasterPassword}
799+
export DbMasterUsername=${DbMasterUsername}
800+
mysql -u root "-p$(grep -oP '(?<=root@localhost\: )\S+' /var/log/mysqld.log)" -e "ALTER USER 'root'@'localhost' IDENTIFIED BY '${DbMasterPassword}'" --connect-expired-password
801+
mysql -u root "-p${DbMasterPassword}" -e "CREATE USER '${DbMasterUsername}' IDENTIFIED BY '${DbMasterPassword}'"
802+
mysql -u root "-p${DbMasterPassword}" -e "GRANT ALL PRIVILEGES ON *.* TO '${DbMasterUsername}'"
803+
mysql -u root "-p${DbMasterPassword}" -e "FLUSH PRIVILEGES"
804+
mysql -u root "-p${DbMasterPassword}" -e "CREATE DATABASE app_db;"
805+
Tags:
806+
- Key: Name
807+
Value: MySQL-Instance
808+
719809

720810
################## OUTPUTS #####################
721811
Outputs:
@@ -732,6 +822,11 @@ Outputs:
732822
Value: !Ref Cloud9LogBucket
733823
Export:
734824
Name: Cloud9LogBucket
825+
MigrationS3BucketName:
826+
Description: S3 Bucket Name
827+
Value: !Ref MigrationS3Bucket
828+
Export:
829+
Name: MigrationS3Bucket
735830
Cloud9RoleArn:
736831
Description: Role Arn
737832
Value: !GetAtt Cloud9Role.Arn

design-patterns/cloudformation/UserDataC9.sh

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ AWS_ACCOUNT_ID=$2
66
AWS_REGION=$3
77
WorkshopZIP="$4" # ${WorkshopZIP}"
88
DDB_REPLICATION_ROLE="$5" #!Sub echo ${DDBReplicationRole.Arn}
9+
DB_USER="$6"
10+
DB_PASSWORD="$7"
911

1012
function log
1113
{
@@ -52,12 +54,15 @@ output = json
5254
EOF
5355
#chmod 600 /home/ubuntu/.aws/config
5456
cat >> /home/ubuntu/.bashrc <<EOF
55-
PATH=$PATH:/usr/local/bin'
56-
export PATH' >> /home/ubuntu/.bashrc
57+
PATH=$PATH:/usr/local/bin
58+
export PATH >> /home/ubuntu/.bashrc
5759
EOF
5860
cat >> /home/ubuntu/.bash_profile <<EOF
5961
export AWS_ACCOUNT_ID="${AWS_ACCOUNT_ID}"
6062
export AWS_REGION="${AWS_REGION}"
63+
export AWS_DEFAULT_REGION="${AWS_REGION}"
64+
export MYSQL_PASSWORD="${DB_PASSWORD}"
65+
export MYSQL_USERNAME="${DB_USER}"
6166
aws cloud9 update-environment --environment-id \$C9_PID --managed-credentials-action DISABLE --region $AWS_REGION &> /dev/null
6267
rm -vf ${HOME}/.aws/credentials &> /dev/null
6368
EOF

0 commit comments

Comments
 (0)