Getty Images/iStockphoto
AWS administrators have several ways to launch an EC2 instance. Many admins use the AWS Command Line Interface for reliable, automated and consistent launches.
Users might choose the AWS CLI over the AWS Management Console because it is a scalable way to launch cloud resources. The AWS CLI also involves less manual work than the Console to create and initiate an EC2 instance. However, there is a learning curve going from the GUI-based AWS Console to commands.
In this step-by-step tutorial, discover how to use the AWS CLI to launch EC2 instances. This tutorial reviews the initial setup for the AWS CLI environment, the minimum parameters required to launch an instance and additional important parameters used in AWS deployments.
To start, install the CLI components. Refer to the AWS documentation for the latest commands, locations of packages and dependencies needed to install the AWS CLI in Linux, Apple macOS and Windows operating systems.
Next, users should configure appropriate permissions in the environment where they will execute CLI commands. For the AWS CLI, AWS credentials are stored in the $HOME/.aws/credentials file. Including AWS credentials in environment variables or in the command itself creates security vulnerabilities. Instead, configure those credentials in a local file. If running commands from an EC2 instance, configure an AWSIdentity and Access Management (IAM) instance profile to avoid internal credential configuration.
To follow this tutorial, the IAM user configured in the credentials file needs sufficient permissions to start EC2 instances and write permissions related to EC2. For example, the user must be able to create AWS security groups and Elastic Block Storage (EBS) volumes. Attach the AmazonEC2FullAccess IAM policy to the user to ensure they can complete the steps required.
The AWS Region is an essential parameter to configure in the CLI. Use the file $HOME/.aws/config. Specify a region by setting the AWS_REGION environment variable or passing the parameter –region in the AWS CLI.
You need the following parameters to launch an EC2 instance:
Use the AMI Catalog screen in the EC2 Console to select an available AMI for a particular region. Filter available AMIs by parameters such as OS, architecture, AWS Marketplace publisher, pricing model and private AMIs. Note any relevant AMI IDs that will be useful to work with the AWS CLI. This can help to avoid revisiting this page each time a user launches a new EC2 instance.
This tutorial assumes certain aspects of the EC2 configuration already exist. It does not cover how to create a new VPC or subnet, security group, or EC2 key pair.
To create the EC2 instance in the AWS CLI with the minimum recommended set of parameters, use the following commands:
aws ec2 run-instances \
--image-id <ami-id> \
--instance-type <instance-type> \
--subnet-id <subnet-id> \
--security-group-ids <security-group-id> <security-group-id> … \
--key-name <ec2-key-pair-name>
The CLI command that launches an EC2 instance has 40 different parameters. In addition to the bare minimum parameters, consider specifying the following to enhance security, tailor the EC2 instance to the application, increase reliability and achieve other goals:
Ernesto Marquez is the owner and project director at Concurrency Labs, where he helps startups launch and grow their applications on AWS. He enjoys building serverless architectures, building data analytics solutions, implementing automation and helping customers cut their AWS costs.
Editor's note: This article was updated to reflect changes in the best practices for using the CLI to create an EC2 instance.
Data center migrations can be complex. Follow this best practices guide to better understand the migration process and tools to ...
Meta and Google tout aggressive AI infrastructure investments focused on data center builds and power.
Due to rapid AI hardware advancement, companies release advanced products yearly to keep up with the competition. The new ...
Discover five nontechnical certifications that can advance your IT career by adding crucial business and leadership competencies ...
Infrastructure as code still anchors IT automation, but its primary users are now platform engineers, prompting ongoing shifts in...
Kubernetes cluster sprawl undermines efficiency and security. Implement governance, standardization and monitoring to balance ...
Compare Datadog vs. New Relic capabilities including alerts, log management, incident management and more. Learn which tool is ...
Many organizations struggle to manage their vast collection of AWS accounts, but Control Tower can help. The service automates ...
There are several important variables within the Amazon EKS pricing model. Dig into the numbers to ensure you deploy the service ...
The first major revision of the VMware platform under Broadcom's ownership is now available to all VMware Cloud Foundation and ...
Customers aren't rotating off VMware by Broadcom, despite gripes on pricing and the rise of enterprise virtualization ...
The companies have 'reached a settlement-in-principle,' but the telecom giant has indicated it's planning an exit from the ...