Skip to content

Instantly share code, notes, and snippets.

@kamgaurav
kamgaurav / resize-ebsvolume.ps1
Created December 31, 2018 08:36
Resize Live AWS EBS Volume
Param (
[string][Parameter(Mandatory = $True)] $VolumeId,
[int][Parameter(Mandatory = $True)] $NewSize
)
$CurrentSize = Get-EC2Volume -Volume $VolumeId
If ($NewSize -lt $CurrentSize.Size) { Write-Host "New volume must be larger than current" -ForegroundColor Red; break}
Edit-EC2Volume -VolumeId $VolumeId -Size $NewSize | Out-Null
$ModifiedSize = (Get-EC2Volume -Volume $VolumeId).Size
While ($ModifiedSize -ne $NewSize) {
@kamgaurav
kamgaurav / Remove-EBSSnapshot.ps1
Last active December 20, 2018 22:11
Remove EBS Snapshot with Lambda Function
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.365.0'}
$RetentionDays = 14
$Snapshots = Get-EC2Snapshot -Filter @{Name = "tag:BackupType"; Values = "Daily"}
ForEach ($Snapshot in $Snapshots) {
$Retention = ([DateTime]::Now).AddDays(-$RetentionDays)
if ([DateTime]::Compare($Retention, $Snapshot.StartTime) -gt 0) {
Remove-EC2Snapshot -SnapshotId $snapshot.SnapshotId -Force
@kamgaurav
kamgaurav / Create-EBSSnapshot.ps1
Last active December 20, 2018 22:09
Lambda Function to create EBS Snapshot
#Requires -Modules @{ModuleName='AWSPowerShell.NetCore';ModuleVersion='3.3.365.0'}
$Type = 'Daily'
Get-EC2Volume -Filter @{Name = "tag:BackupEnabled"; Values = "True"} |
ForEach-Object {
If ($_.Attachment) {
$Device = $_.Attachment[0].Device
@kamgaurav
kamgaurav / StateMachineDefinition.json
Last active December 21, 2018 06:43
Step Function State Machine Definition
{
"StartAt": "Create-EBSSnapshot",
"States": {
"Create-EBSSnapshot": {
"Type": "Task",
"Resource": "arn:aws:lambda:eu-west-1:527359538786:function:Create-EBSSnapshot",
"Next": "Remove-EBSSnapshot"
},
"Remove-EBSSnapshot": {
"Type": "Task",
@kamgaurav
kamgaurav / AssumeRolePolicy-CloudWatchRule.json
Created December 20, 2018 21:55
Allows CloudWatch to call AWS services on your behalf.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "events.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
@kamgaurav
kamgaurav / Policy-StateMachineInvoke.json
Last active December 21, 2018 06:41
Policy to Invoke Step Function.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Action": [
"states:StartExecution"
],
"Resource": [
"arn:aws:states:eu-west-1:527359538786:stateMachine:StateMachine-ManageSnapshot"
@kamgaurav
kamgaurav / AssumeRolePolicy-StepFunction.json
Created December 20, 2018 21:53
Allows Step Function to call AWS services on your behalf.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "states.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
@kamgaurav
kamgaurav / Policy-LambdaInvoke.json
Last active December 21, 2018 06:41
Policy to Invoke Lambda Function.
{
"Version":"2012-10-17",
"Statement":[
{
"Effect":"Allow",
"Action":[
"lambda:InvokeFunction"
],
"Resource":[
"arn:aws:lambda:eu-west-1:527359538786:function:Create-EBSSnapshot",
@kamgaurav
kamgaurav / AssumeRolePolicy-Lambda.json
Created December 20, 2018 21:51
Allows Lambda Function to call AWS services on your behalf.
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "lambda.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
@kamgaurav
kamgaurav / Policy-EBSSnapshot.json
Last active December 20, 2018 22:07
Policy to take Snapshot of EBS volumes.
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "VisualEditor0",
"Effect": "Allow",
"Action": [
"ec2:DeleteSnapshot",
"ec2:CreateTags",
"ec2:CreateSnapshot"