6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

前回作成したLocalStack上にEC2インスタンスを起動します。まだLocalStackを起動していない方は以下の記事を参考にしてください。

また、LocalStack上でEC2インスタンス起動するための説明は以下のページに記載があります。

1. イメージファイル取得

まずはEC2インスタンス用のDockerイメージファイルを取得します。

docker pull ubuntu:focal
docker tag ubuntu:focal localstack-ec2/ubuntu-focal-ami:ami-000001

2. コンテナ起動 & Packageインストール

次にコンテナを起動して最低限のパッケージを入れておきます。これをしないとEC2インスタンスとして起動しても何もできないので、事前にやっておきます。

docker container run -it --name test01 localstack-ec2/ubuntu-focal-ami:ami-000001 /bin/bash
apt update
apt install -y wget curl vim git procps net-tools iputils-ping

3. コミット

別ターミナルを立ち上げて、起動したコンテナをコミットします。

docker ps #コンテナIDを特定
docker commit コンテナID localstack-ec2/ubuntu-focal-ami:ami-000002

コミットしたら、立ち上げたコンテナはexitしておきましょう。

4. EC2インスタンスとして起動

続いて、LocalStackコンテナにログインし、SSH-Keyを作成してEC2インスタンスを起動します。LocalStackコンテナ外からSSHできるよう鍵は/tmpにコピーしておきます。

docker exec -it localstack-main /bin/bash
awslocal ec2 create-key-pair --key-name newKey --query 'KeyMaterial' --output text | tee newKey.pem
awslocal ec2 run-instances --image-id ami-000002 --count 1 --instance-type t3.small --key-name newKey
cp -pr newKey.pem /tmp/

LocalStackのステータスを見ると、インスタンスが起動しているのが確認できます。
image.png

5. EC2インスタンスへ接続

先程の手順にてコピーした鍵をDockerホスト側にコピーしてEC2インスタンスとして起動したコンテナを確認します。

docker cp localstack-main:/tmp/newKey.pem ./
docker ps
49d240ddc1ce   localstack-ec2/ubuntu-focal-ami:ami-000002   "sleep infinity"         27 hours ago   Up 27 hours           0.0.0.0:30145->22/tcp, :::30145->22/tcp

ここではDockerホスト30145番PortがEC2インスタンスの22番Portに紐づけられていることがわかります。では、以下のコマンドでEC2インスタンスとして起動したコンテナにログインします。

root@localstack001:~# ssh -i newKey.pem -p 30145 root@127.0.0.1
root@49d240ddc1ce:~# #ログインOK

起動したEC2インスタンス上でパッケージを入れたい場合は/etc/resolv.confを修正しておきましょう

vim /etc/resolv.conf
/etc/resolv.conf
# Generated by Docker Engine.
# This file can be edited; Docker Engine will not make further changes once it
# has been modified.

#nameserver 127.0.0.11  # コメント
nameserver 8.8.8.8      # 追記
search .
options ndots:0

# Based on host file: '/run/systemd/resolve/resolv.conf' (internal resolver)
# ExtServers: [172.18.0.2]
# Overrides: [nameservers]
# Option ndots from: internal

これでinternetに出れます。

root@49d240ddc1ce:~# apt update
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease [128 kB]
Hit:3 http://archive.ubuntu.com/ubuntu focal-backports InRelease
Get:4 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [1530 kB]
Get:5 http://archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [4228 kB]
Get:6 http://security.ubuntu.com/ubuntu focal-security InRelease [128 kB]
Get:7 http://security.ubuntu.com/ubuntu focal-security/main amd64 Packages [3758 kB]
Get:8 http://security.ubuntu.com/ubuntu focal-security/universe amd64 Packages [1245 kB]
Fetched 11.0 MB in 8s (1321 kB/s)
Reading package lists... Done
Building dependency tree
Reading state information... Done
All packages are up to date.
6
4
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
6
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?