0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

開発用EC2インスタンスのユーザーデータ

Posted at

ubuntu24.04を使うときのユーザデータを作りました。
開発に必要なものは一通りいれてあります。

#!/bin/bash
set -euxo pipefail
export DEBIAN_FRONTEND=noninteractive

# ---- base packages ----
apt-get update
apt-get install -y \
  ca-certificates curl gnupg unzip \
  bash-completion git postgresql-client tree \
  python3-venv

# ---- Docker (official repo) ----
install -m 0755 -d /etc/apt/keyrings
if [ ! -f /etc/apt/keyrings/docker.gpg ]; then
  curl -fsSL https://download.docker.com/linux/ubuntu/gpg \
    | gpg --dearmor -o /etc/apt/keyrings/docker.gpg
  chmod a+r /etc/apt/keyrings/docker.gpg
fi
source /etc/os-release  # => VERSION_CODENAME=noble (24.04)
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.gpg] \
https://download.docker.com/linux/ubuntu ${VERSION_CODENAME} stable" \
  > /etc/apt/sources.list.d/docker.list

apt-get update
apt-get install -y docker-ce docker-ce-cli containerd.io docker-buildx-plugin docker-compose-plugin
systemctl enable --now docker
usermod -aG docker ubuntu || true

# ---- Node.js (LTSを明示:必要なら NODE_MAJOR=20 などで上書き可) ----
NODE_MAJOR="${NODE_MAJOR:-22}"
curl -fsSL "https://deb.nodesource.com/setup_${NODE_MAJOR}.x" | bash -
apt-get install -y nodejs

# ---- AWS CLI v2(アーキ自動判定)----
ARCH="$(uname -m)"
if [ "$ARCH" = "x86_64" ]; then
  AWSCLI_PKG="awscli-exe-linux-x86_64.zip"
elif [ "$ARCH" = "aarch64" ] || [ "$ARCH" = "arm64" ]; then
  AWSCLI_PKG="awscli-exe-linux-aarch64.zip"
else
  echo "Unsupported arch: $ARCH" >&2
  exit 1
fi
pushd /tmp
curl -fsSLO "https://awscli.amazonaws.com/${AWSCLI_PKG}"
unzip -q "${AWSCLI_PKG}"
./aws/install
popd
rm -rf "/tmp/${AWSCLI_PKG}" /tmp/aws

# ---- AWS CDK(v2を明示)----
npm install -g aws-cdk@2

# ---- uv(python パッケージ/環境管理; ubuntuユーザに導入)----
sudo -u ubuntu bash -lc 'command -v uv >/dev/null 2>&1 || (curl -LsSf https://astral.sh/uv/install.sh | sh)'

# PATH に ~/.local/bin を通す(uv の配置先)
grep -q 'export PATH=\$HOME/.local/bin:\$PATH' /home/ubuntu/.bashrc || \
  echo 'export PATH=$HOME/.local/bin:$PATH' >> /home/ubuntu/.bashrc

# ---- AWS CLI 補完(任意)----
grep -q 'aws_completer' /home/ubuntu/.bashrc || \
  echo "complete -C '/usr/local/bin/aws_completer' aws" >> /home/ubuntu/.bashrc
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?