3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

【AWS】VPC内でCloudShellを起動する

Last updated at Posted at 2025-03-25

はじめに

本ブログに記載した内容は個人の見解であり、所属する会社、組織とは全く関係ありません。

簡単なCLIコマンドの実行やトラブルシュート時にはCloudShellを使うことがあります。
しかし、パブリックな環境でのCloudShellはインターネットに接続されているため、セキュリティ面で少し不安がありました。ところが昨年のアップデートで、VPC内でもCloudShellが起動できるようになったとのことで、試してみた記事になります。
参考:Using AWS CloudShell in Amazon VPC

IAMの設定

VPC内でCloudShellが起動できるようなIAMポリシーを作成します。
CloudShellへのフルアクセス権限に加えて、CloudShellに紐づけるENI(Elastic Network Interface)を管理するために以下の権限も付与します。

  • ec2:DescribeVpcs
  • ec2:DescribeSubnets
  • ec2:DescribeSecurityGroups
  • ec2:DescribeDhcpOptions
  • ec2:DescribeNetworkInterfaces
  • ec2:CreateTags
  • ec2:CreateNetworkInterface
  • ec2:CreateNetworkInterfacePermission
  • ec2:DeleteNetworkInterface

なお、ec2:DeleteNetworkInterface は必須ではありませんが、CloudShell用に作成されたENIをCloudShellの終了時に削除するためには必要となります。(参考:IAMポリシーの詳細
以上を踏まえて、CloudShellFullAccessIncludingVPC というIAMポリシーを作成します。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "AllowCloudShellOperations",
      "Effect": "Allow",
      "Action": [
        "cloudshell:*"
      ],
      "Resource": "*"
    },
    {
      "Sid": "AllowDescribeVPC",
      "Effect": "Allow",
      "Action": [
        "ec2:DescribeDhcpOptions",
        "ec2:DescribeNetworkInterfaces",
        "ec2:DescribeSubnets",
        "ec2:DescribeSecurityGroups",
        "ec2:DescribeVpcs"
      ],
      "Resource": "*"
    },
    {
      "Sid": "AllowCreateTagWithCloudShellKey",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateTags"
      ],
      "Resource": "arn:aws:ec2:*:*:network-interface/*",
      "Condition": {
        "StringEquals": {
          "ec2:CreateAction": "CreateNetworkInterface"
        },
        "ForAnyValue:StringEquals": {
          "aws:TagKeys": "ManagedByCloudShell"
        }
      }
    },
    {
      "Sid": "AllowCreateNetworkInterfaceWithSubnetsAndSG",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateNetworkInterface"
      ],
      "Resource": [
        "arn:aws:ec2:*:*:subnet/*",
        "arn:aws:ec2:*:*:security-group/*"
      ]
    },
    {
      "Sid": "AllowCreateNetworkInterfaceWithCloudShellTag",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateNetworkInterface"
      ],
      "Resource": "arn:aws:ec2:*:*:network-interface/*",
      "Condition": {
        "ForAnyValue:StringEquals": {
          "aws:TagKeys": "ManagedByCloudShell"
        }
      }
    },
    {
      "Sid": "AllowCreateNetworkInterfacePermissionWithCloudShellTag",
      "Effect": "Allow",
      "Action": [
        "ec2:CreateNetworkInterfacePermission"
      ],
      "Resource": "arn:aws:ec2:*:*:network-interface/*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/ManagedByCloudShell": ""
        }
      }
    },
    {
      "Sid": "AllowDeleteNetworkInterfaceWithCloudShellTag",
      "Effect": "Allow",
      "Action": [
        "ec2:DeleteNetworkInterface"
      ],
      "Resource": "arn:aws:ec2:*:*:network-interface/*",
      "Condition": {
        "StringEquals": {
          "aws:ResourceTag/ManagedByCloudShell": ""
        }
      }
    }
  ]
}

なお、上記のIAMポリシーではパブリック環境でもCloudShellが起動できてしまうため、VPC内でのみ起動できるように制限するためのIAMポリシー DenyCloudShellPublic を作成します。

{
  "Version": "2012-10-17",
  "Statement": [
    {
      "Sid": "DenyCloudShellNonVpcEnvironments",
      "Action": [
        "cloudshell:CreateEnvironment"
      ],
      "Effect": "Deny",
      "Resource": "*",
      "Condition": {
        "Null": {
          "cloudshell:VpcIds": "true"
        }
      }
    }
  ]
}

作成した CloudShellFullAccessIncludingVPC, DenyCloudShellPublic をユーザにアタッチします。

CloudShellの起動

検索窓にCloudShell と入力し、CloudShellを起動します。
image.png

特に何も制限していなければパブリック環境でCloudShellが起動しますが、今回は DenyCloudShellPublic によって制限しているためエラーが起きます。
image-1.png

Create a VPC environment をクリックし、VPC内でCloudShellを起動するVPCやサブネット、セキュリティグループを設定します。
セキュリティグループではインバウンドルールは設定せず、アウトバウンドルールのみフルオープンにしています。
通信先を絞る場合は、アウトバウンドルールを絞るようにします。
image-2.png

Create をクリックすると、環境が作成され、VPC内でCloudShellが起動します。
image-3.png

まとめ

VPC内でCloudShellを起動する方法を試してみました。
踏み台サーバーを作成せずともVPC内のリソースに気軽にアクセスできたりする等、便利ですので今後も活用していこうと思います。

本ブログに記載した内容は個人の見解であり、所属する会社、組織とは全く関係ありません。

3
2
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
3
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?