This repository provisions a Developer Environment on AWS EC2 using AWS CDK (TypeScript). The setup includes a VPC, NetworkACL, Security Group, and an EC2 Instance, ensuring a secure and scalable development environment.
- β EC2 Instance: Pre-configured for development tasks, enabling a remote coding environment.
- β VPC (Virtual Private Cloud): Isolated networking for enhanced security.
- β Security Group: Controlled inbound and outbound access to allow secure SSH and development tools.
- β Scalability: Supports heavy workloads for software builds, testing, and development.
- β Infrastructure as Code: Easily deploy, modify, and manage using AWS CDK.
- β
Customizable Access Control (NetworkACL) Support: for subnets to enhance security.
- Allows inbound traffic only from specified IP addresses.
- If no whitelist IPs are provided (in the
parameters.ts
), all inbound/outbound traffic is allowed by default.
- β¨ Resource Scheduling: Start and stop the EC2 instance based on a developer's schedule.
- β¨ Auto Scaling: Automatically scale the EC2 instance based on the workload.
- β¨ Monitoring & Logging: Implement CloudWatch for monitoring and logging.
- β¨ Cost Display: Present the usage statistics along with the estimated bill amount and duration.
- AWS CDK (TypeScript): Infrastructure as Code for provisioning AWS resources.
- AWS EC2: Virtual server for development and deployment.
- AWS VPC: Isolated networking environment for secure communication.
- AWS Security Group: Firewall rules for controlling inbound and outbound traffic.
- Docker & Docker Compose: Containerization for building and deploying the application.
Before deploying, ensure you have the following:
- AWS Account with permissions to create EC2, VPC, and security groups.
- Docker and Docker Compose (Latest version)- Download
The parameters.ts
file defines the environment configuration for deploying EC2 instances. It includes two primary modes:
- Used for testing and developing new features in AWS CDK.
- Deploys a minimal EC2 instance to reduce costs while still allowing feature validation.
- Deploys the actual EC2 instance used for software development.
- Ensures a full-fledged development environment for engineers.
env: {
account: process.env.AWS_ACCOUNT_ID,
region: process.env.AWS_DEFAULT_REGION,
}
- Defines the AWS Account ID and Region where the resources will be deployed.
- Helps avoid cross-stack reference errors in AWS CDK.
- An object, where all the properties inside it represent the properties used under the service
devInstanceService
.
ec2Instances
: An array of objects, where each object defines an EC2 instance's parameters.- If you need 10 EC2 instances for 10 developers, you simply add 10 objects to this array.
- Specifies the name of the SSH Key Pair that will be associated with the EC2 instance.
- Can be found in AWS Console β EC2 β Key Pairs.
- Specifies the path to an existing SSH public key.
- If provided, CDK will not generate a new key pair, instead, it will use the provided public key.
- If not provided, CDK automatically creates a key pair and stores it in AWS Systems Manager Parameter Store.
π Reference: AWS CDK Key Pair Documentation
- Defines the user account inside the EC2 instance.
- By default, Debian-based EC2 instances use
admin
. - If a custom username is provided, it will be created within the EC2 instance.
- This username is used for SSH login.
- Specifies the EC2 instance type for development.
- Choose an instance type based on your workload and budget.
π Reference: AWS EC2 Instance Types
- Defines inbound traffic rules for the EC2 instance's Security Group.
- Each rule consists of:
port
: The port number to allow traffic (e.g., SSH, HTTP).source
: Defines where the traffic is allowed from.
[
{ port: 22, source: ec2.Peer.anyIpv4() }, // SSH access from anywhere
{ port: 5173, source: ec2.Peer.anyIpv4() }, // Vite React app
{ port: 3000, source: ec2.Peer.anyIpv4() }, // Backend service
{ port: 8080, source: ec2.Peer.anyIpv4() }, // Database viewer
]
- Ensures that developers can SSH into the instance and run their applications.
- Test Mode (
test
): Cost-efficient EC2 for AWS CDK feature testing. - Dev Mode (
dev
): Full-scale EC2 for software development. - Flexible EC2 Configuration: Supports multiple EC2 instances with customizable parameters.
- Automated Key Management: Uses either an existing key pair or generates one via AWS Systems Manager.
- Secure Access Rules: Defines controlled inbound access via Security Groups.
This structured parameterization allows teams to dynamically provision development environments in AWS with minimal manual effort. π
cd EC2Code
create a `.env` file as in the `.env_copy` file and fill in the necessary values.
docker-compose build
docker compose run --rm app bash
- Initialize the CDK environment by bootstrapping the AWS environment.
- Perform this step only if not done already.
cdk bootstrap
- Deploy the CDK stack to create the EC2 instance.
cdk deploy DevInstanceStage/*
- Deployment will take some time, once the deployment is done, you will see the public IP of the ec2 instance in the output.
- Direct deployment like this will create the ssh key pair and store it in the AWS System manager Parameter Store.
- This key pair will be used to ssh into the ec2 instance.
bash ../helper-scripts/fetch-aws-parameter-store-key.sh /ec2/keypair/YOUR_KEY_PAIR_ID ../tmp/ACCESS_KEY.pem
- Store the key pair in a safe location and use it to ssh into the ec2 instance.
If you already have a key pair, you can pass it's public key as a parameter in the file before deploying the stacks.
- replace the value of 'keyPairPublicKeyPath' with the path to your public key.
- 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. - eg:
keyPairPublicKeyPath: '../tmp/your_public_key.pub'
- Retrieve the public IP of the EC2 instance from the AWS Console or the output of the CDK deployment.
- Retrieve the key pair from the parameter store using the command mentioned above.\
- Use the key pair to ssh into the EC2 instance:
ssh -i /path/to/your/ACCESS_KEY.pem USER-NAME@YOUR_EC2_PUBLIC_IP
- USER-NAME : The user name of the EC2 instance (default:
admin
).- if you want to have your own user name, you can pass it as a parameter in the file app/lib/config/parameters.ts
- replace the value of 'ec2InstanceUsername' with your desired user name.
- YOUR_EC2_PUBLIC_IP : The public IP of the EC2 instance.
ssh-keygen -t rsa -b 4096 -m PEM -f MyEc2Key.pem
chmod 400 MyEc2Key.pem
aws ec2 describe-instances --query "Reservations[].Instances[].PublicIpAddress"
- Delete the CDK stack to remove the EC2 instance.
cdk destroy DevInstanceStage/*