How to create an EC2 instance using AWS APIs? - amazon-web-services

Today, I started working on AWS. I want to create an EC2 instance by using API only. I want to do more actions like copying instance, creating bucket, transforming file from one instance to another etc.
I am not getting how to create the instance using API.
Can anyone assist me?

Here are some examples using the AWS CLI.
aws ec2 run-instances --image-id ami-xxxxxxxx --count 1 --instance-type t2.micro --key-name MyKeyPair --security-group-ids sg-xxxxxxxx --subnet-id subnet-xxxxxxxx

Here's a simple code to create an EC2 instance.
import boto3
client = boto3.client('ec2', region_name='us-east-1')
client.run_instances(ImageId='ami-123456', SubnetId='subnet-12345', InstanceType='t2.micro', MinCount=1, MaxCount=1, KeyName='my-keypair', SecurityGroupIds=['sg-123456'])
Modify the image id: ami-123456, subnet id: subnet-12345, keypair name: my-keypair and security group id: sg-123456. Save this file as run_instances.py (or any name) and execute the script as: python run_instances.py. If you have setup your credentials properly, the instance will be launched in us-east-1.
References:
Setting up environment variables: Environment Variables
Configure profiles: Profiles
Run instances boto3: boto3 run_instances()

Related

How do I enable the AWS CLI on an EC2 instance?

How do I enable the AWS CLI on an EC2 instance? After I create the EC2 instance, I can SSH into the machine, but when I try to do something like aws s3 ls, it prompts me to do aws configure first, which I then have to enter my keys. I want to be able to automate this so that I can grab additional artifacts from S3 buckets to install. Note that I am using the AWS CLI on my computer to create the EC2 instance, but I need to use the AWS CLI on the EC2 instance itself.
My AWS command to create a simple EC2 instance looks like the following (this is done on my computer).
aws ec2 run-instances \
--image-id ami-14c5486b \
--count 1 \
--instance-type t2.micro \
--key-name testkey \
--subnet-id subnet-xxxxxxxx \
--security-group-ids sg-xxxxxxxx \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=test}]'
--user-data file://install-software.sh
The install-software.sh looks something like the following (this is submitted to the EC2 instance).
#!/bin/bash
aws s3 cp s3://mybucket/some-archive.tar.gz some-archive.tar.gz
tar xf some-archive.tar.gz
sudo some-archive/bin/install.sh
You need to use an instance profile when launching your EC2 instance – if it has an instance profile attached then the AWS CLI will automatically use the permissions set in it to grant access to resources, rather than relying on your providing credentials.
You need to assign an instance role to your instance. Give it rights to get objects from your bucket. Then the aws cli will get the credentials from instance metadata automatically so you won't need to configure aws first.

AWS EC2: Add a tag when launching an instance using CLI

If you want to add a tag to an instance when launching, you have to perform two steps:
Launch an instance (run-instances)
Add a tag to the newly created instance (create-tags)
Is there a way to add a tag (or set a name) when launching an instance using a single CLI command?
This request had been pending for a long time and AWS finally supported this in March 2017.
See: Amazon EC2 and Amazon EBS add support for tagging resources upon creation and additional resource-level permissions
Make sure your AWS CLI version is at least 1.11.106
$ aws --version
aws-cli/1.11.109 Python/2.6.9 Linux/4.1.17-22.30.amzn1.x86_64 botocore/1.5.72
CLI to tag the instance when launching:
The following example applies a tag with a key of webserver and
value of production to the instance.
aws ec2 run-instances --image-id ami-abc12345 --count 1 --instance-type t2.micro
--key-name MyKeyPair --subnet-id subnet-6e7f829e
--tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]'
CLI to tag the instance and the volume:
The command also applies a tag with a key of cost-center and a value
of cc123 to any EBS volume that's created (in this case, the root
volume).
aws ec2 run-instances --image-id ami-abc12345 --count 1 --instance-type t2.micro
--key-name MyKeyPair --subnet-id subnet-6e7f829e
--tag-specifications 'ResourceType=instance,Tags=[{Key=webserver,Value=production}]' 'ResourceType=volume,Tags=[{Key=cost-center,Value=cc123}]'

Is it possible to launch one of my custom AWS AMI using CLI?

Basically, what I am looking for is the CLI version of http://docs.aws.amazon.com/AWSEC2/latest/UserGuide/launching-instance.html
Previously I have saved my instance as EBS AMI, and want to launch it using command line preferably, or APIs, instead of using Web Interface.
Thank you.
Here is an example of launching a new Amazon EC2 instance via the AWS Command-Line Interface (CLI) with a specified Amazon Machine Image (AMI):
aws ec2 run-instances --image-id ami-abcd1234 --security-group-id sg-abcd1234 --instance-type t1.micro
See: AWS CLI run-instances documentation

Assigning role when creating new EC2 instance through AWS CLI

I know role can be assigned to instance from the console but I can't find anything about how to do it through AWS CLI tool.
Use the --iam-instance-profile switch. For example:
$ aws ec2 run-instances --image-id ami-11aa22bb --iam-instance-profile Name="s3access-profile" --key-name my-key-pair --security-groups my-security-group --subnet-id subnet-1a2b3c4d
See Launching an Instance with an IAM Role Using the AWS CLI.

AWS spot price history coming empty

I got AWS CLI tools and account on AWS. I ran the command aws configure and set the AWS Access key, AWS secret key, region and output format. Now when I am running the command
aws ec2 describe-spot-price-history --instance-type m1.xlarge --product-description "Linux/Unix"
I get the output empty
{
"SpotPriceHistory": []
}
I have tried it with multiple instance types, giving start time and end time etc.
Please can someone help me on this.
Are you sure your account supports launching an ec2-classic instance ?
If you created your account after 2013-12-04, it supports EC2-VPC only and to retrieve the information you will need to specify that you want ec2 instances from VPC
aws ec2 describe-spot-price-history --product-description "Linux/UNIX (Amazon VPC)" --instance-type m1.xlarge