Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in AWS by (5.6k points)

I have created my VPC, Now how can I grep only 1 VPC-ID from a specific VPC by AWS EC2 describe-vpcs, so that VPC ID can be passed inside the script for the next step. I can see it from CLI or from the AWS console, for example:

aws ec2 describe-vpcs --vpc-ids |grep VpcId

            "VpcId": "vpc-00a0338c2f671a77c",

            "VpcId": "vpc-0b3697513d5987516",

            "VpcId": "vpc-061e25f5f78877798",

it gives me all of them

aws ec2 describe-vpcs --vpc-ids |grep -i ansible

                    "Value": "ANSIBLE_VPC",

But I need to get only VPC-ID for that specific VPC from the command.

1 Answer

0 votes
by (12.4k points)

If you have just issued a "create-vpc" command, then the VPC ID of that VPC would have been returned in response to that command:

Output:

{

    "Vpc": {

        "CidrBlock": "10.0.0.0/16",

        "DhcpOptionsId": "dopt-5EXAMPLE",

        "State": "pending",

        "VpcId": "vpc-0a60eb65b4EXAMPLE",    <-- This is the VPC ID

        "OwnerId": "123456789012",

        "InstanceTenancy": "default",

        "Ipv6CidrBlockAssociationSet": [],

        "CidrBlockAssociationSet": [

            {

                "AssociationId": "vpc-cidr-assoc-07501b79ecEXAMPLE",

                "CidrBlock": "10.0.0.0/16",

                "CidrBlockState": {

                    "State": "associated"

                }

            }

        ],

        "IsDefault": false,

        "Tags": []

    }

}

You could create the VPC and store its ID like this:

$ ID=`aws ec2 create-vpc --cidr-block 10.0.0.0/16 --query Vpc.VpcId --output text`

$ echo $ID

vpc-0fb4d08f9d6501e94

Instead, you are seeking the VPC ID for a VPC given its Name tag, you could use:

$ ID=`aws ec2 describe-vpcs --filter Name=tag:Name,Values=ANSIBLE_VPC --query Vpcs[].VpcId --output text`

$ echo $ID

vpc-0fb4d08f9d6501e94

Do you want to master AWS? Then do check out the AWS Certified Solutions Architect Professional training by Intellipaat.

Related questions

0 votes
1 answer
Want to get 50% Hike on your Salary?

Learn how we helped 50,000+ professionals like you !

0 votes
1 answer
0 votes
1 answer

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...