Skip to content

Instantly share code, notes, and snippets.

@anecula
anecula / aws-logs-tail-group.sh
Created December 3, 2018 19:51 — forked from tekwiz/aws-logs-tail-group.sh
AWS Logs Tailer
#!/bin/bash
group_name='<log-group-name>'
start_seconds_ago=3600
aws_cmd_opts= # e.g. "--profile <profile-name>"
# Usage: get_loglines "<log-group-name>" <start-time>
get_loglines() {
aws $aws_cmd_opts --output text logs filter-log-events \
--log-group-name "$1" \
@anecula
anecula / get_cloudwatch_logs.py
Created December 3, 2018 19:51 — forked from eldondevcg/get_cloudwatch_logs.py
Pull down cloudwatch logs with boto
# IF YOU INCUR HUGE COSTS WITH THIS OR IT BREAKS DON'T BLAME ME License
# This is a throw-away script I wrote to pull the json events for all of the streams from a cloudwatch log
# For some reason, the naive way to do vpc network logging does logging to different streams in a cloudwatch
# log based on interface.
# Great for diagnosing lots of things, and generating verbose logs, but for the broad-stroke analysis I was doing,
# all I really wanted was the basic data. This would have been easier if I had logged to s3, but I did not see a
# way to do that in 2 clicks.
group_name = 'CHANGEME'

AWS Systems Manager lets you remotely and securely manage the configuration of your managed instances. It helps you automate management tasks.

  • Create IAM Role - Assign Permissions
  • Create EC2 & Assign IAM Role - "AmazonEC2RoleforSSM" role to an Amazon EC2 instance with "AmazonSSMFullAccess" Permissions
  • Bootstrap with SSM
  • Run Command

User-Data:

#!/bin/bash
cd /tmp
@anecula
anecula / all_aws_managed_policies.json
Created October 1, 2018 09:56 — forked from gene1wood/all_aws_managed_policies.json
A list of all AWS managed policies and they're policy documents as well as a short script to generate the list
{
"AWSAccountActivityAccess": {
"Arn": "arn:aws:iam::aws:policy/AWSAccountActivityAccess",
"AttachmentCount": 0,
"CreateDate": "2015-02-06T18:41:18+00:00",
"DefaultVersionId": "v1",
"Document": {
"Statement": [
{
"Action": [
@anecula
anecula / all_aws_lambda_modules.txt
Created September 26, 2018 13:00 — forked from gene1wood/all_aws_lambda_modules_python.md
AWS Lambda function to list all available Python modules and post the list to Pastebin
# module list (generated by listmodules.py)
#
# timestamp='20160226T200954Z'
# sys.version='2.7.10 (default, Dec 8 2015, 18:25:23) \n[GCC 4.8.3 20140911 (Red Hat 4.8.3-9)]'
# sys.platform='linux2'
# platform='Linux-4.1.13-19.31.amzn1.x86_64-x86_64-with-glibc2.2.5'
#
BaseHTTPServer
Bastion
CDROM
@anecula
anecula / parse_aws_s3_billing.py
Created September 12, 2018 15:06 — forked from oddskool/parse_aws_s3_billing.py
Simplistic script to parse the detailed AWS billing CSV file. Script displays cost of S3 operations broken down per region, bucket and usage type (either storage or network). It also sums up the amount of storage used per bucket. Output is filtered wrt to costs < 1$. See http://docs.aws.amazon.com/awsaccountbilling/latest/about/programaccess.html
# -*- coding:utf-8 -*-
'''
Simplistic script to parse the detailed AWS billing CSV file.
Script displays cost of S3 operations broken down per region, bucket and usage
type (either storage or network). It also sums up the amount of storage used per bucket.
Output is filtered wrt to costs < 1$.
See http://docs.aws.amazon.com/awsaccountbilling/latest/about/programaccess.html for
how to set up programmatic access to your billing.
@anecula
anecula / get_aws_billing.sh
Created September 8, 2018 19:19 — forked from sechiro/get_aws_billing.sh
aws cliを使って料金を確認。リージョンは「us-east-1」固定(※1)。 ディメンションも「--dimensions Name=Currency,Value=USD」が必須。jqを入れているのは、タイムスタンプ順にソートするため。※1 http://aws.amazon.com/jp/billing/faqs/#Can_I_view_estimated_charge_metrics_or_set_billing_alerts_in_a_region_other_than_US-East
#!/bin/bash
namespace=AWS/Billing
os_name=`uname`
now=`date -u "+%Y-%m-%dT%H:%M:%SZ"`
if [ $os_name = "Darwin" ]; then
yesterday=`date -u -v-1d "+%Y-%m-%dT%H:%M:%SZ"`
else
yesterday=`date -u -d "1 days ago" "+%Y-%m-%dT%H:%M:%SZ"`
fi
start_time=${1:-$yesterday}
@anecula
anecula / gist:9e6fa226c4b92443c1a0549a066aac28
Created September 8, 2018 18:59 — forked from bfleming-ciena/gist:c1d3e5a5b77a156473a8
Pull estimated AWS charges Billing
#list metrics
aws --profile dev --region us-east-1 cloudwatch list-metrics --namespace "AWS/Billing"
# Estimated charges
aws --region us-east-1 cloudwatch get-metric-statistics --namespace "AWS/Billing" --metric-name "EstimatedCharges" --dimension "Name=Currency,Value=USD" --start-time $(gdate +"%Y-%m-%dT%H:%M:00" --date="-12 hours") --end-time $(gdate +"%Y-%m-%dT%H:%M:00") --statistic Maximum --period 60 --output text | sort -r -k 3 | head -n 1 | cut -f 2
# Marketplace Charges
aws --profile dev --region us-east-1 cloudwatch get-metric-statistics --namespace "AWS/Billing" --metric-name "EstimatedCharges" --dimensions Name=ServiceName,Value=AWSMarketplace Name=Currency,Value=USD --start-time $(gdate +"%Y-%m-%dT%H:%M:00" --date="-12 hours") --end-time $(gdate +"%Y-%m-%dT%H:%M:00") --statistic Maximum --period 60 --output text
# Redshift

Using cloud-watch alerts I want to set daily bills alert for each used AWS service.

Unfortunately, AWS doesn’t have an API to get custom billing for a time-frame, if the bill exceeds the set amount you will be charged by the month’s end but this alert can get trigged in a few days and the damage is already done.

I have a template to raise alarms in CloudWatch, and I want to customized to monitor daily and send alerts if the bill exceeds a certain amount:

Description: >

    This template deploys the billing stack
@anecula
anecula / estimated_bill.sh
Created September 7, 2018 18:21 — forked from RajatGoyal/estimated_bill.sh
total estimated bill aws
#!/bin/sh
from_email="from@synaptic.com"
to_email="to@synaptic.com"
max_estimated_charge_12_hour=$(aws --region us-east-1 cloudwatch get-metric-statistics \
--namespace "AWS/Billing" \
--metric-name "EstimatedCharges" \
--dimension "Name=Currency,Value=USD" \
--start-time $(date +"%Y-%m-%dT%H:%M:00" --date="-12 hours") --end-time $(date +"%Y-%m-%dT%H:%M:00") \