Skip to content

Instantly share code, notes, and snippets.

View thanakijwanavit's full-sized avatar
🎯
Focusing

Nic Wanavit thanakijwanavit

🎯
Focusing
View GitHub Profile
@thanakijwanavit
thanakijwanavit / cloudwatchBasedTrigger.py
Created September 12, 2020 11:48
trigger a function periodically
UpdateS3Function:
Type: AWS::Serverless::Function
Properties:
Handler: app.saveToS3
Runtime: python3.8
Events:
UpdateDatabaseScheduledEvent:
Type: Schedule
Properties:
Schedule: rate(1 hour)
sam validate -t template.yaml
@thanakijwanavit
thanakijwanavit / vimCheatSheet.md
Last active September 12, 2020 13:36 — forked from alsibir/# Vim Cheatsheet
vim cheatsheet

Vim Cheatsheet

Disclaimer: This cheatsheet is summarized from personal experience and other online tutorials. It should not be considered as an official advice.

Global

:help keyword # open help for keyword
:o file       # open file
:saveas file  # save file as
:close        # close current pane
@thanakijwanavit
thanakijwanavit / samParameters.yaml
Last active September 15, 2020 00:37
set sam parameters, in the source template you need to add
# in the root stack
Resources:
WalletDatabase:
Type: AWS::Serverless::Application
Properties:
Parameters:
BRANCH: !Ref BRANCH
ROOTSTACKNAME: !Ref 'AWS::StackName'
@thanakijwanavit
thanakijwanavit / permissionTemplate.yaml
Last active September 18, 2020 00:47
add permission template to your sam config, for list of lambda function, look [here](https://tenxor.sh/permissionTemplate)
registerFunction:
Type: AWS::Serverless::Function
Properties:
CodeUri: member/
Handler: app.register
Runtime: python3.8
FunctionName: !Join [ "-" , [ !Ref 'AWS::StackName', "register"]]
Policies:
- LambdaInvokePolicy:
FunctionName: !Join [ "-", [ !Ref ROOTSTACKNAME, "get-member"]]
@thanakijwanavit
thanakijwanavit / uploadIotState.py
Created September 18, 2020 00:49
uploading iot state to an iot shadow in aws
iot = boto3.client(
'iot-data',
region_name='us-east-1',
aws_access_key_id = USER,
aws_secret_access_key = PW
)
iot.update_thing_shadow(
thingName = 'raspiHackathon',
payload= json.dumps(
@thanakijwanavit
thanakijwanavit / loadPassword.py
Last active September 22, 2020 08:39
load password and other credentials from file using pickle
import pickle
credLocation = 'drive/My Drive/.pynamodbDax'
user =''
pw = ''
if user and pw:
with open (credLocation , 'wb') as f:
pickle.dump({
'user': user,
'pw': pw
}, f)
@thanakijwanavit
thanakijwanavit / secretStore.py
Created September 18, 2020 16:27
load and save secrets using aws secrets manager
def get_secret(secretName = None, accessKey=None, secretKey=None, region = 'ap-southeast-1'):
secretManager = boto3.client(
service_name='secretsmanager',
region_name=region,
aws_access_key_id = accessKey,
aws_secret_access_key = secretKey
)
response = secretManager.get_secret_value(
SecretId=secretName
)
@thanakijwanavit
thanakijwanavit / putAccelerate.py
Last active November 27, 2020 06:13
put accelerate status on s3 using python
# create bucket
config = Config(s3={"use_accelerate_endpoint": True,
"addressing_style": "virtual"})
s3 = boto3.client(
's3',
aws_access_key_id= ACCESS_KEY_ID,
aws_secret_access_key= SECRET_ACCESS_KEY,
region_name = REGION,
config = config
)
@thanakijwanavit
thanakijwanavit / s3Helper.py
Created September 19, 2020 02:46
interact with s3 using accelerate endpoints and bzip compression
import requests, bz2, boto3
class S3:
@staticmethod
def s3():
'''
create and return s3 client
'''
config = Config(s3={"use_accelerate_endpoint": True,
"addressing_style": "virtual"})
s3 = boto3.client(