Created
March 27, 2017 16:40
-
-
Save dreamnite/64ebd59e3aabafccf298615c07ab991a to your computer and use it in GitHub Desktop.
find vpc by name
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require 'aws-sdk' | |
def lookup_vpc_by_name(vpc_name) | |
client=Aws::EC2::Client.new # Uses your local credentials or default iam role. Modify as needed if you get creds from a different source. | |
resp = client.describe_vpcs({ filters: [{ name: "tag:Name", values: [ vpc_name ]}]}) | |
if resp['vpcs'].empty? | |
raise "No vpc by #{vpc_name} found" | |
elsif resp['vpcs'].length > 1 | |
raise 'received too many responses, should be 1.' | |
else | |
resp['vpcs'].first['vpc_id'] | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Use this by getting the id for a name, storing it in a local variable, and then referencing that vpc-id with any vpc resources you use with chef-provisioning.