Code Monkey home page Code Monkey logo

vscode-on-ec2-for-prototyping's Introduction

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.

vscode-on-ec2-for-prototyping's People

Contributors

tbrandaws avatar amazon-auto avatar

Stargazers

Taichiro Suzuki avatar  avatar

Watchers

Taichiro Suzuki avatar Yukinobu Mine avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.