AWS EC2 Script 자동화
EC2 Instance를 구성하면서 매번 처음부터 세팅하고 번거로움을 줄이기 위하여 사용
EC2 User Data
- EC2 Instance
처음 부팅
시 스크립트 자동으로 실행, 설정하는 기능 ( 재부팅 시 적용 되지 않는다. 만약 재부팅할 때 마다 적용하고 싶다면 아래에 별도로 소개 ) #!/bin/bash
옵션을 주어야만 이를 인식하고 실행- 방화벽 해제 시 강제 옵션을 주지 않으면 실행 불가
자동 실행 목록
- Ubuntu 방화벽 해제
- ssh(22), 80, 8080(jenkins)
- Docker 설치 및 Jenkins+Python 환경 구성
- Dockerfile image build 및 Docker-compose 실행 ( restart 옵션 추가 )
- Codedeploy 설치
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/bash
sudo ufw --force enable
sudo ufw allow ssh
sudo ufw allow 80
sudo ufw allow 8080
sudo apt-get update
sudo apt-get install \
ca-certificates \
curl \
gnupg \
lsb-release
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo gpg --dearmor -o /etc/apt/keyrings/docker.gpg
echo \
"deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] https://download.docker.com/linux/ubuntu \
$(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
sudo apt-get update
sudo apt-get -y install docker-ce docker-ce-cli containerd.io docker-compose-plugin
sudo chmod 666 /var/run/docker.sock
sudo apt install -y docker-compose
cd /home/ubuntu
mkdir jenkins
sudo chmod 755 jenkins/
cd jenkins
echo 'FROM jenkins/jenkins:lts
USER root
RUN echo "Acquire::Check-Valid-Until \"false\";\nAcquire::Check-Date \"false\";" | cat > /etc/apt/apt.conf.d/10no--check-valid-until
RUN apt-get update && apt-get install -y vim && apt-get install -y python3.9 python3-pip
RUN echo "alias ll='ls -al'" >> ~/.bashrc
RUN pip3 install requests pandas openpyxl numpy sqlalchemy flask pymysql redshift-connector scikit-learn scipy seaborn matplotlib gspread bs4 oauth2client gspread-dataframe sshtunnel' >> Dockerfile
sudo docker build -t "jenkins:python" ./
sudo echo 'version: "3"
services:
jenkins:
image: jenkins:python
container_name: jenkins
user: root
volumes:
- /home/ubuntu/jenkins:/var/jenkins_home
ports:
- 8080:8080
restart: always' | sudo tee -a ./docker-compose.yml
sudo docker-compose up -d
sudo apt update
sudo apt install -y ruby-full
sudo apt install wget
cd ~
wget https://aws-codedeploy-ap-northeast-2.s3.ap-northeast-2.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto
MIME
- EC2 Instance가 최초 실행뿐만 아니라 재부팅 시에도 Cloud init을 옵션을 통해 계속하여 실행
- User Init에 동일하게 추가하여 주면 된다.
- 재부팅 시 docker command가 막혀 옵션 추가
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
Content-Type: multipart/mixed; boundary="//"
MIME-Version: 1.0
--//
Content-Type: text/cloud-config; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="cloud-config.txt"
#cloud-config
cloud_final_modules:
- [scripts-user, always]
--//
Content-Type:
text/x-shellscript; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit
Content-Disposition: attachment; filename="userdata.txt"
#!/bin/bash
sudo chmod 666 /var/run/docker.sock
--//
참조
- https://blog.voidmainvoid.net/391