前幾日,已經把完整的架構,以及如何撰寫YAML,且使用CloudFormation客製化服務,如下架構圖:
EC2可以透過撰寫Userdata,預先做套件安裝。建立Web的檔案,也可以透過Userdata,在EC2啟動的時候,就事先把檔案存在EC2。參照[Day9]的文章,撰寫的YAML去修改,如下:
看到UserData的區塊,這邊demo是建立web,所以就會安裝跟web相關的套件,以及會使用到的web檔,可以把以下Scripts貼到put your script here的區塊
yum -y update
yum -y install httpd php mysql php-mysql
chkconfig httpd on
service httpd start
cd /var/www/html
wget https://s3-us-west-2.amazonaws.com/us-west-2-aws-training/awsu-spl/spl-13/scripts/app.tgz
tar xvfz app.tgz
chown apache:root /var/www/html/rds.conf.php
這裡的web檔,選擇下載aws的範例檔。EC2的配置完整如下:
myEC2Instance:
Type: AWS::EC2::Instance
Properties:
BlockDeviceMappings:
- DeviceName: "/dev/xvda"
Ebs:
VolumeSize: 10
VolumeType: gp3
KeyName: "itdemo"
SubnetId: !Ref subnetName
DisableApiTermination: false
ImageId: ami-0443305dabd4be2bc
InstanceType: "t3.micro"
SecurityGroupIds:
- !Ref secGroupName
UserData: !Base64 |
#!/bin/bash -ex
yum -y update
yum -y install httpd php mysql php-mysql
chkconfig httpd on
service httpd start
cd /var/www/html
wget https://s3-us-west-2.amazonaws.com/us-west-2-aws-training/awsu-spl/spl-13/scripts/app.tgz
tar xvfz app.tgz
chown apache:root /var/www/html/rds.conf.php
Tags:
- Key: itdemo-ec2-cf
Value: v1
確定檔案都撰寫完成後,就可以透過CloudFormation來建立架構的所有服務,CloudFormation的建置步驟參考[Day11]文章。
CloudFormation都建立完成後,可以去查看建立好的EC2,複製public ipv4 dns到browser直接瀏覽,若瀏覽失敗,可以先確認SG的allow rule,或連進EC2確認檔案有在/var/www/html。成功瀏覽如下圖:
CloudFormation之旅就先在這裡結束,明日會開啟CDK之旅