{"id":26452047,"url":"https://github.com/prasad-vamer/ec2code","last_synced_at":"2025-03-18T17:25:57.860Z","repository":{"id":282810060,"uuid":"945503365","full_name":"prasad-vamer/EC2Code","owner":"prasad-vamer","description":"🚀 EC2Code: - AWS EC2 Environment for software development build using AWS CDK","archived":false,"fork":false,"pushed_at":"2025-03-17T03:53:02.000Z","size":104,"stargazers_count":2,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-03-17T04:26:45.469Z","etag":null,"topics":["aws","awsec2","cdk","development-environment","docker","docker-compose","ec2","ec2-instance","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/prasad-vamer.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2025-03-09T15:24:32.000Z","updated_at":"2025-03-17T03:53:07.000Z","dependencies_parsed_at":"2025-03-17T10:01:12.884Z","dependency_job_id":null,"html_url":"https://github.com/prasad-vamer/EC2Code","commit_stats":null,"previous_names":["prasad-vamer/ec2code"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasad-vamer%2FEC2Code","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasad-vamer%2FEC2Code/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasad-vamer%2FEC2Code/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/prasad-vamer%2FEC2Code/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/prasad-vamer","download_url":"https://codeload.github.com/prasad-vamer/EC2Code/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244267710,"owners_count":20425884,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["aws","awsec2","cdk","development-environment","docker","docker-compose","ec2","ec2-instance","typescript"],"created_at":"2025-03-18T17:25:57.048Z","updated_at":"2025-03-18T17:25:57.854Z","avatar_url":"https://github.com/prasad-vamer.png","language":"TypeScript","readme":"# 🚀 EC2Code: - AWS EC2 Environment for software development build using AWS CDK\n\nThis repository provisions a **Developer Environment** on **AWS EC2** using **AWS CDK (TypeScript)**. \nThe setup includes a **VPC, NetworkACL, Security Group, and an EC2 Instance**, ensuring a secure and scalable development environment.\n\n## **🌟 Features**\n- ✅ **EC2 Instance**: Pre-configured for development tasks, enabling a remote coding environment.\n- ✅ **VPC (Virtual Private Cloud)**: Isolated networking for enhanced security.\n- ✅ **Security Group**: Controlled inbound and outbound access to allow secure SSH and development tools.\n- ✅ **Scalability**: Supports heavy workloads for software builds, testing, and development.\n- ✅ **Infrastructure as Code**: Easily deploy, modify, and manage using AWS CDK.\n- ✅ **Customizable Access Control (NetworkACL) Support**: for subnets to enhance security.\n  - Allows inbound traffic only from specified IP addresses.\n  - If no whitelist IPs are provided (in the `parameters.ts`), all inbound/outbound traffic is allowed by default.\n\n## **🚀 Upcoming Features**\n- ✨ **Resource Scheduling**: Start and stop the EC2 instance based on a  developer's schedule.\n- ✨ **Auto Scaling**: Automatically scale the EC2 instance based on the workload.\n- ✨ **Monitoring \u0026 Logging**: Implement CloudWatch for monitoring and logging.\n- ✨ **Cost Display**: Present the usage statistics along with the estimated bill amount and duration.\n\n---\n\n## **💪🏼 Technologies**\n- **AWS CDK (TypeScript)**: Infrastructure as Code for provisioning AWS resources.\n- **AWS EC2**: Virtual server for development and deployment.\n- **AWS VPC**: Isolated networking environment for secure communication.\n- **AWS Security Group**: Firewall rules for controlling inbound and outbound traffic.\n- **Docker \u0026 Docker Compose**: Containerization for building and deploying the application.\n\n---\n\n## **📌 Prerequisites**\nBefore deploying, ensure you have the following:\n- **AWS Account** with permissions to create EC2, VPC, and security groups.\n- **Docker and Docker Compose** (Latest version)- [Download](https://www.docker.com/products/docker-desktop/)\n\n---\n\n## **🛠️ Configuration: Understanding `parameters.ts`**\n\nThe `parameters.ts` file defines the environment configuration for deploying EC2 instances. It includes two primary modes:\n\n### **1️⃣ Test Mode (`test`)**\n- Used for testing and developing new features in AWS CDK.\n- Deploys a minimal EC2 instance to **reduce costs** while still allowing feature validation.\n\n### **2️⃣ Development Mode (`dev`)**\n- Deploys the **actual EC2 instance** used for software development.\n- Ensures a full-fledged development environment for engineers.\n\n### **🔹 Environment Configuration (`env`)**\n```ts\nenv: {\n  account: process.env.AWS_ACCOUNT_ID,\n  region: process.env.AWS_DEFAULT_REGION,\n}\n```\n- Defines the **AWS Account ID** and **Region** where the resources will be deployed.\n- Helps avoid **cross-stack reference errors** in AWS CDK.\n\n### **🔹 `devInstanceServiceProps`: EC2 Instance Configuration**\n- An **object**, where all the properties inside it represent the properties used under the service `devInstanceService` .\n\n### **🔊 Key Parameters in `ec2Instances` under `devInstanceServiceProps`**\n- `ec2Instances`: An **array of objects**, where each object defines an EC2 instance's parameters.\n- If you need **10 EC2 instances for 10 developers**, you simply add **10 objects** to this array.\n\n#### **1️⃣ `keyPairName`**\n- Specifies the **name of the SSH Key Pair** that will be associated with the EC2 instance.\n- Can be found in **AWS Console → EC2 → Key Pairs**.\n\n#### **2️⃣ `keyPairPublicKeyPath`** (Optional)\n- Specifies the **path** to an **existing SSH public key**.\n- If provided, **CDK will not generate a new key pair**, instead, it will use the provided public key.\n- If not provided, **CDK automatically creates a key pair** and stores it in **AWS Systems Manager Parameter Store**.\n\n📞 **Reference**: [AWS CDK Key Pair Documentation](https://docs.aws.amazon.com/cdk/api/v2/docs/aws-cdk-lib.aws_ec2.CfnKeyPair.html)\n\n#### **3️⃣ `ec2InstanceUsername`** (Optional)\n- Defines the **user account** inside the EC2 instance.\n- By default, Debian-based EC2 instances use **`admin`**.\n- If a custom username is provided, it will be created within the EC2 instance.\n- This username is used for SSH login.\n\n#### **4️⃣ `ec2InstanceType`**\n- Specifies the **EC2 instance type** for development.\n- Choose an instance type based on your workload and budget.\n\n📞 **Reference**: [AWS EC2 Instance Types](https://aws.amazon.com/ec2/instance-types/)\n\n#### **5️⃣ `ingressRules`** (Security Group Rules)\n- Defines **inbound traffic rules** for the EC2 instance's Security Group.\n- Each rule consists of:\n  - **`port`**: The port number to allow traffic (e.g., SSH, HTTP).\n  - **`source`**: Defines where the traffic is allowed from.\n\n##### **Example: Security Rules for a React Developer**\n```ts\n[\n  { port: 22, source: ec2.Peer.anyIpv4() },  // SSH access from anywhere\n  { port: 5173, source: ec2.Peer.anyIpv4() }, // Vite React app\n  { port: 3000, source: ec2.Peer.anyIpv4() }, // Backend service\n  { port: 8080, source: ec2.Peer.anyIpv4() }, // Database viewer\n]\n```\n- Ensures that developers can SSH into the instance and run their applications.\n\n---\n\n### **🫧 Summary**\n- **Test Mode (`test`)**: Cost-efficient EC2 for AWS CDK feature testing.\n- **Dev Mode (`dev`)**: Full-scale EC2 for software development.\n- **Flexible EC2 Configuration**: Supports multiple EC2 instances with customizable parameters.\n- **Automated Key Management**: Uses either an existing key pair or generates one via AWS Systems Manager.\n- **Secure Access Rules**: Defines controlled inbound access via **Security Groups**.\n\nThis structured parameterization allows teams to **dynamically provision development environments** in AWS with minimal manual effort. 🚀\n\n---\n\n## **⚙️ Setup \u0026 Deployment**\n\n### **1️⃣ Clone the Repository**\n[![Clone Repo](https://img.shields.io/badge/Clone-Repository-blue?style=for-the-badge\u0026logo=github)](https://github.com/prasad-vamer/EC2Code.git)\n\n```sh\ncd EC2Code\n```\n\n### **2️⃣ Configure AWS Credentials in Environment Variables**\n```sh\ncreate a `.env` file as in the `.env_copy` file and fill in the necessary values.\n```\n\n### **3️⃣ Build the Docker Image**\n```sh\ndocker-compose build\n```\n\n### **4️⃣ Run the Docker Container**\n```sh\ndocker compose run --rm app bash\n```\n\n### **5️⃣ Bootstrap CDK environment**\n- Initialize the CDK environment by bootstrapping the AWS environment.\n- Perform this step only if not done already.\n```sh\ncdk bootstrap\n```\n\n### **6️⃣ Deploy the CDK Stack**\n- Deploy the CDK stack to create the EC2 instance.\n```sh\ncdk deploy DevInstanceStage/*\n```\n- Deployment will take some time, once the deployment is done, you will see the public IP of the ec2 instance in the output.\n- Direct deployment like this will create the ssh key pair and store it in the AWS System manager Parameter Store.\n- This key pair will be used to ssh into the ec2 instance.\n\n#### ***📝 Note:***\n\n#### To retrieve the key pair from the parameter store, run the following command:\n\n```sh\nbash ../helper-scripts/fetch-aws-parameter-store-key.sh /ec2/keypair/YOUR_KEY_PAIR_ID ../tmp/ACCESS_KEY.pem\n```\n- Store the key pair in a safe location and use it to ssh into the ec2 instance.\n\n#### If you already have a key pair, you can pass it's public key as a parameter in the file before deploying the stacks. \n[app/lib/config/parameters.ts](app/lib/config/parameters.ts)\n\n  - replace the value of 'keyPairPublicKeyPath' with the path to your public key.\n  - since the public key need to be accessible to the CDK running inside the docker container, you can place the public key in the `tmp` folder and pass the path to keyPairPublicKeyPath.\n  - eg: `keyPairPublicKeyPath: '../tmp/your_public_key.pub'`\n\n### **7️⃣ SSH into the EC2 Instance**\n- Retrieve the public IP of the EC2 instance from the AWS Console or the output of the CDK deployment.\n- Retrieve the key pair from the parameter store using the command mentioned above.\\\n- Use the key pair to ssh into the EC2 instance:\n\n```sh\nssh -i /path/to/your/ACCESS_KEY.pem USER-NAME@YOUR_EC2_PUBLIC_IP\n```\n\n- **USER-NAME** : The user name of the EC2 instance (default: `admin`).\n  - if you want to have  your own user name, you can pass it as a parameter in the file [app/lib/config/parameters.ts](app/lib/config/parameters.ts)\n  - replace the value of 'ec2InstanceUsername' with your desired user name.\n- **YOUR_EC2_PUBLIC_IP** : The public IP of the EC2 instance.\n\n--- \n\n## **🔗 Useful Commands**\n\n### **🔑 GENERATE SSH KEY PAIR**\n```sh\nssh-keygen -t rsa -b 4096 -m PEM -f MyEc2Key.pem\n```\n\n#### **🔐 Secure the Key Pair**\n```sh\nchmod 400 MyEc2Key.pem\n```\n\n### Get the public IP of the ec2 instances\n```sh\naws ec2 describe-instances --query \"Reservations[].Instances[].PublicIpAddress\" \n```\n\n### **🧹 Clean Up**\n- Delete the CDK stack to remove the EC2 instance.\n\n```sh\ncdk destroy DevInstanceStage/*\n```\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprasad-vamer%2Fec2code","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fprasad-vamer%2Fec2code","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fprasad-vamer%2Fec2code/lists"}