RDS 설치 -> host 서버 접속 -> httpd + wordpress 설치
패키지 버전 확인: amazon-linux-extras list | grep php
특정 버전 설치: amazon-linux-extras enable php8.0
rds 만들고 엔드포인트 받기(mydb.chtn61fibgt9.us-west-1.rds.amazonaws.com)
(표준생성 - mysql(엔진버전 8.0.28 제일 낮은) - 프리티어 - 식별자(mydb) - 자격증명(root) - 마스터암호(It12345!) - 버스터블클래스(t2마이크로) - vpc 선택
워드프레스 홈페이지 다운로드(5.7.8버전 tar.gz)
홈페이지 - all releases - 5.7.8 tar.gz 링크 복사
#! /bin/bash
wget https://ko.wordpress.org/wordpress-5.7.8-ko_KR.tar.gz
tar xvfz wordpress-5.7.8-ko_KR.tar.gz
yum install -y httpd
cp -r wordpress/* /var/www/html/
amazon-linux-extras enable php8.0
yum install -y php php-fpm php-pdo php-mysqlnd
cp /var/www/html/wp-config-sample.php /var/www/html/wp-config.php
sed -i 's/DirectoryIndex index.html/DirectoryIndex index.php/g' /etc/httpd/conf/httpd.conf
sed -i 's/database_name_here/root/g' /var/www/html/wp-config.php
sed -i 's/user_here/root/g' /var/www/html/wp-config.php
sed -i 's/password_here/root/g' /var/www/html/wp-config.php
sed -i 's/localhost/{ endpoint 주소 }/g' /var/www/html/wp-config.php
systemctl enable --now httpd
코드 작성

명령어들은 코드에 하드 코딩하지 않는것이 좋으므로 따로 파일에 저장하여 사용.
sh 파일 생성


코드 수정

엔드포인트가 없는 경우, 실행하면 DB 연결 오류 화면이 출력됨 -> DB 서버 생성 후 엔드포인트를 추가해 주면 wordpress 화면이 정상적으로 출력됨.

공식 문서: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb

tmi:
AWS는 frontend와 backend를 리스너와 타겟그룹으로 부른다. 접속 구조는 리스너(Front-end) -> LB -> target group(Back-end)
ALB 생성시 target group은 listener 보다 먼저 생성되어야 한다.
health check는 html로만 가능하기 때문에 health check용 html 생성이 필요할 수 있음.
tf code 작성

생성됨

공식 문서: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_target_group

tf code

health check를 위해 sh파일에 health.html을 생성하는 코드 추가.

apply

user data가 추가 되면 기존 인스턴스는 종료되고 새로운 인스턴스가 생성됨

코드 수정후 health.html 내용이 정상 출력됨.

aws console에서 확인

공식 문서: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/lb_listener

tf code

apply

auto scale 및 attachment 필요.
auto scaling 그룹 -> 시작 템플릿 필요
공식 문서: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/ami_from_instance

depens_on 명령어 사용
코드

aws console 확인

Launch configuration
평문으로 입력하면 안됨 -> filebase64 사용해서 암호화 필요
공식 문서(Launch template): https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/launch_template

tf code

공식 문서: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_group

tf code 작성

concat을 이용해서 한번에 여러개의 resource를 처리하는것도 가능함.
Auto Scaling group 생성 확인

공식 문서: https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/autoscaling_attachment

tf code 작성

공식 문서(DB instance): https://registry.terraform.io/providers/hashicorp/aws/latest/docs/resources/db_instance

tf code

RDS 생성됨
