-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathec2-ruby-example-create-vpc.rb
96 lines (85 loc) · 2.79 KB
/
ec2-ruby-example-create-vpc.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
# SPDX-License-Identifier: Apache-2.0
# Purpose:
# ec2-ruby-example-create-vpc.rb demonstrates how to create a virtual private cloud
# (VPC) in Amazon Virtual Private Cloud (Amazon VPC) and then tags the VPC.
# snippet-start:[ec2.Ruby.createVpc]
require "aws-sdk-ec2"
# Creates a virtual private cloud (VPC) in
# Amazon Virtual Private Cloud (Amazon VPC) and then tags
# the VPC.
#
# @param ec2_resource [Aws::EC2::Resource] An initialized
# Amazon Elastic Compute Cloud (Amazon EC2) resource object.
# @param cidr_block [String] The IPv4 CIDR block for the subnet.
# @param tag_key [String] The key portion of the tag for the VPC.
# @param tag_value [String] The value portion of the tag for the VPC.
# @return [Boolean] true if the VPC was created and tagged;
# otherwise, false.
# @example
# exit 1 unless vpc_created_and_tagged?(
# Aws::EC2::Resource.new(region: 'us-west-2'),
# '10.0.0.0/24',
# 'my-key',
# 'my-value'
# )
def vpc_created_and_tagged?(
ec2_resource,
cidr_block,
tag_key,
tag_value
)
vpc = ec2_resource.create_vpc(cidr_block: cidr_block)
# Create a public DNS by enabling DNS support and DNS hostnames.
vpc.modify_attribute(enable_dns_support: { value: true })
vpc.modify_attribute(enable_dns_hostnames: { value: true })
vpc.create_tags(tags: [{ key: tag_key, value: tag_value }])
puts "Created VPC with ID '#{vpc.id}' and tagged with key " \
"'#{tag_key}' and value '#{tag_value}'."
return true
rescue StandardError => e
puts "#{e.message}"
return false
end
# Example usage:
def run_me
cidr_block = ""
tag_key = ""
tag_value = ""
region = ""
# Print usage information and then stop.
if ARGV[0] == "--help" || ARGV[0] == "-h"
puts "Usage: ruby ec2-ruby-example-create-vpc.rb " \
"CIDR_BLOCK TAG_KEY TAG_VALUE REGION"
# Replace us-west-2 with the AWS Region you're using for Amazon EC2.
puts "Example: ruby ec2-ruby-example-create-vpc.rb " \
"10.0.0.0/24 my-key my-value us-west-2"
exit 1
# If no values are specified at the command prompt, use these default values.
elsif ARGV.count.zero?
cidr_block = "10.0.0.0/24"
tag_key = "my-key"
tag_value = "my-value"
# Replace us-west-2 with the AWS Region you're using for Amazon EC2.
region = "us-west-2"
# Otherwise, use the values as specified at the command prompt.
else
cidr_block = ARGV[0]
tag_key = ARGV[1]
tag_value = ARGV[2]
region = ARGV[3]
end
ec2_resource = Aws::EC2::Resource.new(region: region)
if vpc_created_and_tagged?(
ec2_resource,
cidr_block,
tag_key,
tag_value
)
puts "VPC created and tagged."
else
puts "VPC not created or not tagged."
end
end
run_me if $PROGRAM_NAME == __FILE__
# snippet-end:[ec2.Ruby.createVpc]