1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

How to make your first VPC and EC2

Posted at

Hi there,

In this post, I’ll show you how to create your first VPC and EC2 instance using the AWS CLI. I hope it helps you better understand AWS service components.

Architecture

You can build the simple environment shown below using the following AWS CLI commands:

image.png

Prerequisites

If you haven’t installed the AWS CLI before, follow these steps:

Install AWS CLI:

    curl "https://awscli.amazonaws.com/awscli-exe-linux-x86_64.zip" -o "awscliv2.zip"
    unzip awscliv2.zip
    sudo ./aws/install

Verify the installation:

aws --version

Before running the configuration, make sure you have your AWS Access Key ID and Secret Access Key ready.

aws configure

Confirm your credentials are properly registered:

cat ~/.aws/credentials
cat ~/.aws/config

After completing these steps, you can start using the AWS CLI:

aws s3 ls --profile myprofile

Procedure

  1. Create a VPC
    It’s useful to capture the newly created VPC ID in a variable for later use.

    aws ec2 create-vpc \
        --cidr-block 10.10.0.0/23
    
    vpc_id="PUT_YOUR_OWN_VALUE"
    echo $vpc_id
    

  2. Create a Subnet

    aws ec2 create-subnet \
        --vpc-id $vpc_id \
        --cidr-block 10.10.0.0/24
    
    subnet_id="PUT_YOUR_OWN_VALUE"
    echo $subnet_id
    

  3. Create and Attach an Internet Gateway
    Create the gateway:

    aws ec2 create-internet-gateway
        
    igw_id="PUT_YOUR_OWN_VALUE"
    echo $igw_id
    

    Attach it to your VPC:

    aws ec2 attach-internet-gateway \
    	--internet-gateway-id $igw_id \
    	--vpc-id $vpc_id
    

  4. Create a Route Table and Configure Routing
    Create the route table:

    aws ec2 create-route-table --vpc-id $vpc_id
        
    rtb_id="PUT_YOUR_OWN_VALUE"
    echo $rtb_id
    

    Create a route:

    aws ec2 create-route --route-table-id $rtb_id \
        --destination-cidr-block 0.0.0.0/0 \
        --gateway-id $igw_id
    

    Associate the route table with the subnet:

    aws ec2 associate-route-table \
        --route-table-id $rtb_id \
        --subnet-id $subnet_id
    

  5. Modify the Subnet to Enable Auto-Assign Public IP

    aws ec2 modify-subnet-attribute \
        --subnet-id $subnet_id \
        --map-public-ip-on-launch
    

  6. Create a Security Group
    Create the group:

    aws ec2 create-security-group \
        --group-name "PUT_YOUR_OWN_VALUE" \
        --description "This is test security group" \
        --vpc-id $vpc_id
    
    sg_id="PUT_YOUR_OWN_VALUE"
    echo $sg_id
    

    Authorize inbound SSH (adjust CIDR as needed):

    aws ec2 authorize-security-group-ingress \
        --group-id $sg_id \
        --protocol tcp \
        --port 22 \
        --cidr <PUT_YOUR_ClientPC_IP>/32
    

  7. Create a Key Pair

    aws ec2 create-key-pair \
        --key-name <your_key_name> \
        --query 'KeyMaterial' \
        --output text > <your_key_name>.pem
    
    # change file permission
    chmod 400 HirakeGoma2.pem
    

  8. Launch an EC2 Instance
    This time, I’m using the Amazon Linux AMI, but you can replace the image ID with any AMI you prefer.

    aws ec2 run-instances \
        --image-id ami-09278528675a8d54e \
        --count 1 \
        --instance-type t3.micro \
        --key-name <your_key_name> \
        --security-group-ids $sg_id \
        --subnet-id $subnet_id
    
    instance_id="PUT_YOUR_OWN_VALUE"
    echo $instance_id
    

  9. Verify the EC2 Instance

    aws ec2 describe-instances --instance-id $instance_id
    

  10. Connect to Your Instance

    ssh -i "TestKeyPair.pem" ec2-user@<Instance's Public IP>
    

Thanks for reading! ;)

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?