项目作者: base2Services

项目描述 :
Lambda functions to cleanup EBS snapshots and AMIs
高级语言: C#
项目地址: git://github.com/base2Services/lambda-cleanup-ebs-ami.git
创建时间: 2017-06-16T06:00:17Z
项目社区:https://github.com/base2Services/lambda-cleanup-ebs-ami

开源协议:

下载


Lambda functions to report and cleanup EBS snapshots and AMIs

1 - build lambda locally
2 - deploy using built ‘Base2.Lambdas.zip’ package (manaully for now)
3 - run the function to generate report. use payload from test/run
section of this README to see parameters
4 - run the function to cleanup orphaned AMI EBS snapshosts

Requirements

Build

You will need docker engine and zip utility to build project. Also, build script uses bash shell
If you have dotnet cli locally installed you may use scripts/build_native.sh, but docker build is
recommended way for automating builds.

  1. $ scripts/build_docker.sh
  2. Restoring packages for /project/Base2.Lambdas.csproj...
  3. Lock file has not changed. Skipping lock file write. Path: /project/obj/project.assets.json
  4. Restore completed in 2.06 sec for /project/Base2.Lambdas.csproj.
  5. NuGet Config files used:
  6. /root/.nuget/NuGet/NuGet.Config
  7. Feeds used:
  8. https://api.nuget.org/v3/index.json
  9. Microsoft (R) Build Engine version 15.1.1012.6693
  10. Copyright (C) Microsoft Corporation. All rights reserved.
  11. Base2.Lambdas -> /project/bin/Debug/netcoreapp2.1/Base2.Lambdas.dll
  12. adding: AWSSDK.AutoScaling.dll (deflated 70%)
  13. adding: AWSSDK.Core.dll (deflated 66%)
  14. adding: AWSSDK.EC2.dll (deflated 70%)
  15. adding: AWSSDK.S3.dll (deflated 63%)
  16. adding: Amazon.Lambda.Core.dll (deflated 57%)
  17. adding: Amazon.Lambda.Serialization.Json.dll (deflated 56%)
  18. adding: Base2.Lambdas.deps.json (deflated 74%)
  19. adding: Base2.Lambdas.dll (deflated 55%)
  20. adding: Base2.Lambdas.pdb (deflated 40%)
  21. adding: Newtonsoft.Json.dll (deflated 60%)
  22. adding: System.Collections.NonGeneric.dll (deflated 60%)
  23. adding: System.Runtime.Serialization.Primitives.dll (deflated 48%)

Automated deployment

You will need serverless framework, version > 1.15 to deploy lambda functions automatically. Use sls deploy,
in comnbination with properly set environment variables:

  1. $ export REGION=ap-southeast-2
  2. $ export SOURCE_BUCKET=automation.cleanup.base2.services
  3. $ sls deploy
  4. Serverless: Packaging service...
  5. Serverless: Uploading CloudFormation file to S3...
  6. Serverless: Uploading artifacts...
  7. Serverless: Validating template...
  8. Serverless: Creating Stack...
  9. Serverless: Checking Stack create progress...
  10. .........................................
  11. Serverless: Stack create finished...
  12. Service Information
  13. service: manualawscleanup
  14. stage: dev
  15. region: ap-southeast-2
  16. api keys:
  17. None
  18. endpoints:
  19. None
  20. functions:
  21. AMIReport: manualawscleanup-dev-AMIReport
  22. AMICleanup: manualawscleanup-dev-AMICleanup
  23. EBSReport: manualawscleanup-dev-EBSReport
  24. EBSCleanup: manualawscleanup-dev-EBSCleanup

Lambda configuration

Note that all of configurtion below is now implemented through serverless framework, and thus

Code Package

scripts/build_docker.sh script will create lambda package in root directory called Base2.Lambdas.zip.
This package is referenced in serverless project as code package.

Handler

Use following entry points (Lambda function handlers)

  • Report generation for EBS - Base2.Lambdas::Base2.Lambdas.Handlers.EBSReportAndCleanup::UploadEBSReport
  • Report generation for AMI - Base2.Lambdas::Base2.Lambdas.Handlers.AMIReportAndCleanup::UploadAMIReport
  • Cleanup from CSV info for EBS - Base2.Lambdas::Base2.Lambdas.Handlers.EBSReportAndCleanup::CleanupFromReport
  • Celanup from CSV info for AMIs - Base2.Lambdas::Base2.Lambdas.Handlers.AMIReportAndCleanup::DeregisterReportedAMIs

IAM Role

Iam role configured for lambda should have following policies

  • read only access to EC2 service
  • write acces to S3 bucket passed in as argument
    1. {
    2. "Version": "2012-10-17",
    3. "Statement": [
    4. {
    5. "Sid": "Stmt1497509441000",
    6. "Effect": "Allow",
    7. "Action": [
    8. "s3:*"
    9. ],
    10. "Resource": [
    11. "arn:aws:s3:::aws.amis-cleanup.reports.example.com/*"
    12. ]
    13. }
    14. ]
    15. }
  • DeleteSnapshot permissions
  1. {
  2. "Version": "2012-10-17",
  3. "Statement": [
  4. {
  5. "Sid": "Stmt1497854974000",
  6. "Effect": "Allow",
  7. "Action": [
  8. "ec2:DeleteSnapshot"
  9. ],
  10. "Resource": [
  11. "*"
  12. ]
  13. }
  14. ]
  15. }
  • Invoke lambda permission, to invoke itself recursively for long running
    deletions
    1. {
    2. "Version": "2012-10-17",
    3. "Statement": [
    4. {
    5. "Sid": "Stmt1497921290000",
    6. "Effect": "Allow",
    7. "Action": [
    8. "lambda:InvokeAsync",
    9. "lambda:InvokeFunction"
    10. ],
    11. "Resource": [
    12. "*"
    13. ]
    14. }
    15. ]
    16. }

Timeout

All of operations can be time consuming, so it’s recommended to set all runtimes to 5 minutes

Runtime

Use C# as runtime

Memory

This functions do not require more than 128MB of memory, even when working with ~10k EBS snapshots (highest tested value)

Other

There is no VPC configuration required

Test / Run

Both report generation and cleanup tasks are accepting location of csv file to write/read
in event parameters. For report generation there is optional parameter OnlyAMIOrphans which default to
true. This parameter determines whether only AMI orphans get reported or ALL EBS snapshots
(danger zone, as you don’t want to delete all snapshots, but you may want to delete some that are not
orphans, thus need for this functionality)

e.g.

  1. {
  2. "BucketName":"aws.amis-cleanup.reports.base2.services",
  3. "Key":"ebs_report_prod.csv",
  4. "OnlyAMIOrphans": true
  5. }