-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1b74875
commit 674410b
Showing
9 changed files
with
214 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[tool.pytest.ini_options] | ||
pythonpath = "src" |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
version = 0.1 | ||
[default.deploy.parameters] | ||
stack_name = "chapter07-stack" | ||
resolve_s3 = true | ||
s3_prefix = "chapter07-stack" | ||
region = "ap-northeast-1" | ||
capabilities = "CAPABILITY_IAM" | ||
image_repositories = [] |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import json | ||
import os | ||
|
||
import boto3 | ||
|
||
if os.environ['ENV'] == 'local': | ||
s3 = boto3.client('s3', endpoint_url='http://localhost.localstack.cloud:4566') | ||
elif os.environ['ENV'] == 'test': | ||
s3 = boto3.client('s3', endpoint_url='http://localhost:14566') | ||
|
||
|
||
def main(event): | ||
for record in event['Records']: | ||
print(record) | ||
body = json.loads(record['body']) | ||
s3.put_object( | ||
Bucket='chapter07-bucket', | ||
Key=f"chapter07/{body['id']}.json", | ||
Body=record['body'], | ||
) | ||
|
||
|
||
def lambda_handler(event, context): | ||
main(event) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
boto3==1.34.120 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import json | ||
import os | ||
import random | ||
|
||
import boto3 | ||
|
||
if os.environ['ENV'] == 'local': | ||
sqs = boto3.client('sqs', endpoint_url='http://localhost.localstack.cloud:4566') | ||
elif os.environ['ENV'] == 'test': | ||
sqs = boto3.client('sqs', endpoint_url='http://localhost:14566') | ||
|
||
|
||
def main(event): | ||
number = random.randint(0, 9999) | ||
sqs.send_message( | ||
QueueUrl='http://sqs.ap-northeast-1.localhost.localstack.cloud:4566/000000000000/chapter07-queue', | ||
MessageBody=json.dumps( | ||
{ | ||
'id': f'id{number:04}', | ||
'body': f'This is message {number:04}.', | ||
} | ||
), | ||
) | ||
|
||
return { | ||
'statusCode': 200, | ||
'body': json.dumps( | ||
{'id': f'id{number:04}'}, | ||
), | ||
} | ||
|
||
|
||
def lambda_handler(event, context): | ||
return main(event) |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
AWSTemplateFormatVersion: '2010-09-09' | ||
Transform: AWS::Serverless-2016-10-31 | ||
|
||
Resources: | ||
SenderFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
FunctionName: chapter07-sender-function | ||
CodeUri: ./src | ||
Handler: sender.lambda_handler | ||
Runtime: python3.12 | ||
Architectures: | ||
- x86_64 | ||
Environment: | ||
Variables: | ||
ENV: local | ||
Events: | ||
Api: | ||
Type: Api | ||
Properties: | ||
Method: POST | ||
Path: / | ||
Queue: | ||
Type: AWS::SQS::Queue | ||
Properties: | ||
QueueName: chapter07-queue | ||
ReceiveMessageWaitTimeSeconds: 20 | ||
ReceiverFunction: | ||
Type: AWS::Serverless::Function | ||
Properties: | ||
FunctionName: chapter07-receiver-function | ||
CodeUri: ./src | ||
Handler: receiver.lambda_handler | ||
Runtime: python3.12 | ||
Architectures: | ||
- x86_64 | ||
Environment: | ||
Variables: | ||
ENV: local | ||
Events: | ||
SqsEvent: | ||
Type: SQS | ||
Properties: | ||
Queue: !GetAtt Queue.Arn | ||
Bucket: | ||
Type: AWS::S3::Bucket | ||
Properties: | ||
BucketName: chapter07-bucket | ||
|
||
Outputs: | ||
ApiId: | ||
Value: !Ref ServerlessRestApi |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
localstack-utils==1.0.1 | ||
pytest==8.3.2 |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import json | ||
from http import HTTPStatus | ||
|
||
import pytest | ||
from localstack_utils.localstack import startup_localstack, stop_localstack | ||
from receiver import main, s3 | ||
|
||
|
||
@pytest.fixture(scope='module', autouse=True) | ||
def _setup(): | ||
startup_localstack(gateway_listen='0.0.0.0:14566') | ||
|
||
s3.create_bucket( | ||
Bucket='chapter07-bucket', | ||
CreateBucketConfiguration={'LocationConstraint': 'ap-northeast-1'}, | ||
) | ||
|
||
yield | ||
|
||
stop_localstack() | ||
|
||
|
||
def test_main(): | ||
event = { | ||
'Records': [ | ||
{ | ||
'body': json.dumps( | ||
{ | ||
'id': 'id0001', | ||
'body': 'This is message 0001.', | ||
} | ||
), | ||
} | ||
], | ||
} | ||
|
||
main(event) | ||
|
||
response = s3.head_object( | ||
Bucket='chapter07-bucket', | ||
Key='chapter07/id0001.json', | ||
) | ||
assert response['ResponseMetadata']['HTTPStatusCode'] == HTTPStatus.OK.value | ||
|
||
response = s3.get_object( | ||
Bucket='chapter07-bucket', | ||
Key='chapter07/id0001.json', | ||
) | ||
assert response['Body'].read().decode('utf-8') == '{"id": "id0001", "body": "This is message 0001."}' |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import json | ||
import re | ||
|
||
import pytest | ||
from localstack_utils.localstack import startup_localstack, stop_localstack | ||
from sender import main, sqs | ||
|
||
|
||
@pytest.fixture(scope='module', autouse=True) | ||
def _setup(): | ||
startup_localstack(gateway_listen='0.0.0.0:14566') | ||
|
||
sqs.create_queue( | ||
QueueName='chapter07-queue', | ||
Attributes={ | ||
'ReceiveMessageWaitTimeSeconds': '20', | ||
}, | ||
) | ||
|
||
yield | ||
|
||
stop_localstack() | ||
|
||
|
||
def test_main(): | ||
event = { | ||
'resource': '/', | ||
'path': '/', | ||
'httpMethod': 'POST', | ||
} | ||
|
||
main(event) | ||
|
||
response = sqs.receive_message( | ||
QueueUrl='http://sqs.ap-northeast-1.localhost.localstack.cloud:14566/000000000000/chapter07-queue', | ||
MaxNumberOfMessages=10, | ||
) | ||
|
||
assert 1 == len(response['Messages']) | ||
body = json.loads(response['Messages'][0]['Body']) | ||
assert 'id' in body | ||
assert re.match(r'id\d{4}', body['id']) |