Skip to content

Instantly share code, notes, and snippets.

@em-shea
em-shea / s3-lambda.yaml
Created November 3, 2023 20:57
S3 Lambda example
Transform: AWS::Serverless-2016-10-31
Resources:
SourceBucket:
Type: AWS::S3::Bucket
DestinationBucket:
Type: AWS::S3::Bucket
Function:
Type: AWS::Serverless::Function
Properties:
CodeUri: src/Function
@em-shea
em-shea / sam-lambda.yaml
Created November 3, 2023 18:01
SAM Lambda function example
Transform: AWS::Serverless-2016-10-31
Resources:
Function:
Type: AWS::Serverless::Function # This resource is a Lambda function
Properties:
CodeUri: src/Function # Location in your file structure where the function code is saved
Handler: index.handler # Your Lambda function handler, or the method in your function code that's called every time your function is invoked
Runtime: python3.11
@em-shea
em-shea / get_words_for_list_id.py
Last active May 3, 2023 17:37
Get words for a given list ID function
# Given a list id, retrieve an array with word ids and simplified characters for that list
# This function is part of a state machine that generates pronunciation audio files for a given list
import list_word_service
def lambda_handler(event, context):
list_id = event['list_id']
last_word_token = event['last_word_token']
# Call to list_word_service, a shared service in the application
# for querying DynamoDB for words by list ID
@em-shea
em-shea / generate_audio_state_machine.yaml
Last active May 3, 2023 18:13
SAM definition for audio generation workflow
GenerateAudioStateMachine:
Type: AWS::Serverless::StateMachine
Properties:
Name: !Sub GenerateAudioStateMachine-${Stage}
DefinitionUri: src/generate_audio_state_machine.asl.json
DefinitionSubstitutions:
TableName: !Ref DynamoDBTable
GetWordsForListIdFunction: !Ref GetWordsForListId
PronunciationAudioBucket: !Ref PronunciationAudioBucket
Policies:
@em-shea
em-shea / audio-workflow-asl-definition.json
Created May 3, 2023 03:27
ASL definition for audio generation workflow
{
"Comment": "Generates and save audio files for a given list ID.",
"StartAt": "Get word list",
"States": {
"Get word list": {
"Type": "Task",
"Resource": "arn:aws:states:::lambda:invoke",
"OutputPath": "$.Payload",
"Parameters": {
"Payload": {
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An example deployment for migrating data between DynamoDB tables
Resources:
ExistingTable:
Type: AWS::DynamoDB::Table
DeletionPolicy: Retain
Properties:
TableName: ExportDataTable
@em-shea
em-shea / daily-vocab-app-table-design.json
Created January 29, 2022 14:57
Example of DynamoDB single table design for a daily vocabapp (NoSQL Workbench json)
{
"ModelName": "full-table-design",
"ModelMetadata": {
"Author": "",
"DateCreated": "Jun 04, 2021, 05:33 PM",
"DateLastModified": "Jun 16, 2021, 11:01 PM",
"Description": "",
"AWSService": "Amazon DynamoDB",
"Version": "3.0"
},
@em-shea
em-shea / import_dynamodb_data_script.py
Last active July 3, 2022 16:25
Example script to import DynamoDB table data
import json
import boto3
# Specify your existing (export) and new (import) table names and the region your tables are in
export_table_name = "ExportDataTable"
import_table_name = "ImportDataTable"
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table(import_table_name)
def import_table_data():
@em-shea
em-shea / export_dynamodb_data_script.py
Last active July 3, 2022 16:17
Example script to export DynamoDB table data
import json
import boto3
# Specify your table name and the region your table is in
export_table_name = "ExportDataTable"
dynamodb = boto3.resource('dynamodb', region_name='us-east-1')
table = dynamodb.Table(export_table_name)
def export_table_data():
print(f"Exporting data from {export_table_name}")
@em-shea
em-shea / example_user_service.py
Created January 16, 2022 19:25
An example of a user service querying DynamoDB and formatting the response
import os
import boto3
import datetime
from typing import List, Dict
from dataclasses import dataclass
from boto3.dynamodb.conditions import Key
@dataclass
class Subscription:
list_name: str