-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrakefile
92 lines (85 loc) · 2.7 KB
/
rakefile
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
require 'bundler/setup'
# require "bundler/gem_tasks"
require 'aws-sdk-dynamodb'
namespace :db do
task :setup do
# Create the database if it doesn't exist.
# Pull in the environment variables needed to create the database table
table_name = ENV['DB_TABLE_NAME'] #|| new_resource.environment['DB_TABLE_NAME']
aws_region = ENV['AWS_REGION'] #|| new_resource.environment['AWS_REGION']
aws_access_key_id = ENV['AWS_ACCESS_KEY_ID'] #|| new_resource.environment['AWS_ACCESS_KEY_ID']
aws_secret_access_key = ENV['AWS_SECRET_ACCESS_KEY'] #|| new_resource.environment['AWS_SECRET_ACCESS_KEY']
dynamo_db_client = Aws::DynamoDB::Client.new(
region: aws_region,
access_key_id: aws_access_key_id,
secret_access_key: aws_secret_access_key
)
puts 'Created DynamoDB client'
puts 'Checking for, and creating table:' + table_name
table_definition = {
attribute_definitions: [ # required
{
attribute_name: 'shortcode', # required
attribute_type: 'S', # required, accepts S, N, B
},
{
attribute_name: 'destination', # required
attribute_type: 'S', # required, accepts S, N, B
},
# {
# attribute_name: "ssl", # required
# attribute_type: "B", # required, accepts S, N, B
# },
# {
# attribute_name: "accessCount", # required
# attribute_type: "N", # required, accepts S, N, B
# },
],
table_name: table_name, # required
key_schema: [ # required
{
attribute_name: 'shortcode', # required
key_type: 'HASH', # required, accepts HASH, RANGE
}
],
global_secondary_indexes: [
{
index_name: 'destinationIndex', # required
key_schema: [ # required
{
attribute_name: 'destination', # required
key_type: 'HASH', # required, accepts HASH, RANGE
}
],
projection: { # required
projection_type: 'ALL', # accepts ALL, KEYS_ONLY, INCLUDE
},
provisioned_throughput: { # required
read_capacity_units: 1, # required
write_capacity_units: 1, # required
}
}
],
provisioned_throughput: { # required
read_capacity_units: 1, # required
write_capacity_units: 1, # required
},
stream_specification: {
stream_enabled: false
}
}
begin
dynamo_db_client.create_table(table_definition)
rescue Aws::DynamoDB::Errors::ResourceInUseException
puts 'Table Already Exists'
end
puts 'Checking Table'
begin
dynamo_db_client.describe_table(
table_name: table_definition[:table_name]
)
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
puts 'Table does not exist, check configuration.'
end
end
end