Skip to content

Instantly share code, notes, and snippets.

Parameters:
InstanceCount:
Description: Number of EC2 instances (must be between 1 and 3).
Type: Number
Default: 1
MinValue: 1
MaxValue: 3
ConstraintDescription: Must be a number between 1 and 3.
Description: launch EC2 instances.
InstanceType:
@sudharsans
sudharsans / PutResourcePolicy.py
Last active January 10, 2024 00:07
Resource Policy for CloudWatch Logs with CloudFormation - Route53 query log
import json
import cfnresponse
import boto3
from botocore.exceptions import ClientError
client = boto3.client("logs")
def PutPolicy(arn,policyname):
response = client.put_resource_policy(
policyName=policyname,
@sudharsans
sudharsans / boto-cloudtrail-paginator.py
Created March 13, 2018 17:55
Boto3 API - example using paginator
import boto3
cloudtrail = boto3.client('cloudtrail')
paginator = cloudtrail.get_paginator('lookup_events')
StartingToken = None
page_iterator = paginator.paginate(
LookupAttributes=[{'AttributeKey':'EventName','AttributeValue': 'RunInstances'}],
PaginationConfig={'PageSize':10, 'StartingToken':StartingToken })
@sudharsans
sudharsans / cfn.yaml
Last active October 19, 2023 21:04
Lambda Script to Query Trust Advisor and Find Idle Resources
---
AWSTemplateFormatVersion: "2010-09-09"
Description:
Create Event rule and lambda functions to report for following criteria
Idle EC2 instances if CPU < 10% and Network < 5MB
RDS Instance with no connections for 7 days
ELB with No Active backends and requests below 100
Parameters:
LambdaName:
Default: AWSReport
@sudharsans
sudharsans / Sublime-Plugin.py
Created March 10, 2018 21:10
Sublime Plugin to convert frequent edits
# Sublime Plugin created for stackoverflow answer.
# https://stackoverflow.com/questions/49213489/sublime-text-and-python-plugin-how-to-incorporate-a-python-code-with-a-for-loop
import sublime, sublime_plugin, re, string #import the required modules
class ConvertCommand(sublime_plugin.TextCommand): #create Text Command
def run(self, edit): #implement run method
for region in self.view.sel(): #get user selection
if not region.empty(): #if selection not empty then
s = self.view.substr(region) #assign s variable the selected region
replace = '\n'.join([' '.join(para.splitlines()) for para in s.split('\n\n')])
@sudharsans
sudharsans / run.py
Created March 10, 2018 19:02
Identity Broker with Python3 Flask
# Quick test for https://stackoverflow.com/questions/49163883/sign-in-page-for-aws-federated-login/49212472#49212472
# Ref: https://docs.aws.amazon.com/IAM/latest/UserGuide/id_roles_providers_enable-console-custom-url.html
# python3 run.py
import urllib.parse,json
import requests # 'pip install requests'
from boto.sts import STSConnection # AWS SDK for Python (Boto) 'pip install boto'
from flask import Flask,redirect # pip install flask
app = Flask(__name__)
@sudharsans
sudharsans / ec2-running-time.py
Created March 1, 2018 11:50
Python Boto3 script to find the number of minutes an instance has been running.
import boto3
from datetime import datetime, timezone
from functools import reduce
import operator
ec2 = boto3.client('ec2')
cloudtrail = boto3.client('cloudtrail')
def get_events(instanceid):
response = cloudtrail.lookup_events (
@sudharsans
sudharsans / GetEC2RunTime.py
Last active December 20, 2023 19:23
Python Boto3 script to find the number of minutes an instance has been running.
##
# Python Boto3 script to find the number of minutes an instance has been running.
## Output
# i-00asdf5xxx50c96aa 84 days, 13:17:51.468559
# i-0260a1fxxx27ec894 6 days, 13:48:04.468643
# i-0acxxx6c630af322 13 days, 12:13:00.468659
# i-069bxxxd41bf975eb 19:42:08.468670
import boto3
from datetime import datetime, timezone
@sudharsans
sudharsans / GetIps.groovy
Last active December 19, 2022 15:56
Groovy, AWS CLI, Active Choices Reactive Parameter and Jenkins
// https://stackoverflow.com/questions/48982349/query-aws-cli-to-populate-jenkins-active-choices-reactive-parameter-linux
// Get list of IPS from AWS CLI and populate Active Choices Reactive Parameter
def command = 'aws ec2 describe-instances --filters Name=tag:Name,Values=Test --query Reservations[*].Instances[*].PrivateIpAddress --output text'
def proc = command.execute()
proc.waitFor()
def output = proc.in.text
def exitcode= proc.exitValue()
def error = proc.err.text
@sudharsans
sudharsans / CustomResourceSample.yaml
Last active November 20, 2020 14:02
Custom Lambda function to enable logs types to publish RDS logs to Amazon CloudWatch Logs
---
AWSTemplateFormatVersion: '2010-09-09'
Resources:
EnableLogs:
Type: Custom::EnableLogs
Version: '1.0'
Properties:
ServiceToken: arn:aws:lambda:us-east-1:acc:function:rds-EnableRDSLogs-1O6XLL6LWNR5Z
DBInstanceIdentifier: mydb
Outputs: