Skip to content

Instantly share code, notes, and snippets.

@downspot
downspot / subnet_hack.txt
Created Apr 16, 2019
terraform bad subnet removal hack
View subnet_hack.txt
"${element(split(",", replace(replace(replace(replace(join(",", data.aws_subnet_ids.private.ids), "/subnet-ca201abd,/", ""), "/subnet-fc0870c1,/", ""), "/subnet-6630da5b/", ""), "/subnet-3f35c402,/", "")), count.index)}"
@downspot
downspot / EC2_AMI_retention.sh
Last active Apr 22, 2018
EC2 AMI retention
View EC2_AMI_retention.sh
#!/bin/sh
date=`date +%Y-%m-%d -d "30 day ago"`
for region in us-east-1 us-west-2 ; do
aws ec2 describe-images --region $region --owners self --output text --filters 'Name=name,Values=IMAGE-NAME-*' --query 'Images[?CreationDate<=`'${date}'`][ImageId]' | grep -v `aws ec2 describe-instances --region $region --output text --filters 'Name=tag:Name,Values=IGNORE-STRING-*' --query 'Reservations[*].Instances[*].[ImageId]' | sed -n 1p` > remove_ami-$region.txt
for remove in `cat remove_ami-$region.txt` ; do
aws ec2 deregister-image --region $region --image-id $remove
@downspot
downspot / ECS_policy.json
Created Feb 11, 2018
policy for ECS access
View ECS_policy.json
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "ECSAccess",
"Effect": "Allow",
"Action": [
"ecs:Poll",
"ecs:StartTask",
@downspot
downspot / terraform_cycle.tf
Created Feb 11, 2018
terraform cycle through instances, useful for adding alarms/names
View terraform_cycle.tf
resource "aws_cloudwatch_metric_alarm" "cpu_credits" {
count = 2
...
dimensions { InstanceId = "${element(list("i-07656896d6947814c", "i-042e664331a74e385"), count.index)}" }
dimensions { InstanceId = "${element(aws_instance.my_instance.*.id, count.index)}" }
}
View ubuntu_find_java_home.sh
#!/bin/bash
# Find the absolute path of the java executable
# follow all symlinks (to cover /etc/alternatives/java ...)
jpath=$(readlink -e $(which java))
if [[ "$jpath" != "" ]]; then
while [[ "$jpath" != "/" ]]; do
jpath=$(dirname $jpath)
View runtest_function.sh
#!/bin/sh
email="email@example.com"
function runtest {
"$@"
local status=$?
if [ $status -ne 0 ]; then
echo "Script failed with error from '$@'" | mail -s "Script failure!" $email
exit 0
@downspot
downspot / aws_cloudwatch_metric_alarm_recover.tf
Created Feb 11, 2018
aws_cloudwatch_metric_alarm recover
View aws_cloudwatch_metric_alarm_recover.tf
resource "aws_cloudwatch_metric_alarm" "example_host_alarm" {
alarm_name = "${aws_instance.example_host_alarm.id}"
comparison_operator = "GreaterThanOrEqualToThreshold"
evaluation_periods = "2"
metric_name = "StatusCheckFailed_System"
namespace = "AWS/EC2"
period = "60"
statistic = "Maximum"
threshold = "1.0"
alarm_description = "Created from EC2 Console"
View terraform_remote_state.tf
data "terraform_remote_state" "network" {
backend = "s3"
workspace = "${terraform.workspace}"
config {
bucket = "bucket-name"
key = "terraform.tfstate"
region = "${var.region}"
}
}