Skip to content

Instantly share code, notes, and snippets.

@stympy
Created December 27, 2018 15:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save stympy/79e649d3ebc12773643b44415014d638 to your computer and use it in GitHub Desktop.
Save stympy/79e649d3ebc12773643b44415014d638 to your computer and use it in GitHub Desktop.
Get a list of things from DynamoDB and render them in Lambda using Ruby
require 'aws-sdk-dynamodb'
require 'erb'
DB = Aws::DynamoDB::Client.new
Views = Hash[Dir['views/*'].map {|v| [ File.basename(v, '.html.erb').to_sym, ERB.new(File.read(v)) ] }]
def list(event:, context:)
@rows = DB.scan({ table_name: ENV['TABLE_NAME'] }).result.items
{ headers: { 'Content-type' => 'text/html' }, statusCode: 200, body: render { Views[:table].result(binding) } }
end
def render
Views[:layout].result(binding)
end
<html>
<head>
<title>A list</title>
</head>
<body>
<%= yield %>
</body>
</html>
<h1>A List</h1>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<% @rows.each do |row| %>
<tr>
<td><%= row['name'] %></td>
</tr>
<% end %>
</tbody>
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment