Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

get-vpc-id-by-name #15

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open

get-vpc-id-by-name #15

wants to merge 2 commits into from

Conversation

fdavis
Copy link

@fdavis fdavis commented Jan 12, 2017

we have a lot of accounts and vpcs so its helpful to be able to grab a vpc-id for other configs/placement

@tchia04
Copy link
Contributor

tchia04 commented Jan 12, 2017

How about adding a get-vpc-name-by-id like this?

aws ec2 describe-vpcs --filter "Name=vpc-id,Values=${1}" --query 'Vpcs[*].Tags[].Value' --output text

@fdavis
Copy link
Author

fdavis commented Jan 13, 2017

Added, I like that it matches a partial vpc-id as well

Copy link
Contributor

@kyleknap kyleknap left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the PR! I like both aliases. I just had a feedback comment for both of the aliases.


get-vpc-id-by-name =
!f() {
aws ec2 describe-vpcs --filter "Name=tag:Name,Values=${1}" --query 'Vpcs[*].{id:VpcId}' --output text
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should be able to simplify your --query by just making it: --query 'Vpcs[].[VpcId] and should be the same result

Copy link

@virgilwashere virgilwashere Nov 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kyleknap the alias as submitted with curly braces, seems correct.

$ aws ec2 describe-vpcs --filter "Name=tag:Name,Values=vpc_name" --query 'Vpcs[].[id:VpcId]' --output text

Bad value for --query Vpcs[].[id:VpcId]: Expecting: comma, got: colon: Parse error at column 10, token ":" (COLON), for expression:
"Vpcs[].[id:VpcId]"
           ^
$ aws ec2 describe-vpcs --filter "Name=tag:Name,Values=vpc_name" --query 'Vpcs[].{id:VpcId}' --output text
vpc-01234567890123456

However, @fdavis you can also do this:

Suggested change
aws ec2 describe-vpcs --filter "Name=tag:Name,Values=${1}" --query 'Vpcs[*].{id:VpcId}' --output text
aws ec2 describe-vpcs --query "Vpcs[?Tags[?Key==\`Name\`]|[?Value==\`${1:?Need a VPC Name}\`]].VpcId" --output text


get-vpc-name-by-id =
!f() {
aws ec2 describe-vpcs --filter "Name=vpc-id,Values=${1}" --query 'Vpcs[*].Tags[].Value' --output text
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like we are including values of all tags. I think we should only be including the Name tag in what is returned.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would also recommend to include only the tag Name. +1 to include these 2 in the aliases.


get-vpc-name-by-id =
!f() {
aws ec2 describe-vpcs --filter "Name=vpc-id,Values=${1}" --query 'Vpcs[*].Tags[].Value' --output text
Copy link

@virgilwashere virgilwashere Nov 30, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
aws ec2 describe-vpcs --filter "Name=vpc-id,Values=${1}" --query 'Vpcs[*].Tags[].Value' --output text
aws ec2 describe-vpcs --vpc-ids "${1:?Need a VPCId}" --query 'Vpcs[].Tags[?Key==`Name`].Value' --output text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants