Class: TerraspacePluginAws::Interfaces::Backend::Table

Inherits:
Base
  • Object
show all
Defined in:
lib/terraspace_plugin_aws/interfaces/backend/table.rb

Instance Method Summary collapse

Methods inherited from Base

#initialize

Methods included from Logging

#logger

Methods included from Clients

#dynamodb, #s3, #secretsmanager, #ssm, #sts

Constructor Details

This class inherits a constructor from TerraspacePluginAws::Interfaces::Backend::Base

Instance Method Details

#createObject



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 3

def create
  table = @info["dynamodb_table"]
  return unless table # not table provided

  if exist?(table)
    logger.debug "Table already exist: #{table}"
    c = TerraspacePluginAws::Interfaces::Config.instance.config
    tag_existing(table) if c.tag_existing
  else
    logger.info "Creating dynamodb table: #{table}"
    create_table(table)
  end
end

#create_table(name) ⇒ Object



17
18
19
20
21
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 17

def create_table(name)
  dynamodb.create_table(table_definition(name))
  logger.info "Waiting for dynamodb table to finish creating..."
  dynamodb.wait_until(:table_exists, table_name: name)
end

#exist?(name) ⇒ Boolean

Returns:

  • (Boolean)


93
94
95
96
97
98
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 93

def exist?(name)
  dynamodb.describe_table(table_name: name)
  true  # table exist
rescue Aws::DynamoDB::Errors::ResourceNotFoundException
  false # table does not exist
end

#secure(definition) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 45

def secure(definition)
  c = TerraspacePluginAws::Interfaces::Config.instance.config.dynamodb
  return unless c.encryption

  # Docs: https://docs.aws.amazon.com/amazondynamodb/latest/APIReference/API_SSESpecification.html
  #
  # If enabled (true), server-side encryption type is set to KMS and an AWS managed CMK is used (AWS KMS charges apply).
  # IE: If we want to use the "AWS Managed KMS Key", set `enable=true` and dont set any other values.
  #
  #     Setting                                             | Console
  #     --------------------------------------------------- | ---------------------------------------------------
  #     enable=false                                        | DEFAULT - The key is owned by Amazon DynamoDB. You are not charged any fee for using these CMKs.
  #     enable=true without sse_type, kms_master_key_id set | KMS - Customer managed CMK - The key is stored in your account that you create, own, and manage. AWS Key Management Service (KMS) charges apply
  #     enable=true with sse_type, kms_master_key_id set    | The key is stored in your account that you create, own, and manage. AWS Key Management Service (KMS) charges apply - The key is stored in your account that you create, own, and manage. AWS Key Management Service (KMS) charges apply
  #
  spec = {enabled: true}
  if c.kms_master_key_id
    spec[:sse_type]          = c.sse_type # accepts AES256, KMS
    spec[:kms_master_key_id] = c.kms_master_key_id
  end

  definition[:sse_specification] = spec
  definition
end

#table_definition(name) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 23

def table_definition(name)
  definition = {
    attribute_definitions: [
      {
        attribute_name: "LockID",
        attribute_type: "S",
      }
    ],
    key_schema: [
      {
        attribute_name: "LockID",
        key_type: "HASH",
      }
    ],
    billing_mode: "PAY_PER_REQUEST", # accepts PROVISIONED, PAY_PER_REQUEST
    table_name: name,
  }
  secure(definition)
  tag(definition)
  definition
end

#tag(definition) ⇒ Object



70
71
72
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 70

def tag(definition)
  definition[:tags] = tags unless tags.empty?
end

#tag_existing(table_name) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 74

def tag_existing(table_name)
  return if tags.empty?
  resp = dynamodb.describe_table(table_name: table_name)
  # Always appends tags, wont remove tags.
  dynamodb.tag_resource(
    resource_arn: resp.table.table_arn,
    tags: tags
  )
end

#tagsObject



84
85
86
87
88
89
90
91
# File 'lib/terraspace_plugin_aws/interfaces/backend/table.rb', line 84

def tags
  c = TerraspacePluginAws::Interfaces::Config.instance.config
  tags = !c.dynamodb.tags.empty? ? c.dynamodb.tags : c.tags
  # Note there is no map! method for Hash
  tags = tags.map do |k,v|
    {key: k.to_s, value: v}
  end
end