Skip to content

This repository introduces how to access and use VSCode hosted on EC2 from a browser. The connection is made via Session Manager, so IAM permissions are used for authentication. The access destination will be localhost.

License

aws-samples/vscode-on-ec2-for-prototyping

Note

日本語のドキュメントは こちら

Visual Studio Code on EC2

This repository introduces how to access and use VSCode hosted on EC2 from a browser. The connection is made via Session Manager, so IAM permissions are used for authentication. The access destination will be localhost. Please note that this repository does not introduce connecting from your local VSCode to an EC2 instance via Remote SSH.

Features

  • You can use VSCode from a browser
  • Node.js environment is installed
  • 128 GB of storage is provided by default
  • aws cli can be executed with AdministratorAccess equivalent permissions
  • The EC2 instance hosting VSCode belongs to a Private Subnet, so it is not exposed to the internet. The connection is made via Session Manager.

Prerequisites

  • Node.js runtime environment
  • aws command (AdministratorAccess equivalent permissions are required to run AWS CDK)
  • git command
  • jq command (not required. Needed when executing session.sh described later)

If it is difficult to prepare the environment locally, CloudShell can be used as an alternative, but the steps from session.sh onwards need to be performed locally (because a session is created to localhost via SessionManager).

Installation

First, clone this repository.

git clone https://github.com/aws-samples/vscode-on-ec2-for-prototyping

The application uses the AWS Cloud Development Kit(CDK) for deployment. Node.js is required to run. First, run the following command. Run all commands from the root of the repository.

npm ci

If you have never used CDK before, a Bootstrap process is required only the first time. The following command is not required if already bootstrapped.

npx cdk bootstrap

Then deploy the AWS resources with the following command:

npx cdk deploy

After deployment completes, check that the EC2 instance was created in the management console. Also please confirm that the Status check changes from Initializing to checks passed.

Once checks passed is confirmed, run session.sh to create a session:

./session.sh

If Unix-like commands cannot be used locally, run the following command instead. The Instance ID and Private IP can be confirmed in the management console mentioned above, or output when running cdk deploy as VscodeOnEc2ForPrototypingStack.InstanceID and VscodeOnEc2ForPrototypingStack.PrivateIP respectively.

# Replace the two values enclosed in <>

aws ssm start-session \
    --target <EC2 instance Instance ID> \
    --document-name AWS-StartPortForwardingSessionToRemoteHost \
    --parameters "{\"portNumber\":[\"8080\"],\"localPortNumber\":[\"8080\"],\"host\":[\"<EC2 instance Private IP>\"]}"

Once the session is created, open http://localhost:8080 in your browser. If it does not connect, please refer to Troubleshooting.

Configurations

The values in the context of cdk.json can be modified to change some items.

  • volume The storage size (GB) of the EC2 instance hosting VSCode
  • nvm The version of nvm used to install Node.js
  • node The version of Node.js

Troubleshooting

The same phenomenon described in this Issue may occur. **If the browser cannot connect after creating a session, first suspect this.

To check for errors, first open the management console and select the created EC2 instance. Then click Connect at the top and open the Session Manager tab and click Connect.

Open a terminal and run the following command. This will show the execution results of the commands run when initializing the EC2 instance:

sudo cat /var/log/cloud-init-output.log

If this shows an error like [Errno 2] No such file or directory: '/var/cache/dnf/amazonlinux-..., the code command installation failed. In that case, reinstall with:

sudo yum install -y code

After successful installation, run:

sudo systemctl start code-server

Try creating a session with session.sh and connecting in the browser (http://localhost:8080) again. You will no longer need to run these steps again after the initial code installation, such as when closing and reopening the browser tab, or restarting the EC2 instance.

Future works

  • Make it possible to import existing VPC
  • Allow selecting instance type

Cleanup

To delete the environment, run the following command:

npx cdk destroy

Security

See CONTRIBUTING for more information.

License

This library is licensed under the MIT-0 License. See the LICENSE file.

About

This repository introduces how to access and use VSCode hosted on EC2 from a browser. The connection is made via Session Manager, so IAM permissions are used for authentication. The access destination will be localhost.

Topics

Resources

License

Code of conduct

Security policy

Stars

Watchers

Forks