Skip to content

Instantly share code, notes, and snippets.

View stewmi's full-sized avatar

Mike Steward stewmi

  • Amazon Web Services
  • Cincinnati, OH
View GitHub Profile
import boto3
import json
orgs = boto3.client("organizations")
paginator = orgs.get_paginator('list_policies')
page_iterator = paginator.paginate(Filter="SERVICE_CONTROL_POLICY")
for page in page_iterator:
for policy in page['Policies']:
@stewmi
stewmi / cfn_template.yaml
Created February 12, 2021 13:57
Base CFN Template
AWSTemplateFormatVersion: "2010-09-09"
Description: A Template
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: String
Mappings:
RegionMap:
us-east-1:
AMI: ami-76f0061f
@stewmi
stewmi / cfn_error_handling.py
Created December 3, 2020 18:31
CloudFormation Error Handling
import boto3
import botocore
cfn = boto3.client("cloudformation")
try:
stack_status = cfn.describe_stacks(StackName="test123")
except botocore.exceptions.ClientError as error:
if error.response['Error']['Code'] == "ValidationError":
print("Stack Not Found")
@stewmi
stewmi / create_launch_template.py
Last active November 18, 2020 20:42
a Python script to update Launch Template in EC2
import boto3
import argparse
ec2 = boto3.client('ec2')
DEFAULT_INSTANCE_PROFILE = ""
DEFAULT_LT_NAME = ""
def create_lt_version(lt_name, iam_instance_profile):
'''

Keybase proof

I hereby claim:

  • I am stewmi on github.
  • I am stewmi (https://keybase.io/stewmi) on keybase.
  • I have a public key ASAq85J6b-0_-6EYEZjBp5VZc19xTTiWppB1PR6iVJHysAo

To claim this, I am signing this object:

@stewmi
stewmi / s3_get_or_delete.py
Created May 27, 2020 21:30
An Example of wrapping s3 operations with a CLI utility
import argparse
import boto3
s3 = boto3.client('s3')
def get_file(bucket, file):
s3.download_file(bucket, file, file)
return
response = sts.assume_role(
RoleArn=ORGS_ROLE,
RoleSessionName="OrgsListRole"
)
session = boto3.Session(
aws_access_key_id=response['Credentials']['AccessKeyId'],
aws_secret_access_key=response['Credentials']['SecretAccessKey'],
aws_session_token=response['Credentials']['SessionToken']
)
organizations = session.client('organizations', region_name="us-east-1")
@stewmi
stewmi / pagination.py
Created July 30, 2019 17:04
Boto3 Pagination
import boto3
# Create a client
client = boto3.client('s3', region_name='us-west-2')
# Create a reusable Paginator
paginator = client.get_paginator('list_objects')
# Create a PageIterator from the Paginator
page_iterator = paginator.paginate(Bucket='my-bucket')
@stewmi
stewmi / boto3_handleexceptions.py
Created February 21, 2019 14:54
Handling Exceptions in Boto3
import boto3
from botocore.exceptions import ClientError
try:
iam = boto3.client('iam')
user = iam.create_user(UserName='fred')
print("Created user: %s" % user)
except ClientError as e:
if e.response['Error']['Code'] == 'EntityAlreadyExists':
print("User already exists")
@stewmi
stewmi / .gitconfig
Created March 26, 2018 16:50
Git Config
[user]
name = Mike Steward
email = mike@stew.cloud
[color]
ui = true
diff = auto
showbranch = auto
branch = auto
status = auto
[color "branch"]