AMAZON and serverless(1)Python 3.6

299 篇文章 0 订阅
160 篇文章 0 订阅
AMAZON and serverless(1)Python 3.6

serverless framework
https://github.com/serverless/serverless

All examples
https://github.com/serverless/examples

Install via NPM
> npm install -g serverless

Upgrade npm
> npm i -g npm

Understand the serverless framework
https://github.com/serverless/serverless/blob/master/docs/providers/aws/guide/intro.md

Python DynamoDB sample
https://github.com/serverless/examples/tree/master/aws-python-rest-api-with-dynamodb
More information about serverless
https://serverless.com/framework/docs/providers/aws/guide/intro/

Download the Python Example
> serverless install -u https://github.com/serverless/examples/tree/master/aws-python-rest-api-with-dynamodb -n aws-python-rest-api-with-dynamodb

The codes is quite straightforward.
serverless.yaml will be my resources template, it generate the cloud formation during deploy

Here is how to run the tests
>pip install pytest-sugar
>pytest tests.py
https://pivotfinland.com/pytest-sugar/

Understand the Dynamodb Stream
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Streams.html

Understand how it mock
https://www.cnblogs.com/fnng/p/5648247.html

Show the logging in console
> py.test --capture=no tests.py

Or

> pytest --capture=no tests.py

Dynamodb Datatype
https://boto3.readthedocs.io/en/latest/reference/services/dynamodb.html#DynamoDB.Client.put_item

Here is how the mock work on tests.py

import json
import handler
from unittest.mock import patch

with open("event_sample.json") as example_event_buffer:
example_event = json.load(example_event_buffer)

def test_process_record():
with patch.object(handler, "notify") as patched_notify:
handler.process_record(example_event['Records'][0])
assert patched_notify.call_count == 0

handler.process_record(example_event['Records'][1])
assert patched_notify.call_count == 1

handler.process_record(example_event['Records'][2])
assert patched_notify.call_count == 2

handler.process_record(example_event['Records'][3])
assert patched_notify.call_count == 2

Install AWS command line tool on MAC
> sudo pip install awscli —upgrade —user

Check version
> aws --version
aws-cli/1.14.56 Python/3.6.4 Darwin/17.4.0 botocore/1.9.9

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Expressions.UpdateExpressions.html#Expressions.UpdateExpressions.SET.ModifyingAttributes

Tried this, but not working
> aws dynamodb put-item --table-name Monitoringservice__int__devicestate --item file:///Users/hluo/company/code/services.deviceprocess/item.json

Command with the region
> aws dynamodb list-tables --region=us-west-1

This will work
> aws dynamodb put-item --table-name Monitoringservice__int__devicestate --item file:///Users/hluo/company/code/services.deviceprocess/item.json --region=us-west-1

Update Command
> aws dynamodb update-item --table-name Monitoringservice__int__devicestate --key '{ "deviceKey" : { "S" : "icon:KQ6A290A0F1842"}}' --update-expression "SET process = :process" --expression-attribute-values file:///Users/hluo/company/code/services.deviceprocess/update.json --return-values ALL_NEW —region=us-west-1


References:
https://aws.amazon.com/cn/sdk-for-python/
https://aws.amazon.com/cloudformation/
http://www.cnblogs.com/huang0925/p/3384596.html
https://aws.amazon.com/cn/documentation/cloudformation/
https://aws.amazon.com/cn/cloudformation/
https://github.com/serverless/examples/tree/master/aws-python-rest-api-with-dynamodb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值