Skip to content

Instantly share code, notes, and snippets.

View junkangli's full-sized avatar

Li Junkang junkangli

View GitHub Profile
@junkangli
junkangli / urlopen.py
Last active January 22, 2020 03:46
Python code that makes a http call to specified url. This may be used to create a AWS Python 3.8 Lambda Function to test the network connectivity to a remote site.
import logging
from urllib.request import urlopen
logger = logging.getLogger()
logger.setLevel(logging.INFO)
SITE = 'http://www.google.com'
def lambda_handler(event, context):
response = urlopen(SITE)
@junkangli
junkangli / run-okta-aws.ps1
Last active May 19, 2019 16:45
PowerShell script to be used together with Okta AWS CLI Assume Role Tool to use the OKTA_PROFILE parameter configured to set the AWS profile name.
function Update-OktaConfigFile {
Param(
[Bool]$Uncomment,
[Int]$MatchLineNumber
)
$content = Get-Content -Path $PSScriptRoot\config.properties
$content |
ForEach-Object {
if ($_.ReadCount -eq $MatchLineNumber -or $_.ReadCount -eq $MatchLineNumber+1) {
@junkangli
junkangli / CloudTrailEC2InstanceChanges.filter
Created March 6, 2019 10:32
This is a metric filter to search for CloudTrailEC2InstanceChanges alerts in CloudWatch Log of CloudTrail events.
{ ($.eventSource="ec2.amazonaws.com") && ($.userIdentity.type="IAMUser") }
@junkangli
junkangli / okta.cmd
Last active April 23, 2019 01:52
CMD script to execute run-okta-aws.ps1 PowerShell script.
@echo off
powershell.exe -File %USERPROFILE%\.okta\run-okta-aws.ps1
@junkangli
junkangli / GetMonthlyAWSCost.js
Created December 5, 2018 08:37
This is an AWS Lambda function that gets the various AWS costs for the previous month and publishes the result to a SNS topic. It is meant to be triggered on a monthly schedule by a CloudWatch event rule.
const AWS = require('aws-sdk');
const snsTopicArn = process.env.SNSTopicArn;
const idepAccountNumbers = process.env.IDEPAWSAccountNumbers.split(',').map(item => item.trim());
const ninetyninesmeAccountNumbers = process.env.Project99SMEAWSAccountNumbers.split(',').map(item => item.trim());
AWS.config.update({ region: 'us-east-1' });
const costexplorer = new AWS.CostExplorer();
const getTotal = (params) => {
@junkangli
junkangli / BasicAuthnAuthorizer.js
Created November 29, 2018 06:33
This is an AWS Lambda function that implements Basic authentication scheme to control access to API methods in Amazon API Gateway. It is to be configured as an API Gatewy Lambda authorizer.
exports.handler = function(event, context, callback) {
const username = process.env['username'];
const password = process.env['password'];
var token = event.authorizationToken;
var encodedCreds = token.split(' ')[1];
var plainCreds = (new Buffer(encodedCreds, 'base64')).toString().split(':');
var p_username = plainCreds[0];
var p_password = plainCreds[1];
@junkangli
junkangli / AzureDevOpsNotifyHipChat.js
Last active October 26, 2018 09:50
This is an AWS Lambda function that handles an Azure DevOps Services event and sends a notification to a room on a hosted HipChat server. It is meant to integrate with AWS API Gateway and subscribe to an Azure DevOps Services project Web Hook.
const http = require('https');
var hipchatToken = process.env['hipchatToken']
var hipchatRoom = '96';
exports.handler = async (event, context, callback) => {
const body = JSON.parse(event.body);
const message = body.message;
var hipchatMessage = JSON.stringify({
@junkangli
junkangli / NotifyHipchat.ps1
Created May 17, 2018 09:01
PowerShell script to post a notification to a chat room
$apitoken = <redacted>
$roomid = "20"
$messageText = "message_goes_here"
$color = 'yellow'
$body = @{
color = $color
message = $messageText
notify = $true
message_format = "text"