Below is the detailed guide on how to deploy the SkillSync-ASG.yml
CloudFormation template using AWS CLI.
-
AWS CLI Installed: Ensure you have the AWS CLI installed on your machine. You can download and install it from here.
-
Configured AWS CLI: Configure your AWS CLI with your credentials. Run:
aws configure
and provide your
AWS Access Key ID
,AWS Secret Access Key
,Default region name
, andDefault output format
. -
IAM Permissions: Ensure that the IAM user or role you are using has sufficient permissions to create and manage CloudFormation stacks, VPCs, EC2 instances, and Auto Scaling Groups.
Download the file named SkillSync-ASG.yml
and use this file in your subsequent AWS CLI Commands.
You also have the option of using the AWS Management Console to create the stack in Step 3 however specific steps for this is not provided here.
Use the following AWS CLI command to create the CloudFormation stack. This command will deploy the resources defined in your SkillSync-ASG.yml
template.
aws cloudformation create-stack \
--stack-name SkillSync-ASG-Stack \
--template-body file://SkillSync-ASG.yml \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
--region <your-region>
Replace <your-region>
with your desired AWS region, such as us-east-1
.
CAPABILITY_IAM: Required for templates that include IAM resources.
CAPABILITY_NAMED_IAM: Required for templates that include IAM resources with custom names.
Purpose: These capabilities ensure that you acknowledge the security implications of creating or modifying IAM resources through your CloudFormation template.
You can monitor the progress of your stack creation using the AWS Management Console or the following AWS CLI command:
aws cloudformation describe-stacks \
--stack-name SkillSync-ASG-Stack
This command will provide the status of your stack and any resources being created.
If you need to make changes to your template and update the stack, use the update-stack
command:
aws cloudformation update-stack \
--stack-name SkillSync-ASG-Stack \
--template-body file://SkillSync-ASG.yml \
--capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \
--region <your-region>
To test the scaling in and scaling out of your Auto Scaling Group (ASG), you can simulate load on your EC2 instances using a tool like stress
. Here’s a step-by-step guide on how to do this:
First, you need to connect to one of the EC2 instances launched by your ASG.
-
Find the Instance ID:
aws ec2 describe-instances \ --filters "Name=tag:Name,Values=Skillsync-ASG" \ --query "Reservations[*].Instances[*].InstanceId" \ --output text
-
Connect to the Instance:
ssh -i /path/to/your/Skillsync-Key.pem ec2-user@<Public-IP-Address>
Once connected to the instance, install the stress
tool.
sudo yum install -y epel-release
sudo yum install -y stress
Run the stress
command to simulate high CPU load, which should trigger the scale-out policy.
stress --cpu 4 --timeout 300
This command stresses 4 CPU cores for 300 seconds (5 minutes). Adjust the parameters based on your scaling policies and instance specifications.
Monitor the scaling activity to see if the ASG launches additional instances.
aws autoscaling describe-scaling-activities \
--auto-scaling-group-name Skillsync-ASG
You can also use the AWS Management Console to check the status of your ASG and see new instances being added.
To test scaling in, you can stop the stress
tool (if it’s running in the background) or simply wait for the cooldown period to end and the CPU usage to go back to normal levels.
After the load decreases, the ASG should terminate some of the instances according to the scale-in policy. Again, you can monitor this using the AWS CLI or the AWS Management Console.
To test the scaling in and out policies, you can manually trigger them or simulate a load condition.
-
List Scaling Policies
aws autoscaling describe-policies \ --auto-scaling-group-name <Your-ASG-Name>
-
Execute Scale-Out Policy
aws autoscaling execute-policy \ --auto-scaling-group-name <Your-ASG-Name> \ --policy-name ScaleOutPolicy
-
Execute Scale-In Policy
aws autoscaling execute-policy \ --auto-scaling-group-name <Your-ASG-Name> \ --policy-name ScaleInPolicy
-
Simulate Load with Custom Metrics
aws cloudwatch put-metric-data \ --namespace "AWS/EC2" \ --metric-name "CPUUtilization" \ --dimensions "AutoScalingGroupName=<Your-ASG-Name>" \ --value 90 \ --unit Percent
-
Check Scaling Activities
aws autoscaling describe-scaling-activities \ --auto-scaling-group-name <Your-ASG-Name>
-
Check Instance Status
aws ec2 describe-instances \ --filters "Name=tag:Name,Values=Skillsync-ASG"
-
Create Stack
aws cloudformation create-stack \ --stack-name SkillSync-ASG-Stack \ --template-body file://SkillSync-ASG.yml \ --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \ --region us-east-1
-
Update Stack
aws cloudformation update-stack \ --stack-name SkillSync-ASG-Stack \ --template-body file://SkillSync-ASG.yml \ --capabilities CAPABILITY_IAM CAPABILITY_NAMED_IAM \ --region us-east-1
-
Delete Stack
aws cloudformation delete-stack \ --stack-name SkillSync-ASG-Stack \ --region us-east-1
-
Trigger Scale-Out Policy
aws autoscaling execute-policy \ --auto-scaling-group-name Skillsync-ASG \ --policy-name ScaleOutPolicy
-
Trigger Scale-In Policy
aws autoscaling execute-policy \ --auto-scaling-group-name Skillsync-ASG \ --policy-name ScaleInPolicy
To clean up the resources created using Cloudformation you need to delete the stack and all associated resources, use the delete-stack
command:
aws cloudformation delete-stack \
--stack-name SkillSync-ASG-Stack \
--region <your-region>