Skip to main content

Rstudio 실습환경 자동화 구성

 

 

Amazon EC2의 User data 기능을 사용하기 위해 User Data내용을 파일로 만들어둡니다.

여기 예에서는 install_rstudio.txt라고 파일을 만들었습니다.

#!/bin/bash

# update indices
apt update -qq
# install two helper packages we need
apt install -y --no-install-recommends software-properties-common dirmngr
# add the signing key (by Michael Rutter) for these repos
# To verify key, run gpg --show-keys /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# Fingerprint: E298A3A825C0D65DFD57CBB651716619E084DAB9
wget -qO- https://cloud.r-project.org/bin/linux/ubuntu/marutter_pubkey.asc | tee -a /etc/apt/trusted.gpg.d/cran_ubuntu_key.asc
# add the repo from CRAN -- lsb_release adjusts to 'noble' or 'jammy' or ... as needed
add-apt-repository -y "deb https://cloud.r-project.org/bin/linux/ubuntu $(lsb_release -cs)-cran40/"

# Install R
apt install -y --no-install-recommends r-base

# Install RStudio dependencies
apt install -y gdebi-core

# Install R pkacage dependencies
apt install -y build-essential

wget https://download2.rstudio.org/server/jammy/amd64/rstudio-server-2024.12.1-563-amd64.deb

gdebi -n rstudio-server-2024.12.1-563-amd64.deb

# Add rstudio user and set password
useradd -m -s /bin/bash rstudio
echo "rstudio:rstudio" | chpasswd

R -e "options(HTTPUserAgent = sprintf('R/%s R (%s)', getRversion(), paste(getRversion(), R.version['platform'], R.version['arch'], R.version['os']))); install.packages('TwoSampleMR', repos = c('https://mrcieu.r-universe.dev/bin/linux/noble/4.4/', 'https://p3m.dev/cran/__linux__/noble/latest', 'https://cloud.r-project.org'))"

이제 다음과 같은 명령어로 EC2를 실행할 수 있습니다. 이 부분이 어렵다면 Amazon EC2 콘솔에서 먼저 진행 후 Command line을 완성하는 것을 추천드립니다.

export AMI_ID={Ubuntu 나 필요한 OS가 설치된 Base AMI}
export INSTANCE_TYPE=t3.large
export KEY_NAME={본인의 KeyName}

# 이외에도 security group (sg-로 시작하는 것들)과 SnapshotId는 본인의 맞게 설정합니다.

aws ec2 run-instances --image-id "${AMI_ID}" --instance-type "${INSTANCE_TYPE}" --key-name "${KEY_NAME}" \
    --user-data file://install_rstudio.txt \
    --block-device-mappings '{"DeviceName":"/dev/sda1","Ebs":{"Encrypted":false,"DeleteOnTermination":true,"Iops":3000,"SnapshotId":"snap-0dbe62bb8f1f21357","VolumeSize":100,"VolumeType":"gp3","Throughput":125}}' \
    --network-interfaces '{"AssociatePublicIpAddress":true,"DeviceIndex":0,"Groups":["sg-0d2f7724e68ddff15","sg-0e2c103f2a28a9be7"]}' \
    --credit-specification '{"CpuCredits":"unlimited"}' --tag-specifications '{"ResourceType":"instance","Tags":[{"Key":"Name","Value":"rstudio server"}]}' \
    --metadata-options '{"HttpEndpoint":"enabled","HttpPutResponseHopLimit":2,"HttpTokens":"required"}' \
    --private-dns-name-options '{"HostnameType":"ip-name","EnableResourceNameDnsARecord":true,"EnableResourceNameDnsAAAARecord":false}' \
    --count "1" --region ap-northeast-2

EC2로 접속해봅니다. 인스턴스 구성이 예상한대로 잘되었다면 아래와 같이 AMI로 만들수 있습니다.

aws ec2 create-image \
    --instance-id {본인의 대상 인스턴스 아이디} \
    --name "My Rstudio server" \
    --description "An AMI for my Rstudio server with TwoSampleMR R package" \
    --region ap-northeast-2

 

이제 만들어진 AMI로 원하는 수와 설정내용으로 새로운 인스턴스를 실행해보세요!