Bootstrap FreeKB - Amazon Web Services (AWS) - Create an EC2 instance using the AWS CLI
Amazon Web Services (AWS) - Create an EC2 instance using the AWS CLI


This assumes you have already configured the aws command line tool. If not, check out my article on Getting Started with the AWS CLI.

At a high level, there are 3 types of EC2 instances

To create an EC2 instance, you will need the following.

  • Amazon Machine Image (AMI) ID
  • EC2 Key Pair 
  • EC2 Security Group
  • Virtual Private Cloud (VPC) Subnet ID

Your AWS account will be in a certain region, such as us-east-1 (eastern USA) or eu-west-1 (western Europe) or ap-east-1 (eastern Asia). You typically want your AWS account to be in the region where most of your customers reside.

In this article, the instance is created by providing things like the Amazon Machine Image (AMI) ID, the EC2 Key Pair, the Security Group and VPC as command line options. Check out my article Create an EC2 instance using a Launch Template as a way to create an EC2 instance with default settings so that you don't have to provide all of these options on the command line.

For example, list Amazon Machine Images (AMI) using the AWS CLI. In this example --filters is used to get the image ID of an Amazon Linux 2024 images.

~]$ aws ec2 describe-images --filters "Name=name,Values=amzn2-ami-kernel-*2024*-x86_64-*" | egrep 'Description|ImageId'
            "Description": "Amazon Linux 2 Kernel 5.10 AMI 2.0.20240124.0 x86_64 HVM gp2",
            "ImageId": "ami-008677ef1baf82eaf",
            "Description": "Amazon Linux 2 Kernel 5.10 AMI 2.0.20240131.0 x86_64 HVM gp2",
            "ImageId": "ami-0cf10cdf9fcd62d37",
            "Description": "Amazon Linux 2 Kernel 5.10 AMI 2.0.20240109.0 x86_64 HVM ebs",
            "ImageId": "ami-0ce402dd4b145b502",
            "Description": "Amazon Linux 2 Kernel 5.10 AMI 2.0.20240131.0 x86_64 HVM ebs",
            "ImageId": "ami-0a62cf63915a42304",
            "Description": "Amazon Linux 2 Kernel 5.10 AMI 2.0.20240124.0 x86_64 HVM ebs",
            "ImageId": "ami-0f09eac1988eae1dd",
            "Description": "Amazon Linux 2 Kernel 5.10 AMI 2.0.20240109.0 x86_64 HVM gp2",
            "ImageId": "ami-0c0b74d29acd0cd97",

 

To connect to a Linux EC2 instance over SSH using Putty, you will need to have already created an SSH key for which you have a PPK file that maps to the SSH key. Check out my article on creating EC2 key pair using the AWS CLI for more details on this.

~]$ aws ec2 describe-key-pairs | grep -i KeyName
            "KeyName": "default",

 

To connect to a Linux EC2 instance over SSH, the Security Group associated wtih the EC2 instance will need to allow incoming connections on SSH port 22. Check out my article on creating a Security Group using the AWS CLI or modifying a Security Group using the AWS CLI.

~]$ aws ec2 describe-security-groups | grep -i GroupId
            "GroupId": "sg-0778124087b3d14d4",

 

List Virtual Private Cloud (VPC) Subnets using the AWS CLI.

~]$ aws ec2 describe-subnets | grep -i SubnetId
            "SubnetId": "subnet-03f11123480f6abcd",

 

With the prior output in hand, you can then create an EC2 instance using the aws ec2 run-instances command.

aws ec2 run-instances 
--image-id ami-0b0dcb5067f052a63 \
--count 1 \
--key-name default \
--security-group-ids sg-0778124087b3d14d4 \
--subnet-id subnet-03f11123480f6abcd

 

Additionally, I often also incude the --instance-type and --tag-specifications to give my instance a Name.

aws ec2 run-instances 
--image-id ami-0b0dcb5067f052a63 \
--count 1 \
--key-name default \
--security-group-ids sg-0778124087b3d14d4 \
--subnet-id subnet-03f11123480f6abcd \
--instance-type t2.micro \
--associate-public-ip-address \
--tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=my-instance}]'

 

