`
sillycat
  • 浏览: 2551906 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

AMAZON and serverless(1)Python 3.6

 
阅读更多
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



分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics