Skip to content

Instantly share code, notes, and snippets.

@smd877
smd877 / controlec2.py
Created October 1, 2018 05:07
AWS Lambda ControlEC2 Code
import boto3
def lambda_handler(event, context):
command = event.get('command')
if command in ['start', 'stop', 'status']:
try:
instance = boto3.resource('ec2').Instance(event.get('target'))
state = instance.state['Name']
if command == 'start' and state == 'stopped':
instance.start()
@smd877
smd877 / sendmail.py
Created November 7, 2018 13:36
AWS LambdaでGmailの送信
#coding: UTF-8
import os
import smtplib
from email.mime.text import MIMEText
# Gmailアカウント
FROM_ADDR = os.environ['mail_from_addr']
# アプリパスワード
PASSWD = os.environ['mail_passwd']
# 送信先メールアドレス
@smd877
smd877 / pushFcm.py
Last active July 2, 2019 06:34
FCM使ってWebPushするPythonこーど
#coding: UTF-8
import os
import json
import urllib.request, urllib.parse
SEND_URL = 'https://fcm.googleapis.com/fcm/send'
def lambda_handler(event, context):
headers = {
'Content-Type': 'application/json',
--- ../html.bak/index.html 2019-07-04 03:46:11.000000000 +0900
+++ index.html 2019-07-04 11:45:24.008447808 +0900
@@ -75,9 +75,22 @@
<!-- Import and configure the Firebase SDK -->
<!-- These scripts are made available when the app is served or deployed on Firebase Hosting -->
<!-- If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup -->
-<script src="/__/firebase/6.2.4/firebase-app.js"></script>
-<script src="/__/firebase/6.2.4/firebase-messaging.js"></script>
-<script src="/__/firebase/init.js"></script>
+<script src="https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js"></script>
--- ../html.bak/firebase-messaging-sw.js 2019-07-04 03:46:11.000000000 +0900
+++ firebase-messaging-sw.js 2019-07-04 11:41:01.940048485 +0900
@@ -1,9 +1,19 @@
// Import and configure the Firebase SDK
// These scripts are made available when the app is served or deployed on Firebase Hosting
// If you do not serve/host your project using Firebase Hosting see https://firebase.google.com/docs/web/setup
-importScripts('/__/firebase/5.5.6/firebase-app.js');
-importScripts('/__/firebase/5.5.6/firebase-messaging.js');
-importScripts('/__/firebase/init.js');
+importScripts('https://www.gstatic.com/firebasejs/6.2.4/firebase-app.js');
cd /usr/share/nginx/html/
svn export --force https://github.com/firebase/quickstart-js/trunk/messaging .
cp -pr . ../html.bak
vi firebase-messaging-sw.js
diff -u ../html.bak/firebase-messaging-sw.js firebase-messaging-sw.js > ../firebase-messaging-sw.js.patch
vi index.html
diff -u ../html.bak/index.html index.html > ../index.html.patch
export AUTH_KEY=ここにクラウドメッセージングタブのサーバーキーを入力
export SEND_TO=ここにブラウザで取得したInstance ID Tokenを入力
curl -X POST -H "Authorization: key=$AUTH_KEY" -H "Content-Type: application/json" -d '{ "notification": { "title": "This is Title!!!", "body": "Body Body Body.", "icon": "firebase-logo.png" }, "to": "'$SEND_TO'" }' "https://fcm.googleapis.com/fcm/send"
<!DOCTYPE html>
<html lang="ja">
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css">
</head>
<body>
<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.7/umd/popper.min.js"></script>
import os
import json
import urllib.request, urllib.parse
URL = 'https://api.line.me/v2/bot/message/push'
def lambda_handler(event, context):
to = event['to'] if 'to' in event.keys() else os.environ['SEND_TO']
token = event['token'] if 'token' in event.keys() else os.environ['CHANNEL_TOKEN']
headers = {
@smd877
smd877 / postStatus.py
Created February 1, 2020 14:20
AWSのコストとEC2の状況をLINEで報告するLambda
#coding: UTF-8
import os
import boto3
import json
import copy
import urllib.request, urllib.parse
from datetime import datetime, timedelta
URL = 'https://api.line.me/v2/bot/message/push'