AWS – Launching an instance

Create a Home page in ASP.Net Core 3.1 MVC

There will be scenarios – usually when testing and developing your infrastructure code – where you need quick access to an instance. Creating it via the AWS CLI is the quickest and most consistent way to create one-off instances.

There are other recipes in this book that will require a running instance. This recipe will get you started.

Getting ready

For this recipe, you must have an existing key pair.

Here, we will be launching an instance of AWS Linux using an Amazon Machine Image (AMI) ID in the us-east-1 region. If you are working in a different region, you will need to update your image-id parameter, since AMIs are stored in each region with unique identifiers.

You must have configured your AWS CLI tool with working credentials.

How to do it…

Run the following AWS CLI command using your own key pair name. It creates an EC2 instance using a specific AMI and instance type, which, in this case, is the economical t2.micro:

      aws ec2 run-instances \
        --image-id ami-9be6f38c \
        --instance-type t2.micro \
        --key-name <your-key-pair-name>

How it works…

This example uses the command-line interface (CLI) instead of the web console. Choosing the CLI over the console is largely a matter of preference, but they both result in the same underlying API calls being made in your account to create resources.

While the key-name argument is optional, you will not be able to connect to your instance unless you have preconfigured some other way of logging in. When the instance is created, the public half of the key is written to ~/.ssh/known_hosts so that, when you use your SSH client with the private key, the instance recognizes you as an authorized user.

The t2.micro instance type that’s used in this recipe is included in the AWS free tier. You can run one micro instance per month for free during the first 12 months of your usage. See  https://aws.amazon.com/free for more information.

Since no Virtual Private Cloud (VPC) or security groups are specified, the instance will be launched in your account’s default VPC and security group. You can modify an instance’s security groups after it is launched without stopping it.

There’s more…

If you have created your own AMI, then you can change the image-id argument to quickly launch your specific AMI.

Take note of the InstanceId value in the response from the API, as you will need it for future commands.

See also

  • The Creating a key pair recipe
  • The Creating machine images recipe

Comments are closed.