Skip to content

Instantly share code, notes, and snippets.

View translate_file.py
import os
import boto3
# Explicitly specifying where the default AWS region is found
# (as an environment variable) to be able to mock it in the test
s3_client = boto3.client('s3', region_name=os.environ['AWS_REGION'])
translate_client = boto3.client('translate', region_name=os.environ['AWS_REGION'])
def lambda_handler(event, context):
print(event)
View test_translate_function_4.py
# Mock S3 new file uploaded event
def s3_upload_event(self, file_name):
return {
"Records":[
{
"eventVersion":"2.1",
"eventSource":"aws:s3",
"awsRegion":"us-east-1",
"eventTime":"2021-06-18T16:03:17.567Z",
View test_translate_function_3.py
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,
View test_translate_file_2.py
# 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",
View test_translate_file_1.py
# Appending the src/ directory to the path
# in order to run tests on function code in a different directory
import sys
sys.path.append('../../')
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'}):
View IAM policy for example CICD app
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"iam:*",
"lambda:*",
"cloudformation:*",
"sns:CreateTopic",
@em-shea
em-shea / template.yaml
Last active May 12, 2020
An example SAM template for a Lambda function that's invoked by CloudWatch Events
View template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An example SAM template for a Lambda function that's invoked by a CloudWatch Event.
Resources:
MyFunction:
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: MyFunction
Handler: MyFunction.lambda_handler
@em-shea
em-shea / template.yaml
Created May 12, 2020
An example SAM template for a Lambda function invoked by API Gateway
View template.yaml
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
@em-shea
em-shea / template.yaml
Last active Jun 17, 2020
An example SAM template for a Lambda & API Gateway app that sends messages using SNS
View template.yaml
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 MyEmail=myemail@gmail.com
MyEmail:
Type: String
Resources:
@em-shea
em-shea / template.yaml
Last active Dec 2, 2020
An example SAM template that creates a DynamoDB table, a Lambda function that writes to DynamoDB, and a CloudWatch Event trigger
View template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: 'AWS::Serverless-2016-10-31'
Description: An app that includes a DynamoDB table, Lambda function that writes to DynamoDB, and CloudWatch Event trigger
Resources:
LambdaWriteToDynamoDB:
# A function that writes to a DynamoDB table on a schedule
Type: 'AWS::Serverless::Function'
Properties:
FunctionName: LambdaWriteToDynamoDB