Something like this should be returned.

{
    "Groups": [],
    "Instances": [
        {
            "AmiLaunchIndex": 0,
            "ImageId": "ami-0b0dcb5067f052a63",
            "InstanceId": "i-03e9bfe721096bf13",
            "InstanceType": "m1.small",
            "KeyName": "default",
            "LaunchTime": "2023-04-12T01:49:06+00:00",
            "Monitoring": {
                "State": "disabled"
            },
            "Placement": {
                "AvailabilityZone": "us-east-1a",
                "GroupName": "",
                "Tenancy": "default"
            },
            "PrivateDnsName": "ip-172-31-12-195.ec2.internal",
            "PrivateIpAddress": "172.31.12.195",
            "ProductCodes": [],
            "PublicDnsName": "",
            "State": {
                "Code": 0,
                "Name": "pending"
            },
            "StateTransitionReason": "",
            "SubnetId": "subnet-0f35c3586e5090314",
            "VpcId": "vpc-014d2fcfa335d3c01",
            "Architecture": "x86_64",
            "BlockDeviceMappings": [],
            "ClientToken": "018537d7-ec7e-4f12-b3fa-4422c26a87a0",
            "EbsOptimized": false,
            "EnaSupport": true,
            "Hypervisor": "xen",
            "NetworkInterfaces": [
                {
                    "Attachment": {
                        "AttachTime": "2023-04-12T01:49:06+00:00",
                        "AttachmentId": "eni-attach-0afdfecefe8100bab",
                        "DeleteOnTermination": true,
                        "DeviceIndex": 0,
                        "Status": "attaching",
                        "NetworkCardIndex": 0
                    },
                    "Description": "",
                    "Groups": [
                        {
                            "GroupName": "default",
                            "GroupId": "sg-0c3296b3cd153fdc1"
                        }
                    ],
                    "Ipv6Addresses": [],
                    "MacAddress": "02:3c:39:fb:ee:93",
                    "NetworkInterfaceId": "eni-00cefd6048896758e",
                    "OwnerId": "123456789012",
                    "PrivateDnsName": "ip-172-31-12-195.ec2.internal",
                    "PrivateIpAddress": "172.31.12.195",
                    "PrivateIpAddresses": [
                        {
                            "Primary": true,
                            "PrivateDnsName": "ip-172-31-12-195.ec2.internal",
                            "PrivateIpAddress": "172.31.12.195"
                        }
                    ],
                    "SourceDestCheck": true,
                    "Status": "in-use",
                    "SubnetId": "subnet-0f35c3586e5090314",
                    "VpcId": "vpc-014d2fcfa335d3c01",
                    "InterfaceType": "interface"
                }
            ],
            "RootDeviceName": "/dev/xvda",
            "RootDeviceType": "ebs",
            "SecurityGroups": [
                {
                    "GroupName": "default",
                    "GroupId": "sg-0c3296b3cd153fdc1"
                }
            ],
            "SourceDestCheck": true,
            "StateReason": {
                "Code": "pending",
                "Message": "pending"
            },
            "VirtualizationType": "hvm",
            "CpuOptions": {
                "CoreCount": 1,
                "ThreadsPerCore": 1
            },
            "CapacityReservationSpecification": {
                "CapacityReservationPreference": "open"
            },
            "MetadataOptions": {
                "State": "pending",
                "HttpTokens": "optional",
                "HttpPutResponseHopLimit": 1,
                "HttpEndpoint": "enabled",
                "HttpProtocolIpv6": "disabled",
                "InstanceMetadataTags": "disabled"
            },
            "EnclaveOptions": {
                "Enabled": false
            },
            "PrivateDnsNameOptions": {
                "HostnameType": "ip-name",
                "EnableResourceNameDnsARecord": false,
                "EnableResourceNameDnsAAAARecord": false
            }
        }
    ],
    "OwnerId": "123456789012",
    "ReservationId": "r-04c0b43a7f568dfc5"
}

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 041256 in the box below so that we can be sure you are a human.