This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"fileLocations": [ | |
{ | |
"URIPrefixes": [ | |
"s3://my-bucket-name" | |
] | |
} | |
], | |
"globalUploadSettings": { | |
"format": "JSON" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
import json | |
import boto3 | |
from datetime import datetime | |
# Import resources using AWS Python SDK (boto3) and specify the DynamoDB table to scan and S3 bucket to write file to | |
# Table and bucket name are passed as environment variables in SAM template | |
s3 = boto3.resource('s3') | |
bucket = s3.Bucket(os.environ['BUCKET_NAME']) | |
table = boto3.resource('dynamodb').Table(os.environ['TABLE_NAME']) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: 'AWS::Serverless-2016-10-31' | |
Description: An app that includes a DynamoDB table, a Lambda function that writes to DynamoDB, and a scheduled EventBridge event | |
Resources: | |
LambdaWriteToDynamoDB: | |
# A function that writes to a DynamoDB table on a schedule | |
Type: 'AWS::Serverless::Function' | |
Properties: | |
FunctionName: LambdaWriteToDynamoDB |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: 'AWS::Serverless-2016-10-31' | |
Description: An example SAM template for a Lambda & API Gateway app that sends messages using SNS. | |
Parameters: | |
# For any variables you don't want stored in your repo, you can pass them through the SAM deploy command | |
# Ie, sam deploy .... --parameter-overrides [email protected] | |
MyEmail: | |
Type: String | |
Resources: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: 'AWS::Serverless-2016-10-31' | |
Description: An example SAM template for a Lambda function invoked by API Gateway. | |
Resources: | |
MyFunction: | |
Type: 'AWS::Serverless::Function' | |
Properties: | |
FunctionName: MyFunction | |
Handler: MyFunction.lambda_handler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
AWSTemplateFormatVersion: '2010-09-09' | |
Transform: 'AWS::Serverless-2016-10-31' | |
Resources: | |
Function: | |
Type: 'AWS::Serverless::Function' | |
Properties: | |
CodeUri: src/Function | |
Handler: handler.handler | |
Runtime: python3.11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"Version": "2012-10-17", | |
"Statement": [ | |
{ | |
"Effect": "Allow", | |
"Action": [ | |
"iam:*", | |
"lambda:*", | |
"cloudformation:*", | |
"sns:CreateTopic", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import unittest | |
from unittest import mock | |
# Setting the default AWS region environment variable required by the Python SDK boto3 | |
with mock.patch.dict('os.environ', {'AWS_REGION': 'us-east-1'}): | |
from translate_file.app import lambda_handler |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Mock call to S3 to read file | |
def mocked_read_file(bucket_name, key_name): | |
return "我爱写单元测试!" | |
# Mock call to Translate to translate file text | |
def mocked_translate_text(original_text): | |
return { | |
"original_text":"我爱写单元测试!", | |
"translated_text":"I love writing unit tests!", | |
"original_language":"zh", |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TranslateFileTest(unittest.TestCase): | |
# Test for valid file type (.txt) | |
@mock.patch('translate_file.app.read_file', side_effect=mocked_read_file) | |
@mock.patch('translate_file.app.translate_text', side_effect=mocked_translate_text) | |
def test_valid_file(self, translate_text_mock, read_file_mock): | |
response = lambda_handler(self.s3_upload_event("valid_file.txt"), "") | |
expected_response = { | |
"success": True, |
OlderNewer