Skip to content

Instantly share code, notes, and snippets.

@dreamnite
Created March 27, 2017 16:40
Show Gist options
  • Save dreamnite/64ebd59e3aabafccf298615c07ab991a to your computer and use it in GitHub Desktop.
Save dreamnite/64ebd59e3aabafccf298615c07ab991a to your computer and use it in GitHub Desktop.
find vpc by name
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
@dreamnite
Copy link
Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment