Controlling multiple amazon EC2 instances - amazon-ec2

Which class/method to use to start/stop a List of amazon EC2 instances with java API? I only know the List<Instance>

startInstances takes a StartInstancesRequest, which has a method .setInstanceIds that allows you to attach a list of instance IDs.
http://docs.amazonwebservices.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/AmazonEC2.html#startInstances%28com.amazonaws.services.ec2.model.StartInstancesRequest%29

Related

How to create a bash script for autoscaling EC2 instances given the work volume of a SQS?

I created a bash script with aws-cli that sends 1000 messages using SQS, now I want to create another one that runs in parallel and creates and destroys EC2 instances given this condition:
Checks every 15 seconds: if (((ApproximateNumberOfMessages + 9)/10) - N running instances) > 0 creates an instance, else destroys an instance.
My first problem is that I don't know how to connect my SQS queue to a EC2 instance so it can process these messages. I tried following this tutorial: https://docs.aws.amazon.com/AWSSimpleQueueService/latest/SQSDeveloperGuide/sqs-sending-messages-from-vpc.html, but I don't want to use a private VPC and security groups so I was wondering if there is a way to make it easier.
My questions are: Is it possible to do it just using a bash script instead of CloudWatch and Autoscaling Groups? How do I create a EC2 instance that is ready to process these messages?
When you create an EC2 instance, it automatically gets an Elastic Network Interface (ENI, a virtual network card) for which AWS automatically assigns either a default security group, either another user created security group. You can not detach the default ENI, also you cannot have an ENI without a security group. Moreover, EC2 instances have to run inside a VPC, which can be private of public. Nevertheless, if you work with EC2 instances, you have to deal with security groups as well.
Is it possible to do it just using a bash script instead of CloudWatch and Autoscaling Groups?
It might be possible, but you will find yourself reinventing the wheel. Autoscaling does more than just adding/removing instances based on some condition. For example, it also makes sure that your instances are replaced if they become unhealthy or if they are terminated for some reason. For more info see AWS ASG FAQ.
How do I create a EC2 instance that is ready to process these messages?
You can't just start an instance and expect to process your messages. You need to have some code or some kind software deployed to it and configured to poll messages from your queues.

To launch EC2 instances based on available AMIs via CloudFormation template

I have a requirement to launch multiple EC2 instances in the Tokyo region, based on the number of AMIs owned by our account in that same region. The AMIs are backed-up daily from another region.
What this CloudFormation needs to achieve is:
Retrieve a list of AMIs created today
Attempt to launch each of them in the same region
For example, if today there are 10 different AMIs created in the Tokyo region, then CloudFormation will then create 10 EC2 instances based on these 10 AMIs.
I have looked at some examples at Walkthrough: Looking Up Amazon Machine Image IDs - AWS CloudFormation but found the code does not suit the requirement.
I already have the Lambda function retrieve-today-ami.py, the challenge is to include them in the CF template found in Walkthrough: Looking Up Amazon Machine Image IDs - AWS CloudFormation
Normally, CloudFormation is used to launch pre-defined infrastructure. Your requirement to launch a variable number of instances with information that changes for each instance every day, does not match the model for using CloudFormation.
Based on your use-case, I would recommend writing a script to perform the operation you want.
For example, a Python scripts that lists the AMIs, identifies the ones you want to use, then launches EC2 instances using those AMIs.
You might be able to achieve this by using a Lambda-backed custom resource to fetch the names of the AMIs. Then, the outputs of your custom resource could be used in the EC2 stanzas in the template. You could have the one template defining the Lambda export the values and import them on your EC2 templates.

How to update code on multiple EC2 instances?

I created an Elastic Load Balancer in front of two EC2 instances. However, I discovered an issue that requires me to update the code on both EC2 instances.
I can access each instance individually to update code via github, or I could create an AMI to launch a new instance. It's very unfavorable.
How can I synchronize code between the two EC2 instances?
In situations like this either a code pipeline would be helpful OR better yet switch to Elastic Beanstalk.

Create EC2 instance from another instance via command line or dynamically

I want to use power of cloud, where master or main ec2 instance is creating multiple instances based on need and then destroying them.
need to Create multiple instance from same AMI.
I want to know best way to accomplish this.
Thanks
You can utilize EC2 APIs for this purpose.
ec2-run-instances (http://docs.aws.amazon.com/AWSEC2/latest/CommandLineReference/ApiReference-cmd-RunInstances.html) is a command that allows you to create a new instance from your own (or public) AMI. You can also specify the number of instances you wish to create.
There are also Web Service operation (RunInstances) for this purpose:
http://docs.aws.amazon.com/AWSEC2/latest/APIReference/ApiReference-query-RunInstances.html
Which one to use is up to you. However, I don't think starting a new instance from a master instance is a good practice in AWS. You can rely on Elastic Load Balancing (http://aws.amazon.com/elasticloadbalancing/) and Auto Scaling (http://aws.amazon.com/autoscaling/) to scale up/down your server fleet depending on incoming traffic or healthiness of your running instances.

AWS API action to list EC2 Instance associated with a specific Elastic Load Balancer?

I have looked through the AWS EC2 and ELB apis and I cannot figure out a way that I can list the EC2 Instances that are associated with an Elastic Load Balancer. I would think that there is a way to do this, since it is so easy to do on the console. Any ideas?
This is indeed possible via the Elastic Load Balancing API as expected:
The DescribeLoadBalancers action returns a list of LoadBalancerDescriptions, where each LoadBalancerDescription contains an Instances element in turn, which provides the desired list of EC2 instance IDs for the LoadBalancer.

Resources