Skip to content

Instantly share code, notes, and snippets.

View artem-hatchenko's full-sized avatar

Artem Hatchenko artem-hatchenko

View GitHub Profile
import boto3
import json
import csv
import datetime
from pkg_resources import resource_filename
ReportFile = './report.csv'
CpuThreshold = 40 # If we reduce instance size, CPU utilization will be twice. e.g. 40*2=80% CPU utilization
# EC2 filter, we will skip terminated instances
properties([
parameters([
choice(
name: 'PYTHON',
description: 'Choose Python version',
choices: ["python2.7", "python3.6", "python3.7", "python3.8", "python3.9"].join("\n")
),
base64File(
name: 'REQUIREMENTS_FILE',
description: 'Upload requirements file (Optional)'
from __future__ import print_function
from datetime import datetime
import sys
import boto3
import botocore
import urllib.request
ec2 = boto3.client('ec2', region_name='eu-west-1')
def create_snapshot(app):
{
"Version": "2012-10-17",
"Statement": [
{
"Action": [
"ec2:AttachNetworkInterface",
"ec2:DetachNetworkInterface",
"ec2:DescribeInstances",
"ec2:DescribeNetworkInterfaces",
"ec2:AssignPrivateIpAddresses",
{
"Version": "2012-10-17",
"Id": "certbot-dns-route53",
"Statement": [
{
"Effect": "Allow",
"Action": [
"route53:ListHostedZones",
"route53:GetChange"
],
import boto3
custom_ec2_filter = [
{
'Name': 'instance-state-name',
'Values': ['running', 'pending']
}
]
# Combination of tag key and value to prevent shutdown/downscale resources
AWSTemplateFormatVersion: "2010-09-09"
Description: 'Template for Lambda Fuction for stopping AWS resources (EC2, RDS, ASG)'
### OUTPUT ###
Outputs:
LambdaRoleARN:
Description: Role for Lambda execution.
Value:
Fn::GetAtt:
- LambdaRole
import sys
import boto3
 
def cleanup():
    get_last_modified = lambda obj: int(obj['LastModified'].strftime('%s'))
 
    s3 = boto3.client('s3')
    result = s3.list_objects(Bucket=bucket, Delimiter='/')
    for dir in result.get('CommonPrefixes'):
        print('Directory: ' + str(dir['Prefix']))
stage("Send notification") {
steps {
script {
withCredentials([string(credentialsId: 'slack-app', variable: 'slack_token')]) {
env.SLACK_USER_ID = sh (script: """
SLACK_USER_ID="\$(curl -d \"token=${slack_token}\" -d \"email=${env.AUTHOR_EMAIL}\" https://slack.com/api/users.lookupByEmail | jq -r '.user.id')"
echo \$SLACK_USER_ID""", returnStdout: true).trim()
}
if ("${env.PULL_REQUEST_COMMENT_COUNT}" == "0") {
sendBitbucketComment ("${env.REPO_FULL_NAME}", "${env.PULL_REQUEST_ID}", "${env.WELCOME_COMMENT}")
env.AUTHOR_EMAIL = "${env.AUTHOR}".replaceAll("\\s",".").toLowerCase() + "@automat-it.com"