ec2 instances and AWS auto scaling group - amazon-ec2

I use the module, https://github.com/terraform-aws-modules/terraform-aws-ec2-instance to provision the ec2 instances on AWS. I would like to have AWS auto scaling group feature implemented, i.e. in case one of the ec2 instances is not healthy, AWS auto replaces it. But, I do not see the code, resource "aws_autoscaling_group" inside the above ec2 instance module.
Question:
Is it reasonable to implement AWS auto scaling group with the ec2 module? I understand we can implement AWS auto scaling group with eks. But, I am not sure about ec2 instances.
If it is possible, how to do it?

Yes, it is not only reasonable, but actually considered a best practice to use an auto scaling group for EC2 instances.
you can do so using the terraform autoscaling module: https://registry.terraform.io/modules/terraform-aws-modules/autoscaling/aws/latest

Related

How to auto on-board EC2 instances in an autoscaling group?

Currently, I have a CFN template to creates an auto-scaling group, which launches instances based on a launch template. I would like to onboard with Systems Manager to automate patching, but I can only find the UI (Quick Setup) and not CFN for performing all those actions; I want to make sure any new instances spun up in the auto-scaling group will be onboarded with SSM as well and become managed instances. How do I go about doing that?
There are few prerequisites.
You can refer this : https://aws.amazon.com/premiumsupport/knowledge-center/systems-manager-ec2-instance-not-appear/
In short, ssm agent must be running and must be able to reach ssm endpoints (public or vpc endpoints) and must have necessary permission in instance profile to make API calls to SSM service, in order to register the instance.

Sagemaker usage of EC2 instances

Is there a way to view/monitor AWS Sagemaker's usage of EC2 instances?
I am running a Sagemaker endpoint and tried to find its instances (ml.p3.2xlarge in this case) in the EC2 UI, but couldn't find them.
ml EC2 instances do not appear in the EC2 console. You can find their metrics in Cloudwatch though, and create dashboards to monitor what you need:

AWS ECS: Will Service Auto Scaling create EC2 instance automately?

I am confused about how Service AutoScaling automately works. Will it create EC2 instance automately?
I create it and add it to a Cluster's service, but it does no create EC2 for placing my required number of tasks. Is any thing wrong with my settings? I check the [Events] and see "service s2 was unable to place a task because no container instance met all of its requirements. ", but shouldn't it create a EC2 if no instance met? Please give me some advice, thanks in advance.
but shouldn't it create a EC2 if no instance met
Not really. There are two types of scaling policies: scaling policies on an ECS service and scaling policies on the ECS cluster. Instances are added based on cluster scaling policies, and that's what you should set up in addition to your service scaling policy.
AWS has a couple of detailed tutorials on scaling ECS clusters:
https://aws.amazon.com/blogs/compute/automatic-scaling-with-amazon-ecs/
https://docs.aws.amazon.com/AmazonECS/latest/developerguide/cloudwatch_alarm_autoscaling.html
AWS Elastic Container Services has two methods to deploy containers over aws environment
Where you no need to worry about orchestration of containers (task in aws)
Fargate (Available in few regions like N.Virginia)
Using EC2 in ECS
I guess you are using 2nd option to deploy application over ECS where you can provide details of scaling tasks/containers not ec2 instances.
For Auto-scaling of ec2 instances you should look into ASG of AWS.
As far as AWS ECS is concerned you need some building blocks which are as follows-
Cluster
Task definition (Memory, Network and Storage configs of tasks)
Service contains EC2 instance configuration
Auto scaling policies if you want to auto-scale tasks

How to perform autoscaling in ec2 inside a vpc?

I want to perform autoscaling without using CLI tools. I want to do it from the console itself.
The instance is in vpc ? how can i apply the autoscale policy on instance
Any lead is appriciated.
Thanks in advance.
Documentation:
Follow the instructions on how to Set Up an Auto-Scaled and Load-Balanced Application
Notes:
The instance, created outside of AutoScaling Group can be added to Elastic Load Balancer, but will not be monitored or managed by AutoScaling group.
Instance, created outside of AutoScaling Group can be marked as unhealthy by Elastic Load Balancer if the health check fails, but it will not cause AutoScaling Group to spawn a new instance.

Adding Spot Instances to the Security Group of an RDS Instance

I have an RDS MySQL persistant instance, and I am trying to run spot instances which are created once every 8 hours and then destroyed.
The issue I am having is that I don't understand how can I add those spot instances which have dynamic IPs to the security group of my RDS Instance in order to let them perform queries,etc.
Should I grab the IP as soon as the spot instance is created and the add it to the security group of the RDS instance each time a new spot instances is created? Also destroy it every time the spot instance is terminated?
Any tip on what approach to take will be much appreciated!
Thanks!
Assuming that both your spot instances and RDS are in the same region: when setting up an RDS security group, you can also allow machines in EC2 security groups to connect to your RDS machine. Simply add all your spot instances to a EC2 security group and grant access to RDS for this security group.
If your RDS and spot instances are in different regions, you could use the AWS API or an AWS API client like boto for Python to add the IPs automatically.

Resources