Skip to content

Instantly share code, notes, and snippets.

@coingraham
coingraham / startstopinstanceswithtag.py
Created March 21, 2016 14:44
Python - start stopped or stop running instances with particular tag
import boto3
tagname = "Name"
tagvalue = "Value"
ec2 = boto3.resource('ec2')
ec2.instances.filter(Filters=[
{'Name': 'tag': [tagname], 'Values': [tagvalue]},
{'Name': 'instance-state-name', 'Values': ['running']}
]).start()
ec2.instances.filter(Filters=[
@coingraham
coingraham / editasgwithtag.py
Created March 21, 2016 14:48
Python - change ASG based on tag and current MinSize
import boto3
tagname = "Name"
tagvalue = "Value"
asg_client = boto3.client('autoscaling')
groups = asg_client.describe_auto_scaling_groups()
for group in groups[u'AutoScalingGroups']:
@coingraham
coingraham / readyamlfroms3.py
Last active December 8, 2021 12:07
Python - read yaml from S3
import boto3
bucket = "bucket"
s3_client = boto3.client('s3')
response = s3_client.get_object(Bucket=bucket, Key="filename.yaml")
try:
configfile = yaml.safe_load(response["Body"])
except yaml.YAMLError as exc:
return exc
@coingraham
coingraham / getcfttags.py
Created March 21, 2016 14:56
Python - get cloudformationtemplate client and it's tags based on name
import boto3
cftname = "Name"
tagname1 = "Key1"
tagname2 = "Key2"
cft_client = boto3.client('cloudformation')
# Here we only get one stack because we know the exact name
stack = cft_client.describe_stacks(StackName=cftname)[u'Stacks'][0]
@coingraham
coingraham / adminwithmfaonly.json
Created March 21, 2016 17:25
AWS IAM Policy Admin with MFA Only
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": "*",
"Resource": "*",
"Condition": {
"Bool": {
"aws:MultiFactorAuthPresent": "true"
@coingraham
coingraham / updateredrivesqs.py
Created March 21, 2016 19:27
Python - Update the redrive policy for existing queues
import boto3
import json
# update these with your actual settings
maxreceivecount = 3
queueurl = "https://sqs.region.amazonaws.com/accountnumber/queuename"
deadlettertargetarn = "arn:aws:sqs:region:accountnumber:deadletterqueuename"
# you could just create a json string
policy = {"maxReceiveCount" : maxreceivecount, "deadLetterTargetArn": deadlettertargetarn}
@coingraham
coingraham / nfs
Last active April 4, 2016 20:18
Setup NFS
#uncomment in nfs config file
LOCKD_TCPPORT=32803
LOCKD_UDPPORT=32769
MOUNTD_PORT=892
#Setup Security Group Rules as follows
TCP 111 - [source]
UDP 111 - [source]
TCP 892 - [source]
UDP 892 - [source]
@coingraham
coingraham / nfs.json
Created March 28, 2016 15:06
NFS Cloud Formation Template
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "CloudFormation Template for deploying a local NFS Server using Amazon Linux AMI",
"Parameters" : {
"AZ1" : {
"Description" : "What availability zone should this stack be launched in? us-east-1a, us-east-1b, etc.",
"Type" : "String",
"Default" : "eu-west-1a",
"ConstraintDescription" : "must be a valid EC2 Availability Zone"
},
@coingraham
coingraham / testses.sh
Last active March 6, 2024 02:11
Test AWS SES from the CLI Command Line
# Connect to AWS SES
openssl s_client -crlf -quiet -connect email-smtp.us-east-1.amazonaws.com:465
openssl s_client -crlf -quiet -starttls smtp -connect email-smtp.us-east-1.amazonaws.com:25
# SMTP commands
EHLO 2ndwatch.com
AUTH LOGIN
# Username by 'echo -n [accesskey] | base64'
skdfjlskdflskj
@coingraham
coingraham / createuploadassets.py
Created April 26, 2016 21:25
Python Script to Create User and companion S3 Bucket
import datetime
import random
import string
import time
import argparse
import json
try:
import unicodecsv as csv
except ImportError: