{"id":862,"date":"2022-12-12T01:15:00","date_gmt":"2022-12-12T01:15:00","guid":{"rendered":"https:\/\/blog.ngocha.biz\/?p=862"},"modified":"2022-12-12T01:15:00","modified_gmt":"2022-12-12T01:15:00","slug":"use-aws-cli-create-ec2-instance","status":"publish","type":"post","link":"https:\/\/blog.ngocha.biz\/?p=862","title":{"rendered":"How to Use  AWS CLI to Create an EC2 instance [Step by Step]"},"content":{"rendered":"<p>This blog will teach you to use <strong>AWS CLI to create an EC2 instance<\/strong>. Also, we will look at all the possible options available in the CLI.<\/p>\n<h2 id=\"create-ec2-instance-using-aws-cli\">Create EC2 instance Using AWS CLI<\/h2>\n<p>To create an ec2 instance using CLI, you need the following.<\/p>\n<ol>\n<li>Security group ID<\/li>\n<li>Key pair name<\/li>\n<li>AMI Id<\/li>\n<li>Subnet ID<\/li>\n<\/ol>\n<p>We will create each resource using the CLI. If you have existing resources, you can directly use the respective resource IDs instead of creating them.<\/p>\n<h3 id=\"get-vpc-id-and-subnet-id\">Get VPC ID and Subnet ID<\/h3>\n<p>To create a security group, you need the following two IDs<\/p>\n<ol>\n<li><strong>VPC ID:<\/strong> To create a security group<\/li>\n<li><strong>One<\/strong> <strong>Subnet ID: <\/strong>To launch ec2 instance.<\/li>\n<\/ol>\n<p>You can get these details from the AWS Management console.<\/p>\n<p>Go to the VPC dashboard and click on the VPC. You will get the VPC ID, click on the subnets, and search with the VPC ID to list all the subnets associated with that VPC, as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/vpc-id.gif\" class=\"kg-image\" alt=\"get VPC and Subnet ID from AWS management console\" loading=\"lazy\" width=\"629\" height=\"434\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/vpc-id.gif 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/vpc-id.gif 629w\"><\/figure>\n<p>I&#8217;m going to use the following VPC and subnet IDs for this guide. You need to replace these IDs with your VPC and subnet ID.<\/p>\n<ol>\n<li><strong>VPC ID<\/strong>: <code>vpc-0d42bf2f27be967ff<\/code><\/li>\n<li><strong>Subnet ID<\/strong>: <code>subnet-00b5ede5e160caa59<\/code><\/li>\n<\/ol>\n<h3 id=\"get-ami-id\">Get AMI Id<\/h3>\n<p>Next, you need to get the AMI ID to be used with ec2 CLI.<\/p>\n<p>AMI ID could be a base image AMI Id or ID of a custom image created by you or your team.<\/p>\n<p>To get the AMI Id, Go to <code>ec2 Dashboard --&gt; AMI Catalog<\/code> and find the list of base images from AWS along with the AMI Id as shown below.<\/p>\n<figure class=\"kg-card kg-image-card\"><img decoding=\"async\" src=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-27-24.png\" class=\"kg-image\" alt=\"\" loading=\"lazy\" width=\"632\" height=\"509\" srcset=\"https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/size\/w600\/2025\/03\/image-27-24.png 600w, https:\/\/storage.ghost.io\/c\/5f\/2f\/5f2f4d20-2abf-4534-8d40-7aa233aedd43\/content\/images\/2025\/03\/image-27-24.png 632w\"><\/figure>\n<p>All the custom AMIs are present under <code>AMIs<\/code> option.<\/p>\n<p>I am going to use the AWS Ubuntu AMI id <code>ami-0d70546e43a941d70<\/code>.<\/p>\n<h3 id=\"create-security-group\">Create Security Group<\/h3>\n<p>Our next requirement is a security group ID to be attached to the ec2 instance. You can attach more than one security group.<\/p>\n<p>Either you can use the ID of an existing security group or you can create one using the following command. Replace <code>vpc-0d42bf2f27be967ff<\/code> with your VPC ID<\/p>\n<pre><code>aws ec2 create-security-group \\\n    --group-name demo-sg \\\n    --description \"AWS ec2 CLI Demo SG\" \\\n    --tag-specifications 'ResourceType=security-group,Tags=[{Key=Name,Value=demo-sg}]' \\\n    --vpc-id \"vpc-0d42bf2f27be967ff\"<\/code><\/pre>\n<p>Note down the <strong>security group ID from the output<\/strong>. The output would look like the following.<\/p>\n<pre><code>{\n    \"GroupId\": \"sg-07570e17ab8331f13\",\n    \"Tags\": [\n        {\n            \"Key\": \"Name\",\n            \"Value\": \"demo-sg\"\n        }\n    ]\n}<\/code><\/pre>\n<p>Refer <a href=\"https:\/\/awscli.amazonaws.com\/v2\/documentation\/api\/latest\/reference\/ec2\/create-security-group.html?ref=devopscube.com\" rel=\"noreferrer noopener\">create-security-group<\/a> official CLI reference for more details.<\/p>\n<p>Now, you need to <strong>add inbound (ingress) firewall rules to the security group<\/strong>. Replace <code>sg-07570e17ab8331f13<\/code> with your security group ID.<\/p>\n<pre><code>aws ec2 authorize-security-group-ingress \\\n    --group-id \"sg-07570e17ab8331f13\" \\\n    --protocol tcp \\\n    --port 22 \\\n    --cidr \"0.0.0.0\/0\" <\/code><\/pre>\n<p>If you want to add multiple ports and multiple CIDRs to the security group using the CLI, use the following command.<\/p>\n<pre><code>aws ec2 authorize-security-group-ingress \\\n    --group-id \"sg-07570e17ab8331f13\" \\\n    --tag-specifications 'ResourceType=security-group-rule,Tags=[{Key=Name,Value=demo-sg}]' \\\n    --ip-permissions \"IpProtocol=tcp,FromPort=22,ToPort=22,IpRanges=[{CidrIp=0.0.0.0\/0},{CidrIp=10.0.0.0\/24}]\" \\\n    --ip-permissions \"IpProtocol=tcp,FromPort=80,ToPort=80,IpRanges=[{CidrIp=0.0.0.0\/0},{CidrIp=10.0.0.0\/24}]\"<\/code><\/pre>\n<h3 id=\"create-ssh-key-pair\">Create SSH Key Pair<\/h3>\n<p>If you have an existing pem key, you can use it. If you don&#8217;t, you can <a href=\"https:\/\/devopscube.com\/generate-ssh-key-pair\/\">create an SSH key pair<\/a> using the following command. The output key gets stored in <code>~\/.ssh<\/code> location.<\/p>\n<pre><code>aws ec2 create-key-pair \\       \n   --key-name  wp-key-03 \\\n   --query 'KeyMaterial' --output text &gt; ~\/.ssh\/demo-key<\/code><\/pre>\n<p>Refer <a href=\"https:\/\/awscli.amazonaws.com\/v2\/documentation\/api\/latest\/reference\/ec2\/create-key-pair.html?ref=devopscube.com\" rel=\"noreferrer noopener\">ec2 create-key-pair<\/a> CLI reference for more details.<\/p>\n<h3 id=\"aws-cli-command-to-create-ec2\">AWS CLI Command to Create ec2<\/h3>\n<p>Now we have the following pre-defined values.<\/p>\n<ol>\n<li><strong>VPC ID<\/strong>: <code>vpc-0d42bf2f27be967ff<\/code><\/li>\n<li><strong>Subnet ID<\/strong>: <code>subnet-00b5ede5e160caa59<\/code><\/li>\n<li><strong>AMI ID<\/strong>: <code>ami-0d70546e43a941d70<\/code><\/li>\n<li><strong>Security Group ID:<\/strong> sg-063c02687e1103c7b<\/li>\n<li><strong>Key name:<\/strong> demo-key<\/li>\n<\/ol>\n<p>Here is an example AWS CLI command to create an ec2 instance.<\/p>\n<pre><code>aws ec2 run-instances \\\n    --image-id ami-0d70546e43a941d70 \\\n    --count 1 \\\n    --instance-type t2.micro \\\n    --key-name bibin-server \\\n    --security-group-ids sg-07570e17ab8331f13 \\\n    --subnet-id subnet-00b5ede5e160caa59 \\\n    --block-device-mappings \"[{\\\"DeviceName\\\":\\\"\/dev\/sdf\\\",\\\"Ebs\\\":{\\\"VolumeSize\\\":30,\\\"DeleteOnTermination\\\":false}}]\" \\\n    --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=demo-server}]' 'ResourceType=volume,Tags=[{Key=Name,Value=demo-server-disk}]'<\/code><\/pre>\n<p>Following are some of the important parameters you should know.<\/p>\n<p><!--kg-card-begin: html--><\/p>\n<table class=\"has-fixed-layout\">\n<thead>\n<tr>\n<th>Parameter<\/th>\n<th>Details<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td><code>--block-device-mappings<\/code><\/td>\n<td>To set the custom volume size&nbsp;for the root volume&nbsp;<code>\/dev\/sdf<\/code>. You can also add additional volumes using this parameter.<\/td>\n<\/tr>\n<tr>\n<td><code>--monitoring<\/code><\/td>\n<td>To enable detailed ec2 monitoring<\/td>\n<\/tr>\n<tr>\n<td><code>--ebs-optimized<\/code><\/td>\n<td>To optimize ec2 for EBS IO<\/td>\n<\/tr>\n<tr>\n<td><code>--iam-instance-profile<\/code><\/td>\n<td>To add instance profile (IAM role) to the instance.<\/td>\n<\/tr>\n<tr>\n<td><code>--launch-template<\/code><\/td>\n<td>To launch the ec2 instance from a launch template. <\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p><!--kg-card-end: html--><\/p>\n<p>Refer <a href=\"https:\/\/docs.aws.amazon.com\/cli\/latest\/reference\/ec2\/run-instances.html?ref=devopscube.com\" rel=\"noreferrer noopener\">ec2 run-instances<\/a> official CLI documentation for all the parameter references.<\/p>\n<h3 id=\"aws-cli-command-to-create-ec2-instance-with-user-data\">AWS CLI Command to Create ec2 Instance With User Data<\/h3>\n<p>With ec2 CLI, you can <strong>pass the <\/strong><a href=\"https:\/\/devopscube.com\/ec2-user-data\/\" rel=\"noreferrer\"><strong>ec2 user data<\/strong><\/a><strong> script<\/strong> using the <strong><code>--user-data<\/code><\/strong> flag.<\/p>\n<p>First, create a user data script file. For example,  a shell script named <strong><code>script.sh<\/code><\/strong><\/p>\n<pre><code>#!\/bin\/bash\napt-get update -y\nsudo systemctl nginx start\nchkconfig nginx on<\/code><\/pre>\n<p>Now, along with the ec2 create CLI command we have learned in the last section, just add the <code>--user-data<\/code> flag with the file path as highlighted below. You can provide the relative path or the absolute path of the script file based on your requirements.<\/p>\n<pre><code>aws ec2 run-instances \\\n    --image-id ami-0d70546e43a941d70 \\\n    --count 1 \\\n    --instance-type t2.micro \\\n    --key-name bibin-server \\\n    --security-group-ids sg-07570e17ab8331f13 \\\n    --subnet-id subnet-00b5ede5e160caa59 \\\n    --block-device-mappings \"[{\\\"DeviceName\\\":\\\"\/dev\/sdf\\\",\\\"Ebs\\\":{\\\"VolumeSize\\\":30,\\\"DeleteOnTermination\\\":false}}]\" \\\n    --tag-specifications 'ResourceType=instance,Tags=[{Key=Name,Value=demo-server}]' 'ResourceType=volume,Tags=[{Key=Name,Value=demo-server-disk}]' \\\n    --user-data file:\/\/path\/to\/script.sh<\/code><\/pre>\n<p>If it is a single-line command, you can pass it directly without an external file as shown below.<\/p>\n<pre><code>--user-data sudo systemctl nginx start<\/code><\/pre>\n<h2 id=\"conclusion\">Conclusion<\/h2>\n<p>In this step-by-step guide, we learned to use AWS CLI to create an ec2 instance.<\/p>\n<p>If you are a DevOps engineer working on the AWS cloud, learning to use AWS CLI to create AWS resources is essential. You can use it for automation as well as ad-hoc tasks.<\/p>\n<p>Also, if you are learning AWS, check out my <a href=\"https:\/\/devopscube.com\/become-devops-engineer\/\">comprehensive guide to becoming a DevOps engineer<\/a>, where I talk about what to learn on the AWS cloud.<\/p>\n<hr>\n<p><strong>Ngu\u1ed3n:<\/strong> <a href=\"https:\/\/devopscube.com\/use-aws-cli-create-ec2-instance\/\" target=\"_blank\" rel=\"noopener noreferrer\">How to Use  AWS CLI to Create an EC2 instance [Step by Step] \u2014 DevOpsCube<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Source: https:\/\/devopscube.com\/use-aws-cli-create-ec2-instance\/<\/p>\n","protected":false},"author":1,"featured_media":863,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-862","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-devops"],"_links":{"self":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/862","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=862"}],"version-history":[{"count":0,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/posts\/862\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=\/wp\/v2\/media\/863"}],"wp:attachment":[{"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=862"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=862"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.ngocha.biz\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=862"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}