Skip to content

Instantly share code, notes, and snippets.

View atheiman's full-sized avatar
😬

Austin Heiman atheiman

😬
View GitHub Profile
@atheiman
atheiman / TerraformProviderRole.yml
Last active October 22, 2023 12:37
CloudFormation templates to deploy a Terraform S3 backend. Includes S3 bucket for state file storage w/ replication for backups, DynamoDB table for state locking, IAM role for accessing the S3 backend, and an IAM role to use with the AWS provider. See https://developer.hashicorp.com/terraform/language/settings/backends/s3 and https://registry.te…
# Deployed in each account where Terraform will be used.
# aws cloudformation deploy \
# --template-file ./TerraformProviderRole.yml \
# --stack-name TerraformProviderRole \
# --capabilities CAPABILITY_IAM
Resources:
TerraformProviderRole:
Type: AWS::IAM::Role
@atheiman
atheiman / README.md
Last active November 14, 2022 15:40
CloudFormation CI/CD using CodeBuild, mimicking a Terraform "Live" repository strategy

CloudFormation "Live"

A CloudFormation template to quickstart a CloudFormation "Live" repository strategy. This supports a "GitOps" strategy for deploying CloudFormation stacks to multiple AWS accounts and regions.

  1. Deploy cfn-live-deploy-role.yml to one or more accounts you need to manage CloudFormation stacks in. Note the created role ARNs.
  2. Deploy cfn-live.yml into a central account where the CodeCommit repository and CodeBuild CI/CD job will be stored.
@atheiman
atheiman / state-machine.yml
Created November 7, 2022 19:58
Step Functions State Machine example using States.ArrayPartition() to create batches of items from a list of items.
{
"StartAt": "Pass1",
"States": {
"Pass1": {
"Type": "Pass",
"Result": {
"Items": [
"a",
"b",
"c",
@atheiman
atheiman / config_aggregator_query.py
Last active January 19, 2024 13:21
Example of querying AWS Config aggregator using Python and boto3
#!/usr/bin/env python3
# Example usage:
#
# ~ $ export AWS_PROFILE=organization-management-account
# ~ $ python ~/tmp/config_aggregator_query.py
# 53 resources inspected
#
import json
@atheiman
atheiman / config-aggregator-search.sh
Last active February 11, 2024 01:04
Get all EC2 instances from an AWS Config Aggregator using AWS CLI
aws configservice batch-get-aggregate-resource-config \
--configuration-aggregator-name 'MyConfigAggregator' \
--resource-identifiers "$(
aws configservice list-aggregate-discovered-resources \
--configuration-aggregator-name 'MyConfigAggregator' \
--resource-type 'AWS::EC2::Instance' \
--query 'ResourceIdentifiers'
)"
@atheiman
atheiman / template.yml
Last active February 11, 2024 01:04
aws-delete-all-organization-default-vpcs
Description: >-
Scheduled Lambda function to delete Default VPC from all accounts in the organization. Deploys an IAM role via
CloudFormation stackset to the organization root and schedules a Lambda function to assume the IAM roles in each
account and attempt to delete the Default VPCs in every Region. If any operation fails, or if a Default VPC is not
empty, or if a Default VPC has a peering connection, the function invocation fails.
Parameters:
OrgRootId:
Type: String
AllowedPattern: '^r-[a-zA-Z0-9]+$'
@atheiman
atheiman / template.yml
Created February 10, 2022 14:48
AWS IAM service-linked role for AWS Backup
Conditions:
PrimaryRegion: !Or [!Equals [!Ref AWS::Region, 'us-east-1'], !Equals [!Ref AWS::Region, 'us-gov-west-1']]
Resources:
BackupSlr:
Type: AWS::IAM::ServiceLinkedRole
Condition: PrimaryRegion
DeletionPolicy: Retain
UpdateReplacePolicy: Retain
Properties:
@atheiman
atheiman / template.yml
Last active March 30, 2024 05:09
CloudFormation template to create a CodeCommit repo and CodeBuild CI/CD. Updates to the main branch and pull requests trigger builds. Feature branch build status is commented on pull requests.
# Usage examples:
#
# Create a new CodeCommit repository with CodeBuild CI/CD
#
# aws cloudformation deploy \
# --stack-name my-new-project \
# --template-file ./template.yml \
# --capabilities CAPABILITY_IAM \
# --parameter-overrides 'RepositoryDescription=My new project description'
#
@atheiman
atheiman / buildspec.yml
Last active April 20, 2024 05:03
Simple AWS CodeBuild buildspec.yml for CloudFormation CI/CD
# Lint (cfn-lint) and Package (aws cloudformation package ...) run on templates for all branches.
# Deploy all templates on 'main' branch (stack names will be built from template names).
version: 0.2
phases:
install:
runtime-versions:
python: 3.9
pre_build:
@atheiman
atheiman / template.yml
Last active February 11, 2024 01:04
CodeCommit repository and CodeBuild job run on push to any branch
Resources:
CodeCommitRepository:
Type: AWS::CodeCommit::Repository
Properties:
RepositoryName: !Ref AWS::StackName
CodeBuildProject:
Type: AWS::CodeBuild::Project
Properties:
Name: !Ref AWS::StackName