Skip to content

A Pulumi program to deploy a simple Load Balanced EC2 Application with Autoscaling based on CPU Metrics

Notifications You must be signed in to change notification settings

jehielmartinez/aws-lb-autoscaling-ec2

Repository files navigation

AWS Infrastructure with Auto Scaling, S3, and Load Balancer using Pulumi

This Pulumi program creates a basic AWS infrastructure including:

  • VPC (Virtual Private Cloud) with subnets.
  • S3 Bucket for object storage.
  • EC2 Instances using Auto Scaling with dynamic scaling policies based on CPU utilization.
  • Elastic Load Balancer (ELB) with public access.
  • IAM Roles to allow EC2 instances access to the S3 bucket.
  • CloudWatch Alarms to trigger scaling actions for the Auto Scaling Group (ASG) when CPU usage crosses thresholds.
  • Optionally, the program also includes configuration for HTTPS using an ACM certificate and Route 53.

Prerequisites

  1. Install Pulumi CLI.

  2. Install AWS CLI and configure your credentials:

    aws configure
  3. Create an SSH key pair in your AWS account to allow SSH access to EC2 instances:

    aws ec2 create-key-pair --key-name my-keypair --query "KeyMaterial" --output text > ~/.ssh/my-keypair.pem
    chmod 400 ~/.ssh/my-keypair.pem

    or create a key pair using the AWS Console. keep the name of the key pair as my-keypair or replace it with your key name in the configuration.

Configuration

This Pulumi program uses Pulumi configuration settings to define various parameters. Set the following values before deploying:

pulumi config set aws:region us-west-2                   # AWS region
pulumi config set appName my-app                         # Application name
pulumi config set billingId 123456                       # Billing ID tag
pulumi config set bucketName my-app-bucket-123456        # S3 bucket name
pulumi config set instanceType t2.micro                  # EC2 instance type 
pulumi config set sshKeyPairName my-keypair              # SSH key pair name (replace with your key name)
pulumi config set asgMinSize 1                           # Min instances in the Auto Scaling Group
pulumi config set asgMaxSize 5                           # Max instances in the Auto Scaling Group
pulumi config set asgDesiredCapacity 2                   # Desired number of instances
pulumi config set cpuHighThreshold 80                    # CPU threshold for scaling out
pulumi config set cpuLowThreshold 20                     # CPU threshold for scaling in
pulumi config set httpsEnabled false                     # Enable HTTPS
pulumi config set domainName example.com                 # Domain name for HTTPS
pulumi config set myIp 192.168.1.1                       # Your IP address for SSH access

Deployment

Once the configuration is set up, deploy the stack by running the following commands:

  1. Initialize Pulumi

    If this is your first time using Pulumi, initialize your Pulumi stack:

    pulumi stack init dev
  2. Preview the deployment

    You can preview what resources will be created with the following command:

    pulumi preview
  3. Deploy the infrastructure

    To deploy the infrastructure to AWS:

    pulumi up

    This command will prompt you to confirm before creating the resources. Type "yes" to proceed.

  4. Access the Load Balancer

    Once the deployment is complete, Pulumi will output the DNS name of your Load Balancer:

    LoadBalancerDNSName: "my-app-lb-123456.elb.amazonaws.com"

    You can access your EC2 instances behind the Load Balancer by opening this URL in a browser.

SSH Access to EC2 Instances

To SSH into your EC2 instances, use the key pair specified in the configuration:

ssh -i ~/.ssh/my-keypair.pem ec2-user@<ec2-instance-public-ip>

Auto Scaling Based on CPU Utilization

The Auto Scaling Group will automatically add instances when CPU utilization exceeds 80% and scale in when it drops below 20%. You can modify these thresholds in the configuration:

  • cpuHighThreshold: Set the CPU usage to trigger a scale-out action.
  • cpuLowThreshold: Set the CPU usage to trigger a scale-in action.

Cleanup

To destroy the resources and avoid incurring additional AWS charges:

pulumi destroy

You can also remove the stack if it's no longer needed:

pulumi stack rm dev

Further Documentation

This README provides all the necessary information to deploy the provided infrastructure. For more advanced use-cases or customizations, refer to the Pulumi and AWS documentation.

About

A Pulumi program to deploy a simple Load Balanced EC2 Application with Autoscaling based on CPU Metrics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published