curl -Ls https://gist.githubusercontent.com/atheiman/45e45ada59e558b21f951d8e81faf345/raw/cloudshell-setup.sh?$RANDOM | bash
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import json | |
import boto3 | |
import os | |
import traceback as tb | |
if boto3.session.Session().region_name.startswith("us-gov-"): | |
partition = "aws-us-gov" | |
regions = ["us-gov-west-1", "us-gov-east-1"] | |
else: | |
partition = "aws" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import boto3 | |
import os | |
# Example usage from a bash shell: | |
# PREFIX='AWS COMM' AWS_PROFILE=comm-mgmt ROLE_NAME=AWSControlTowerExecution python ./aws_switch_role_bookmark_generator.py > ./aws-switch-role-bookmarks.html | |
# Environment variables for configuration | |
role_name = os.environ.get("ROLE_NAME", "OrganizationAccountAccessRole") | |
include_mgmt = os.environ.get("INCLUDE_MGMT", "true").lower() == "true" | |
prefix = os.environ.get("PREFIX", "AWS") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import json | |
import boto3 | |
default_region = boto3.Session().region_name | |
if default_region.startswith("us-gov-"): | |
partition = "aws-us-gov" | |
regions = ["us-gov-west-1", "us-gov-east-1"] | |
else: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import boto3 | |
import json | |
sechub = boto3.client("securityhub") | |
sts = boto3.client("sts") | |
caller_arn = sts.get_caller_identity()["Arn"] | |
print(caller_arn) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# aws cloudformation deploy \ | |
# --profile mgmt \ | |
# --template-file ./template.yml \ | |
# --stack-name ConfigRuleAccountTags \ | |
# --capabilities CAPABILITY_IAM | |
Resources: | |
ConfigRule: | |
Type: AWS::Config::ConfigRule | |
DependsOn: EvaluationFunctionConfigPermission |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from ipaddress import IPv4Network, IPv4Address, summarize_address_range | |
import json | |
import os | |
def lambda_handler(event, context): | |
print(json.dumps(event)) | |
# Basic event validation | |
if "cidrs" not in event or not isinstance(event["cidrs"], list) or len(event["cidrs"]) < 1: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Resources: | |
SecurityHubFindingUpdateFunction: | |
Type: AWS::Lambda::Function | |
Properties: | |
Description: Applies metadata to Security Hub findings | |
Role: !Sub '${SecurityHubFindingUpdateFunctionRole.Arn}' | |
# ReservedConcurrentExecutions can be used to throttle the function if invocations get too | |
# high. However, all findings may not be updated. | |
#ReservedConcurrentExecutions: 3 | |
Environment: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def dict_dot_notation(d, path=[]): | |
d2 = {} | |
for k, v in d.items(): | |
k_path = path + [str(k)] | |
k_formatted = ".".join(k_path) | |
if isinstance(v, dict): | |
# merge in dict with recursive call | |
d2 = {**d2, **dict_dot_notation(v, path=k_path)} | |
elif isinstance(v, list) or isinstance(v, tuple): |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python3 | |
import boto3 | |
import botocore | |
partition = 'aws' | |
regions = ['us-east-1', 'us-west-2'] | |
skip_master_acct = True | |
organizations = boto3.client('organizations') |