Hey there, code warriors! 👩💻👨💻 Tired of your local machine wheezing under the weight of your epic projects? Let's take your Visual Studio Code setup to the cloud with Amazon EC2 AMD/Intel/Graviton instances. Buckle up for a turbo boost to your development workflow! This solution is greatly inspired by AWS's blog post on using Cloud9 with VS Code. We've repurposed and enhanced the approach to use EC2 instead of Cloud9, while injecting a bit of automation. 😉
This setup is like giving your VS Code a dose of super-soldier serum:
💪 Flex with EC2 muscle 🌐 Code from anywhere (yes, even that cool coffee shop) 💰 Keep your wallet happy with Graviton's cost-efficiency 🔒 Fort Knox-level security without the hassle of open ports
Before we dive into the code, make sure you've got:
- AWS account (with the right superpowers, err... permissions)
- VS Code installed on your trusty local machine
- AWS CLI configured and ready to roll
- An SSH client that plays nice with OpenSSH
- Your personal SSH key pair (your digital ID badge)
- The
ssm-proxy-goexecutable (our secret weapon, more on this later)
- Power Up VS Code: Install the "Remote - SSH" extension. It's like giving VS Code x-ray vision into the cloud.
- Smooth Operator: Grab the Session Manager plugin for AWS CLI. Think of it as your VIP pass to the AWS club.
- Key to the Kingdom: Generate your SSH key pair:
cd ~/.ssh
ssh-keygen -b 4096 -C 'vscode-remote-ssh' -t rsa -f id_rsa-ide- Config Magic: Set up your SSH config (~/.ssh/config) like a boss:
Host ide
IdentityFile ~/.ssh/id_rsa-ide
User ubuntu
HostName vscode-host
ProxyCommand sh -c "~/.ssh/ssm-proxy-go %h 22 myprofile ap-south-1 m7g.xlarge my-vpc ~/.ssh/id_rsa-ide.pub"
Host ide-x86
IdentityFile ~/.ssh/id_rsa-ide
User ubuntu
HostName vscode-host
ProxyCommand sh -c "~/.ssh/ssm-proxy-go %h 22 myprofile ap-south-1 m5.xlarge my-vpc ~/.ssh/id_rsa-ide.pub"- The Secret Sauce: Place the
ssm-proxy-goexecutable in a directory that's in your system's PATH (e.g., /usr/local/bin on macOS/Linux). or in.sshfolder
OS=$(uname | tr '[:upper:]' '[:lower:]'); ARCH=$(uname -m); ARCH=${ARCH/x86_64/amd64}; ARCH=${ARCH/aarch64/arm64}; ARCH=${ARCH/i386/386}; VERSION=$(curl -s https://api.github.com/repos/alphacenturai/vscode-ec2-integration/releases/latest | grep '"tag_name"' | sed -E 's/.*"([^"]+)".*/\1/'); wget -O ~/.ssh/ssm-proxy-go "https://github.com/alphacenturai/vscode-ec2-integration/releases/download/${VERSION}/ssm-proxy-go-${OS}-${ARCH}" && chmod +x ~/.ssh/ssm-proxy-go- The Final Leap: Connect VS Code to your cloud instance and watch the magic happen! 🎩✨
- 🕵️ Plays detective, checking if your instance exists
- 🎭 Sets the stage with IAM roles and security groups
- 🚀 Launches your EC2 instance into the cloud
- 🛠️ Installs the cool kids (necessary software) and rolls out the red carpet (SSH access)
- 🔄 Manages your instance's lifecycle like a helicopter parent
- 🔐 Establishes connections so secure, they'd make a spy jealous
The ssm-proxy-go program is written in Go and leverages the AWS SDK for Go to automate the process of setting up and connecting to an Amazon EC2 Graviton instance. Here's a detailed breakdown of its structure and functionality:
package main
import (
// Standard library imports
"context"
"encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"log"
"net/http"
"os"
"os/exec"
"strings"
"time"
// AWS SDK imports
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/ec2"
"github.com/aws/aws-sdk-go-v2/service/ec2/types"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/ssm"
ssmTypes "github.com/aws/aws-sdk-go-v2/service/ssm/types"
)The program uses a mix of standard library packages and AWS SDK packages to handle various operations.
type Config struct {
InstanceName string
Port string
AwsProfile string
AwsRegion string
InstanceType string
VpcName string
SshPublicKeyPath string
ManagedPolicyArn string
}This struct holds the configuration for the EC2 instance setup, including AWS settings and instance details.
The main function serves as the entry point and orchestrates the entire process:
- Parses command-line flags and arguments
- Sets up AWS configuration
- Creates necessary AWS service clients (EC2, IAM, SSM)
- Calls various functions to set up and manage the EC2 instance
- createOrUpdateInstanceRole: Manages IAM role creation and policy attachment.
- getLatestAmiID: Queries EC2 to find the latest Ubuntu AMI for ARM64.
- getVpcID and getSubnetID: Retrieve network configuration details.
- createOrUpdateSecurityGroup: Manages security group creation and rule configuration.
- createUserData: Generates a base64-encoded user data script for instance initialization.
- findOrCreateInstance: Checks for an existing instance or creates a new one.
- waitForInstanceOnline: Polls the instance status until it's ready.
- startSSMSession: Initiates an SSM session for SSH access.
The code implements robust error handling and retry mechanisms:
for attempt = 0; attempt < maxRetries; attempt++ {
resp, err := ssmClient.StartSession(ctx, input)
if err != nil {
if isTargetNotConnectedError(err) {
log.Printf("Instance %s is not connected. Retrying...", instanceID)
time.Sleep(time.Duration(attempt) * time.Second)
continue
}
log.Fatalf("Failed to start SSM session: %v", err)
}
// ... rest of the function
}The program uses Go's context package for managing timeouts and cancellations:
ctx := context.TODO()While this example uses a blank context, in production, you'd typically use a context with timeout or cancellation capabilities.
By leveraging Go's concurrency features, robust standard library, and the powerful AWS SDK, this program creates a seamless bridge between local development environments and cloud-based compute resources. It enables developers to harness the power of EC2 Graviton instances within their familiar VS Code environment, all while maintaining security best practices and optimizing for cost-efficiency.