Scheduling to start and stop Ec2 instance in aws compaired to azure? - amazon-web-services

I am trying to start and stop my EC2 instances at a specific time of the day.
There is a scheduled start and stop VMS options available in Azure.
I am wondering if something similar to that is present in AWS.
if not what is the best way to achieve it?
assuming 7am I start my EC2 and stop it by 9pm IST. I am also aware of the cloud watch event integrated with lamnda option

Based on the comments.
The lambda function would be only required to start your instance, if you want to trigger it using CloudWatch (CW) Event rule. The reason is, that you can stop the instance directly, using CW built-in targets:
Built-in targets: EC2 CreateSnapshot API call, EC2 RebootInstances API call, EC2 StopInstances API call, and EC2 TerminateInstances API call.
Sadly, there is no build-in target for starting the instance, thus lambda function is needed for that.

Create two different Lambda function to launch and terminate an EC2 instance and schedule them via CloudWatch. For the Lambda you can use any language from the AWS SDK. To select a particular instance to terminate, the EC2 instance can be tagged to segregate it. Here is the solution for the same.

Here is the complete guide https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/. Let me know if that helps.
As suggested by Praveen Sripati https://aws.amazon.com/solutions/implementations/instance-scheduler/ works pretty well.

Related

Send AWS EC2 metrics to AWS Elasticsearch Service Domain for monitoring in Kibana

I am stuck on one point I have created one EC2 Linux based instance in Aws.
Now I want to send the EC2 metrics data to the managed Elasticsearch domain for monitoring purposes in Kiban, I go through the cloud watch console and check the metric is present of instance but didn't get how to connect with the Elasticsearch domain that I have created.
Can anyone please help me with this situation?
There is no build in mechanism for extraction/streaming of metrics data points in real time. You have to develop a custom solution for that. For example, by having a lambda function which is invoked every minute and which reads data points using get_metric_data. The the lambda would inject the points into your ES.
To invoke a lambda function periodically, e.g. every 1 minute you would have to setup CloudWatch Event rule with schedule Expressions. Lambda function would also need to have permissions granted to interact with CloudWatch metrics.
Welcome to SO :)
An alternative to the solution suggested by Marcin is to install metricbeat on the EC2 Instance and configure the metricbeat config file to send metrics to your Managed AWS ES Domain.
This is pretty simple and you should be able to do this fairly quickly.

AWS EC2 standalone instance Windows , Can I include start stop schdule in cloud formation?

I am already doing scale in scale out for auto scaling stack through scheduled actions.
I am in need of to auto start and stop for standalone EC2 ( not part of ASG)just wondering if CF support that ?
You can use the AWS Instance Scheduler to automate the starting and stopping of EC2 machines. Here you can find the documentation on how to set this up manually, and there is also a nice tutorial to follow along if the documentation is a bit too heavy.
The AWS Instance Scheduler deployment can also be setup using CloudFormation. AWS provides these examples as a starting point.
There is no capability in AWS (or CloudFormation) to schedule a start and stop of an instance.
However, you can code-up a simple solution using Amazon CloudWatch Events to trigger an AWS Lambda function on a schedule. You could even use Tags to identify when to start/stop instances.
For some examples, see: Simple EC2 Stopinator in Lambda - DEV Community

Schedule Auto Shutdown of resources in AWS

I am trying to schedule auto start or stop resources(EC2 Instances) for specific timings on AWS.
Is there an API to do the same ?
You can do this with the help of Lambda and CloudWatch.
Please refer: https://aws.amazon.com/premiumsupport/knowledge-center/start-stop-lambda-cloudwatch/
Hi If you want to put automation for start and stop a particular set of or a particular ec2 instance now this is possible using AWS System Manager you can do a lot more. I would strongly recommend to check out the official documentation it helped me sure it will help you as well AWS System Manager
You can also set up a Lambda function to start and stop EC2 AWS Setup a Lambda to Start and Stop EC2 Instance
Please let me know if you need some help.
We are using the instance scheduler stack. You can easily create a cloudformation stack then use tags to define which and when the instances need to stop and started.
Reference:
https://docs.aws.amazon.com/solutions/latest/instance-scheduler/architecture.html

Accessing files in EC2 from Lambda

I have few EC2 servers in AWS. Whenever the disk space exceeds a limit, i want to delete some files (may be logs folder) in EC2 instance automatically. I am planning to use Lambda and cloudwatch for this. Can i use Lambda to interact with EC2. If not possible, what is the alternate approach to achieve this functionality.
This is not an appropriate use-case for an AWS Lambda function.
AWS Lambda is suitable for tasks where compute is required in response to an event. Your use-case, however, is to manipulate information on an EC2 instance, which does not need cloud compute.
You could run a script on each each computer, triggered by a Scheduled Task.
Alternatively, you could use the Systems Manager Run Command (also known as the EC2 Run Command), which allows you to run commands on multiple Amazon EC2 instances and view the results. This could be used to trigger a local script, or it could pass the whole command to run (including the script). It is purpose-built for the type of task you describe.
AWS Lambda has access to your instances if they are available in the internet. If they are not available in the internet, it is possible to give access to AWS lambda using a NAT or instance Gateway in your VPC.
The problem is: access to your instance does not means access to the instances filesystems. To delete the files from Lambda you can use two alternatives:
Configure a network filesystem service in your instances an connect
to this services in your lambda function. Using windows you would
just "share" your disks, but in that case you would use some SMB
library in your lambda code, that "I think" did not have native SMB
support. Just keep in mind that your security guy will scream out
loud when you propose this alternative.
Create a "agent" in your EC2 instances and keep it running as a
Windows Service and call this agent from your lambda function. In
that case, the lambda will start the execution of the agent that
will be responsible for the file deletion.
Another option, is to follow Ramesh's suggestion and create a Powershell script and configure a cron job. To be easy, you can create a Image with this Powershell script and use the image to initialize each instance. The same solution would be applicable to "the agent" solution in the lambda alternantives.
I think that, in any case, you will need to change something in your 150 servers. Using a customized image can help you to simplify this a little bit, but you will not get a solution without some changes.
According to the following thread, you cannot access files inside a EC2 VM unless you are exposing files to the public using different methodology.
AWS Forum
Quoting from the forum
If you are talking about the underlying EC2 instance, answer is No, you cannot access those files.
However as a solution for your problem, you can used scheduled job to cleanup your files depending your usage. You can use a service or cron job.

start and stop EC2 instances based on a schedule

How can I start or stop EC2 instances based on a schedule (night, weekend)? are AWS EC2 API only solution for this?
AWS does not have any built-in method to schedule instances to start or stop. You have a few options:
Run your own EC2 instance that used cron or Task Manager to run your own scripts using the AWS SDK/CLI to start and stop the instances.
Use scheduled Auto Scaling to terminate and re-launch EC2 instances for you.
Use AWS Lambda to execute a Lambda function that starts and stops your instances, using the Lambda scheduler to schedule the execution of your Lambda function.
Use a third-party service that will start and stop your EC2 instances for you.
You can try my software, CloudRoboAWSScheduler. Download it from http://cloudrobo.net/download/
Please go through the setup details mentioned in http://cloudrobo.net/setup/
It is absolutely free.
Features:
- Easy to use GUI
- START / STOP EC2 Instances
- Reduce costs by starting and stopping your EC2 instances automatically
- Create dynamic schedules using ClouRobo AWS scheduler
- Manage multiple accounts in different AWS regions - Easy to use user interface
- Installs on Windows Server 2008/2012 or Windows 7/8/10
Note: This is a beta release.
-Viswanath