|
| 1 | +AWSTemplateFormatVersion: '2010-09-09' |
| 2 | +Description: Personal PaaS VM (Git-push-to-deploy) |
| 3 | + |
| 4 | +Parameters: |
| 5 | + KeyName: |
| 6 | + Type: AWS::EC2::KeyPair::KeyName |
| 7 | + Description: EC2 KeyPair for SSH access |
| 8 | + |
| 9 | +Resources: |
| 10 | + PaaSEC2Instance: |
| 11 | + Type: AWS::EC2::Instance |
| 12 | + Properties: |
| 13 | + InstanceType: t2.micro |
| 14 | + KeyName: "" # Requirement 4: Inject the KeyName provided as a parameter |
| 15 | + ImageId: "" # Requirement 5: Use Ubuntu 24.04 image for us-west-2 |
| 16 | + |
| 17 | + SecurityGroupIds: |
| 18 | + - !Ref InstanceSecurityGroup |
| 19 | + |
| 20 | + UserData: |
| 21 | + Fn::Base64: !Sub | |
| 22 | + #!/bin/bash |
| 23 | + set -e |
| 24 | + |
| 25 | + apt-get update -y |
| 26 | + apt-get install -y docker.io git |
| 27 | + |
| 28 | + systemctl start docker |
| 29 | + systemctl enable docker |
| 30 | + |
| 31 | + # Directories for app + logs |
| 32 | + mkdir -p /home/ubuntu/app |
| 33 | + mkdir -p /home/ubuntu/logs |
| 34 | + chown -R ubuntu:ubuntu /home/ubuntu/app /home/ubuntu/logs |
| 35 | + |
| 36 | + # Bare repo |
| 37 | + sudo -u ubuntu git init --bare /home/ubuntu/app.git |
| 38 | + sudo -u ubuntu git --git-dir=/home/ubuntu/app.git symbolic-ref HEAD refs/heads/main |
| 39 | + |
| 40 | + # Create the deploy script |
| 41 | + cat << 'EOF' > /usr/local/bin/deploy.sh |
| 42 | + #!/bin/bash |
| 43 | + set -e |
| 44 | + |
| 45 | + APP_DIR=/home/ubuntu/app |
| 46 | + REPO_DIR=/home/ubuntu/app.git |
| 47 | + LOG_FILE=/var/log/paas-deploy.log # Deployment logs |
| 48 | + APP_LOG_FILE=/home/ubuntu/logs/app.log # App logs |
| 49 | + |
| 50 | + exec >> $LOG_FILE 2>&1 # Replace the current shell and capture all the log output lines |
| 51 | + |
| 52 | + echo "==== Deploy started at $(date) ====" |
| 53 | + |
| 54 | + rm -rf $APP_DIR |
| 55 | + mkdir -p $APP_DIR |
| 56 | + |
| 57 | + git --work-tree=$APP_DIR --git-dir=$REPO_DIR checkout -f |
| 58 | + cd $APP_DIR |
| 59 | + |
| 60 | + # Requirement 6: Build the docker container. Tag it myapp |
| 61 | + |
| 62 | + docker stop myapp-container || true |
| 63 | + docker rm myapp-container || true |
| 64 | + |
| 65 | + pkill -f "docker logs -f myapp-container" || true |
| 66 | + |
| 67 | + # Requirement 7: Run the Docker container. Map the application port to port 80. Name the container myapp-container |
| 68 | + |
| 69 | + mkdir -p /home/ubuntu/logs |
| 70 | + touch $APP_LOG_FILE |
| 71 | + chown -R ubuntu:ubuntu /home/ubuntu/logs |
| 72 | + |
| 73 | + docker logs -f myapp-container >> $APP_LOG_FILE 2>&1 & |
| 74 | + |
| 75 | + echo "==== Deploy finished ====" |
| 76 | + EOF |
| 77 | + |
| 78 | + chmod +x /usr/local/bin/deploy.sh |
| 79 | + echo "ubuntu ALL=(ALL) NOPASSWD: /usr/local/bin/deploy.sh" > /etc/sudoers.d/deploy |
| 80 | + chmod 440 /etc/sudoers.d/deploy |
| 81 | + |
| 82 | + # Git post-receive hook |
| 83 | + cat << 'EOF' > /home/ubuntu/app.git/hooks/post-receive |
| 84 | + #!/bin/bash |
| 85 | + sudo /usr/local/bin/deploy.sh |
| 86 | + EOF |
| 87 | + |
| 88 | + chmod +x /home/ubuntu/app.git/hooks/post-receive |
| 89 | + chown -R ubuntu:ubuntu /home/ubuntu/app.git |
| 90 | + |
| 91 | + InstanceSecurityGroup: |
| 92 | + Type: AWS::EC2::SecurityGroup |
| 93 | + Properties: |
| 94 | + GroupDescription: Allow SSH and HTTP |
| 95 | + SecurityGroupIngress: |
| 96 | + - IpProtocol: tcp |
| 97 | + FromPort: 22 |
| 98 | + ToPort: 22 |
| 99 | + CidrIp: 0.0.0.0/0 |
| 100 | + |
| 101 | + # Requirement 8: Add the security group for Port 80 to allow traffic from anywhere. |
| 102 | + |
| 103 | +Outputs: |
| 104 | + PublicIP: |
| 105 | + Value: !GetAtt PaaSEC2Instance.PublicIp |
0 commit comments