├── backup-ec2 ├── __init__.py ├── tests │ ├── __init__.py │ ├── unit │ │ ├── __init__.py │ │ └── test_handler.py │ ├── integration │ │ ├── __init__.py │ │ └── test_api_gateway.py │ └── requirements.txt ├── backup_ec2 │ ├── __init__.py │ ├── requirements.txt │ ├── clf-temp.json │ └── aws_ami_backup_restore.py ├── samconfig.toml ├── .aws-sam │ └── build.toml ├── template.yaml ├── events │ └── event.json ├── .gitignore └── README.md ├── status.json └── README.md /backup-ec2/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backup-ec2/tests/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backup-ec2/backup_ec2/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backup-ec2/tests/unit/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /status.json: -------------------------------------------------------------------------------- 1 | {"status": "uninstall"} -------------------------------------------------------------------------------- /backup-ec2/tests/integration/__init__.py: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /backup-ec2/backup_ec2/requirements.txt: -------------------------------------------------------------------------------- 1 | requests -------------------------------------------------------------------------------- /backup-ec2/tests/requirements.txt: -------------------------------------------------------------------------------- 1 | pytest 2 | boto3 3 | requests 4 | -------------------------------------------------------------------------------- /backup-ec2/samconfig.toml: -------------------------------------------------------------------------------- 1 | version = 0.1 2 | [default] 3 | [default.deploy] 4 | [default.deploy.parameters] 5 | stack_name = "backup-instance" 6 | s3_bucket = "aws-sam-cli-managed-default-samclisourcebucket-1w1re0pfxttaw" 7 | s3_prefix = "backup-instance" 8 | region = "us-east-1" 9 | capabilities = "CAPABILITY_IAM" 10 | image_repositories = [] 11 | parameter_overrides = "ImageId=\"/EC2/AMI_ID\"" 12 | -------------------------------------------------------------------------------- /backup-ec2/.aws-sam/build.toml: -------------------------------------------------------------------------------- 1 | # This file is auto generated by SAM CLI build command 2 | 3 | [function_build_definitions] 4 | [function_build_definitions.2ed2d2a4-20b6-417c-9d7b-ab43133778ce] 5 | codeuri = "C:\\Users\\vahid\\OneDrive\\Documents\\disaster-recovery-main\\backup-ec2\\backup-ec2\\backup_ec2" 6 | runtime = "python3.9" 7 | architecture = "x86_64" 8 | handler = "backup.lambda_handler" 9 | manifest_hash = "" 10 | packagetype = "Zip" 11 | functions = ["DetectFunction"] 12 | 13 | [layer_build_definitions] 14 | -------------------------------------------------------------------------------- /backup-ec2/template.yaml: -------------------------------------------------------------------------------- 1 | AWSTemplateFormatVersion: '2010-09-09' 2 | Transform: AWS::Serverless-2016-10-31 3 | Description: > 4 | backup-ec2 5 | 6 | Sample SAM Template for backup-ec2 7 | 8 | 9 | Globals: 10 | Function: 11 | Timeout: 3 12 | Tracing: Active 13 | Environment: 14 | Variables: 15 | s3_bucket: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region} 16 | Api: 17 | TracingEnabled: True 18 | 19 | Resources: 20 | S3Bucket: 21 | Type: AWS::S3::Bucket 22 | Properties: 23 | BucketName: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region} 24 | DetectFunction: 25 | Type: AWS::Serverless::Function 26 | Properties: 27 | CodeUri: backup_ec2/ 28 | Handler: aws_ami_backup_restore.lambda_handler 29 | Runtime: python3.9 30 | Policies: 31 | - S3FullAccessPolicy: 32 | BucketName: !Sub ${AWS::StackName}-${AWS::AccountId}-${AWS::Region} 33 | - SSMParameterReadPolicy: 34 | ParameterName: EC2/AMI_ID 35 | Architectures: 36 | - x86_64 37 | Events: 38 | S3Event: 39 | Type: S3 40 | Properties: 41 | Bucket: !Ref S3Bucket 42 | Events: s3:ObjectCreated:* 43 | 44 | 45 | 46 | -------------------------------------------------------------------------------- /backup-ec2/backup_ec2/clf-temp.json: -------------------------------------------------------------------------------- 1 | { 2 | "Parameters": { 3 | "ImageId": { 4 | "Type": "AWS::SSM::Parameter::Value", 5 | "Default": "/EC2/AMI_ID" 6 | } 7 | }, 8 | "Resources": { 9 | "JenkinsInstance": { 10 | "Type": "AWS::EC2::Instance", 11 | "Properties": { 12 | "AvailabilityZone": "us-east-1a", 13 | "ImageId": { 14 | "Ref": "ImageId" 15 | }, 16 | "InstanceType": "t2.micro" 17 | } 18 | }, 19 | "AllocEip": { 20 | "Type": "AWS::EC2::EIPAssociation", 21 | "Properties": { 22 | "AllocationId": "eipalloc-0ef82b69034f337e1", 23 | "InstanceId": { 24 | "Ref": "JenkinsInstance" 25 | } 26 | } 27 | } 28 | }, 29 | "Outputs": { 30 | "DemoInstanceId": { 31 | "Description": "Instance Id", 32 | "Value": { 33 | "Ref": "JenkinsInstance" 34 | } 35 | } 36 | } 37 | } -------------------------------------------------------------------------------- /backup-ec2/tests/integration/test_api_gateway.py: -------------------------------------------------------------------------------- 1 | import os 2 | 3 | import boto3 4 | import pytest 5 | import requests 6 | 7 | """ 8 | Make sure env variable AWS_SAM_STACK_NAME exists with the name of the stack we are going to test. 9 | """ 10 | 11 | 12 | class TestApiGateway: 13 | 14 | @pytest.fixture() 15 | def api_gateway_url(self): 16 | """ Get the API Gateway URL from Cloudformation Stack outputs """ 17 | stack_name = os.environ.get("AWS_SAM_STACK_NAME") 18 | 19 | if stack_name is None: 20 | raise ValueError('Please set the AWS_SAM_STACK_NAME environment variable to the name of your stack') 21 | 22 | client = boto3.client("cloudformation") 23 | 24 | try: 25 | response = client.describe_stacks(StackName=stack_name) 26 | except Exception as e: 27 | raise Exception( 28 | f"Cannot find stack {stack_name} \n" f'Please make sure a stack with the name "{stack_name}" exists' 29 | ) from e 30 | 31 | stacks = response["Stacks"] 32 | stack_outputs = stacks[0]["Outputs"] 33 | api_outputs = [output for output in stack_outputs if output["OutputKey"] == "HelloWorldApi"] 34 | 35 | if not api_outputs: 36 | raise KeyError(f"HelloWorldAPI not found in stack {stack_name}") 37 | 38 | return api_outputs[0]["OutputValue"] # Extract url from stack outputs 39 | 40 | def test_api_gateway(self, api_gateway_url): 41 | """ Call the API Gateway endpoint and check the response """ 42 | response = requests.get(api_gateway_url) 43 | 44 | assert response.status_code == 200 45 | assert response.json() == {"message": "hello world"} 46 | -------------------------------------------------------------------------------- /backup-ec2/events/event.json: -------------------------------------------------------------------------------- 1 | { 2 | "body": "{\"message\": \"hello world\"}", 3 | "resource": "/hello", 4 | "path": "/hello", 5 | "httpMethod": "GET", 6 | "isBase64Encoded": false, 7 | "queryStringParameters": { 8 | "foo": "bar" 9 | }, 10 | "pathParameters": { 11 | "proxy": "/path/to/resource" 12 | }, 13 | "stageVariables": { 14 | "baz": "qux" 15 | }, 16 | "headers": { 17 | "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 18 | "Accept-Encoding": "gzip, deflate, sdch", 19 | "Accept-Language": "en-US,en;q=0.8", 20 | "Cache-Control": "max-age=0", 21 | "CloudFront-Forwarded-Proto": "https", 22 | "CloudFront-Is-Desktop-Viewer": "true", 23 | "CloudFront-Is-Mobile-Viewer": "false", 24 | "CloudFront-Is-SmartTV-Viewer": "false", 25 | "CloudFront-Is-Tablet-Viewer": "false", 26 | "CloudFront-Viewer-Country": "US", 27 | "Host": "1234567890.execute-api.us-east-1.amazonaws.com", 28 | "Upgrade-Insecure-Requests": "1", 29 | "User-Agent": "Custom User Agent String", 30 | "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", 31 | "X-Amz-Cf-Id": "cDehVQoZnx43VYQb9j2-nvCh-9z396Uhbp027Y2JvkCPNLmGJHqlaA==", 32 | "X-Forwarded-For": "127.0.0.1, 127.0.0.2", 33 | "X-Forwarded-Port": "443", 34 | "X-Forwarded-Proto": "https" 35 | }, 36 | "requestContext": { 37 | "accountId": "123456789012", 38 | "resourceId": "123456", 39 | "stage": "prod", 40 | "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", 41 | "requestTime": "09/Apr/2015:12:34:56 +0000", 42 | "requestTimeEpoch": 1428582896000, 43 | "identity": { 44 | "cognitoIdentityPoolId": null, 45 | "accountId": null, 46 | "cognitoIdentityId": null, 47 | "caller": null, 48 | "accessKey": null, 49 | "sourceIp": "127.0.0.1", 50 | "cognitoAuthenticationType": null, 51 | "cognitoAuthenticationProvider": null, 52 | "userArn": null, 53 | "userAgent": "Custom User Agent String", 54 | "user": null 55 | }, 56 | "path": "/prod/hello", 57 | "resourcePath": "/hello", 58 | "httpMethod": "POST", 59 | "apiId": "1234567890", 60 | "protocol": "HTTP/1.1" 61 | } 62 | } 63 | -------------------------------------------------------------------------------- /backup-ec2/tests/unit/test_handler.py: -------------------------------------------------------------------------------- 1 | import json 2 | 3 | import pytest 4 | 5 | from hello_world import app 6 | 7 | 8 | @pytest.fixture() 9 | def apigw_event(): 10 | """ Generates API GW Event""" 11 | 12 | return { 13 | "body": '{ "test": "body"}', 14 | "resource": "/{proxy+}", 15 | "requestContext": { 16 | "resourceId": "123456", 17 | "apiId": "1234567890", 18 | "resourcePath": "/{proxy+}", 19 | "httpMethod": "POST", 20 | "requestId": "c6af9ac6-7b61-11e6-9a41-93e8deadbeef", 21 | "accountId": "123456789012", 22 | "identity": { 23 | "apiKey": "", 24 | "userArn": "", 25 | "cognitoAuthenticationType": "", 26 | "caller": "", 27 | "userAgent": "Custom User Agent String", 28 | "user": "", 29 | "cognitoIdentityPoolId": "", 30 | "cognitoIdentityId": "", 31 | "cognitoAuthenticationProvider": "", 32 | "sourceIp": "127.0.0.1", 33 | "accountId": "", 34 | }, 35 | "stage": "prod", 36 | }, 37 | "queryStringParameters": {"foo": "bar"}, 38 | "headers": { 39 | "Via": "1.1 08f323deadbeefa7af34d5feb414ce27.cloudfront.net (CloudFront)", 40 | "Accept-Language": "en-US,en;q=0.8", 41 | "CloudFront-Is-Desktop-Viewer": "true", 42 | "CloudFront-Is-SmartTV-Viewer": "false", 43 | "CloudFront-Is-Mobile-Viewer": "false", 44 | "X-Forwarded-For": "127.0.0.1, 127.0.0.2", 45 | "CloudFront-Viewer-Country": "US", 46 | "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8", 47 | "Upgrade-Insecure-Requests": "1", 48 | "X-Forwarded-Port": "443", 49 | "Host": "1234567890.execute-api.us-east-1.amazonaws.com", 50 | "X-Forwarded-Proto": "https", 51 | "X-Amz-Cf-Id": "aaaaaaaaaae3VYQb9jd-nvCd-de396Uhbp027Y2JvkCPNLmGJHqlaA==", 52 | "CloudFront-Is-Tablet-Viewer": "false", 53 | "Cache-Control": "max-age=0", 54 | "User-Agent": "Custom User Agent String", 55 | "CloudFront-Forwarded-Proto": "https", 56 | "Accept-Encoding": "gzip, deflate, sdch", 57 | }, 58 | "pathParameters": {"proxy": "/examplepath"}, 59 | "httpMethod": "POST", 60 | "stageVariables": {"baz": "qux"}, 61 | "path": "/examplepath", 62 | } 63 | 64 | 65 | def test_lambda_handler(apigw_event): 66 | 67 | ret = app.lambda_handler(apigw_event, "") 68 | data = json.loads(ret["body"]) 69 | 70 | assert ret["statusCode"] == 200 71 | assert "message" in ret["body"] 72 | assert data["message"] == "hello world" 73 | -------------------------------------------------------------------------------- /backup-ec2/.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode 3 | 4 | ### Linux ### 5 | *~ 6 | 7 | # temporary files which can be created if a process still has a handle open of a deleted file 8 | .fuse_hidden* 9 | 10 | # KDE directory preferences 11 | .directory 12 | 13 | # Linux trash folder which might appear on any partition or disk 14 | .Trash-* 15 | 16 | # .nfs files are created when an open file is removed but is still being accessed 17 | .nfs* 18 | 19 | ### OSX ### 20 | *.DS_Store 21 | .AppleDouble 22 | .LSOverride 23 | 24 | # Icon must end with two \r 25 | Icon 26 | 27 | # Thumbnails 28 | ._* 29 | 30 | # Files that might appear in the root of a volume 31 | .DocumentRevisions-V100 32 | .fseventsd 33 | .Spotlight-V100 34 | .TemporaryItems 35 | .Trashes 36 | .VolumeIcon.icns 37 | .com.apple.timemachine.donotpresent 38 | 39 | # Directories potentially created on remote AFP share 40 | .AppleDB 41 | .AppleDesktop 42 | Network Trash Folder 43 | Temporary Items 44 | .apdisk 45 | 46 | ### PyCharm ### 47 | # Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio and Webstorm 48 | # Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839 49 | 50 | # User-specific stuff: 51 | .idea/**/workspace.xml 52 | .idea/**/tasks.xml 53 | .idea/dictionaries 54 | 55 | # Sensitive or high-churn files: 56 | .idea/**/dataSources/ 57 | .idea/**/dataSources.ids 58 | .idea/**/dataSources.xml 59 | .idea/**/dataSources.local.xml 60 | .idea/**/sqlDataSources.xml 61 | .idea/**/dynamic.xml 62 | .idea/**/uiDesigner.xml 63 | 64 | # Gradle: 65 | .idea/**/gradle.xml 66 | .idea/**/libraries 67 | 68 | # CMake 69 | cmake-build-debug/ 70 | 71 | # Mongo Explorer plugin: 72 | .idea/**/mongoSettings.xml 73 | 74 | ## File-based project format: 75 | *.iws 76 | 77 | ## Plugin-specific files: 78 | 79 | # IntelliJ 80 | /out/ 81 | 82 | # mpeltonen/sbt-idea plugin 83 | .idea_modules/ 84 | 85 | # JIRA plugin 86 | atlassian-ide-plugin.xml 87 | 88 | # Cursive Clojure plugin 89 | .idea/replstate.xml 90 | 91 | # Ruby plugin and RubyMine 92 | /.rakeTasks 93 | 94 | # Crashlytics plugin (for Android Studio and IntelliJ) 95 | com_crashlytics_export_strings.xml 96 | crashlytics.properties 97 | crashlytics-build.properties 98 | fabric.properties 99 | 100 | ### PyCharm Patch ### 101 | # Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721 102 | 103 | # *.iml 104 | # modules.xml 105 | # .idea/misc.xml 106 | # *.ipr 107 | 108 | # Sonarlint plugin 109 | .idea/sonarlint 110 | 111 | ### Python ### 112 | # Byte-compiled / optimized / DLL files 113 | __pycache__/ 114 | *.py[cod] 115 | *$py.class 116 | 117 | # C extensions 118 | *.so 119 | 120 | # Distribution / packaging 121 | .Python 122 | build/ 123 | develop-eggs/ 124 | dist/ 125 | downloads/ 126 | eggs/ 127 | .eggs/ 128 | lib/ 129 | lib64/ 130 | parts/ 131 | sdist/ 132 | var/ 133 | wheels/ 134 | *.egg-info/ 135 | .installed.cfg 136 | *.egg 137 | 138 | # PyInstaller 139 | # Usually these files are written by a python script from a template 140 | # before PyInstaller builds the exe, so as to inject date/other infos into it. 141 | *.manifest 142 | *.spec 143 | 144 | # Installer logs 145 | pip-log.txt 146 | pip-delete-this-directory.txt 147 | 148 | # Unit test / coverage reports 149 | htmlcov/ 150 | .tox/ 151 | .coverage 152 | .coverage.* 153 | .cache 154 | .pytest_cache/ 155 | nosetests.xml 156 | coverage.xml 157 | *.cover 158 | .hypothesis/ 159 | 160 | # Translations 161 | *.mo 162 | *.pot 163 | 164 | # Flask stuff: 165 | instance/ 166 | .webassets-cache 167 | 168 | # Scrapy stuff: 169 | .scrapy 170 | 171 | # Sphinx documentation 172 | docs/_build/ 173 | 174 | # PyBuilder 175 | target/ 176 | 177 | # Jupyter Notebook 178 | .ipynb_checkpoints 179 | 180 | # pyenv 181 | .python-version 182 | 183 | # celery beat schedule file 184 | celerybeat-schedule.* 185 | 186 | # SageMath parsed files 187 | *.sage.py 188 | 189 | # Environments 190 | .env 191 | .venv 192 | env/ 193 | venv/ 194 | ENV/ 195 | env.bak/ 196 | venv.bak/ 197 | 198 | # Spyder project settings 199 | .spyderproject 200 | .spyproject 201 | 202 | # Rope project settings 203 | .ropeproject 204 | 205 | # mkdocs documentation 206 | /site 207 | 208 | # mypy 209 | .mypy_cache/ 210 | 211 | ### VisualStudioCode ### 212 | .vscode/* 213 | !.vscode/settings.json 214 | !.vscode/tasks.json 215 | !.vscode/launch.json 216 | !.vscode/extensions.json 217 | .history 218 | 219 | ### Windows ### 220 | # Windows thumbnail cache files 221 | Thumbs.db 222 | ehthumbs.db 223 | ehthumbs_vista.db 224 | 225 | # Folder config file 226 | Desktop.ini 227 | 228 | # Recycle Bin used on file shares 229 | $RECYCLE.BIN/ 230 | 231 | # Windows Installer files 232 | *.cab 233 | *.msi 234 | *.msm 235 | *.msp 236 | 237 | # Windows shortcuts 238 | *.lnk 239 | 240 | # Build folder 241 | 242 | */build/* 243 | 244 | # End of https://www.gitignore.io/api/osx,linux,python,windows,pycharm,visualstudiocode -------------------------------------------------------------------------------- /backup-ec2/README.md: -------------------------------------------------------------------------------- 1 | # backup-ec2 2 | 3 | This project contains source code and supporting files for a serverless application that you can deploy with the SAM CLI. It includes the following files and folders. 4 | 5 | - hello_world - Code for the application's Lambda function. 6 | - events - Invocation events that you can use to invoke the function. 7 | - tests - Unit tests for the application code. 8 | - template.yaml - A template that defines the application's AWS resources. 9 | 10 | The application uses several AWS resources, including Lambda functions and an API Gateway API. These resources are defined in the `template.yaml` file in this project. You can update the template to add AWS resources through the same deployment process that updates your application code. 11 | 12 | If you prefer to use an integrated development environment (IDE) to build and test your application, you can use the AWS Toolkit. 13 | The AWS Toolkit is an open source plug-in for popular IDEs that uses the SAM CLI to build and deploy serverless applications on AWS. The AWS Toolkit also adds a simplified step-through debugging experience for Lambda function code. See the following links to get started. 14 | 15 | * [CLion](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 16 | * [GoLand](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 17 | * [IntelliJ](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 18 | * [WebStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 19 | * [Rider](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 20 | * [PhpStorm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 21 | * [PyCharm](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 22 | * [RubyMine](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 23 | * [DataGrip](https://docs.aws.amazon.com/toolkit-for-jetbrains/latest/userguide/welcome.html) 24 | * [VS Code](https://docs.aws.amazon.com/toolkit-for-vscode/latest/userguide/welcome.html) 25 | * [Visual Studio](https://docs.aws.amazon.com/toolkit-for-visual-studio/latest/user-guide/welcome.html) 26 | 27 | ## Deploy the sample application 28 | 29 | The Serverless Application Model Command Line Interface (SAM CLI) is an extension of the AWS CLI that adds functionality for building and testing Lambda applications. It uses Docker to run your functions in an Amazon Linux environment that matches Lambda. It can also emulate your application's build environment and API. 30 | 31 | To use the SAM CLI, you need the following tools. 32 | 33 | * SAM CLI - [Install the SAM CLI](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-install.html) 34 | * [Python 3 installed](https://www.python.org/downloads/) 35 | * Docker - [Install Docker community edition](https://hub.docker.com/search/?type=edition&offering=community) 36 | 37 | To build and deploy your application for the first time, run the following in your shell: 38 | 39 | ```bash 40 | sam build --use-container 41 | sam deploy --guided 42 | ``` 43 | 44 | The first command will build the source of your application. The second command will package and deploy your application to AWS, with a series of prompts: 45 | 46 | * **Stack Name**: The name of the stack to deploy to CloudFormation. This should be unique to your account and region, and a good starting point would be something matching your project name. 47 | * **AWS Region**: The AWS region you want to deploy your app to. 48 | * **Confirm changes before deploy**: If set to yes, any change sets will be shown to you before execution for manual review. If set to no, the AWS SAM CLI will automatically deploy application changes. 49 | * **Allow SAM CLI IAM role creation**: Many AWS SAM templates, including this example, create AWS IAM roles required for the AWS Lambda function(s) included to access AWS services. By default, these are scoped down to minimum required permissions. To deploy an AWS CloudFormation stack which creates or modifies IAM roles, the `CAPABILITY_IAM` value for `capabilities` must be provided. If permission isn't provided through this prompt, to deploy this example you must explicitly pass `--capabilities CAPABILITY_IAM` to the `sam deploy` command. 50 | * **Save arguments to samconfig.toml**: If set to yes, your choices will be saved to a configuration file inside the project, so that in the future you can just re-run `sam deploy` without parameters to deploy changes to your application. 51 | 52 | You can find your API Gateway Endpoint URL in the output values displayed after deployment. 53 | 54 | ## Use the SAM CLI to build and test locally 55 | 56 | Build your application with the `sam build --use-container` command. 57 | 58 | ```bash 59 | backup-ec2$ sam build --use-container 60 | ``` 61 | 62 | The SAM CLI installs dependencies defined in `hello_world/requirements.txt`, creates a deployment package, and saves it in the `.aws-sam/build` folder. 63 | 64 | Test a single function by invoking it directly with a test event. An event is a JSON document that represents the input that the function receives from the event source. Test events are included in the `events` folder in this project. 65 | 66 | Run functions locally and invoke them with the `sam local invoke` command. 67 | 68 | ```bash 69 | backup-ec2$ sam local invoke HelloWorldFunction --event events/event.json 70 | ``` 71 | 72 | The SAM CLI can also emulate your application's API. Use the `sam local start-api` to run the API locally on port 3000. 73 | 74 | ```bash 75 | backup-ec2$ sam local start-api 76 | backup-ec2$ curl http://localhost:3000/ 77 | ``` 78 | 79 | The SAM CLI reads the application template to determine the API's routes and the functions that they invoke. The `Events` property on each function's definition includes the route and method for each path. 80 | 81 | ```yaml 82 | Events: 83 | HelloWorld: 84 | Type: Api 85 | Properties: 86 | Path: /hello 87 | Method: get 88 | ``` 89 | 90 | ## Add a resource to your application 91 | The application template uses AWS Serverless Application Model (AWS SAM) to define application resources. AWS SAM is an extension of AWS CloudFormation with a simpler syntax for configuring common serverless application resources such as functions, triggers, and APIs. For resources not included in [the SAM specification](https://github.com/awslabs/serverless-application-model/blob/master/versions/2016-10-31.md), you can use standard [AWS CloudFormation](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-template-resource-type-ref.html) resource types. 92 | 93 | ## Fetch, tail, and filter Lambda function logs 94 | 95 | To simplify troubleshooting, SAM CLI has a command called `sam logs`. `sam logs` lets you fetch logs generated by your deployed Lambda function from the command line. In addition to printing the logs on the terminal, this command has several nifty features to help you quickly find the bug. 96 | 97 | `NOTE`: This command works for all AWS Lambda functions; not just the ones you deploy using SAM. 98 | 99 | ```bash 100 | backup-ec2$ sam logs -n HelloWorldFunction --stack-name backup-ec2 --tail 101 | ``` 102 | 103 | You can find more information and examples about filtering Lambda function logs in the [SAM CLI Documentation](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/serverless-sam-cli-logging.html). 104 | 105 | ## Tests 106 | 107 | Tests are defined in the `tests` folder in this project. Use PIP to install the test dependencies and run tests. 108 | 109 | ```bash 110 | backup-ec2$ pip install -r tests/requirements.txt --user 111 | # unit test 112 | backup-ec2$ python -m pytest tests/unit -v 113 | # integration test, requiring deploying the stack first. 114 | # Create the env variable AWS_SAM_STACK_NAME with the name of the stack we are testing 115 | backup-ec2$ AWS_SAM_STACK_NAME= python -m pytest tests/integration -v 116 | ``` 117 | 118 | ## Cleanup 119 | 120 | To delete the sample application that you created, use the AWS CLI. Assuming you used your project name for the stack name, you can run the following: 121 | 122 | ```bash 123 | aws cloudformation delete-stack --stack-name backup-ec2 124 | ``` 125 | 126 | ## Resources 127 | 128 | See the [AWS SAM developer guide](https://docs.aws.amazon.com/serverless-application-model/latest/developerguide/what-is-sam.html) for an introduction to SAM specification, the SAM CLI, and serverless application concepts. 129 | 130 | Next, you can use AWS Serverless Application Repository to deploy ready to use Apps that go beyond hello world samples and learn how authors developed their applications: [AWS Serverless Application Repository main page](https://aws.amazon.com/serverless/serverlessrepo/) 131 | -------------------------------------------------------------------------------- /backup-ec2/backup_ec2/aws_ami_backup_restore.py: -------------------------------------------------------------------------------- 1 | import json 2 | import boto3 3 | from subprocess import Popen, PIPE 4 | import json 5 | import time 6 | import logging 7 | from botocore.exceptions import ClientError 8 | import os 9 | 10 | stack_instance_name="JenkinsInstance" 11 | ssm_parameter="/EC2/AMI_ID" 12 | ssm_parameter_key="ImageId" 13 | stack_name="jenkins-stack" 14 | region="us-east-1" 15 | aminame="jenkins-ami" 16 | timeout=25 17 | cloud_formation_template="clf-temp.json" 18 | s3_bucket_cloudformation="ami-recovery-cloudformation-bucket" 19 | s3_bucket_event="os.environ.get('s3_bucket')" 20 | 21 | 22 | logger = logging.getLogger(f"{stack_name}") 23 | logger.setLevel(logging.INFO) 24 | handler = logging.StreamHandler() 25 | handler.setLevel(logging.INFO) 26 | formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 27 | handler.setFormatter(formatter) 28 | logger.addHandler(handler) 29 | 30 | 31 | class PrepareAmi: 32 | 33 | 34 | 35 | def __init__(self, stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout): 36 | self.stack_instance_name = stack_instance_name 37 | self.ssm_parameter = ssm_parameter 38 | self.stack_name = stack_name 39 | self.region = region 40 | self.aminame = aminame 41 | self.timeout = timeout 42 | 43 | 44 | 45 | def get_parameter_version(self): 46 | ssm = boto3.client('ssm') 47 | parameter = ssm.get_parameter(Name=f"{self.ssm_parameter}", WithDecryption=True) 48 | parameter_response = parameter['Parameter']['Version'] 49 | return parameter_response 50 | 51 | 52 | 53 | def get_old_image_parameters(self,y): 54 | ec2 = boto3.resource('ec2') 55 | for instance in list(ec2.instances.filter(Filters=[{'Name': 'tag:aws:cloudformation:logical-id', 'Values': [f"*{self.stack_instance_name}*"]},{'Name': 'instance-state-name', 'Values': ['running']}])): 56 | def wrapper(*args): 57 | if y == 1: 58 | return instance.id 59 | if y == 2: 60 | return instance.image.id 61 | if y == 3: 62 | return instance.image.name 63 | return wrapper 64 | 65 | 66 | 67 | def create_new_ami(self, instance_id, new_ami_name): 68 | client = boto3.client('ec2', region_name=f"{self.region}") 69 | ami_response = client.create_image(InstanceId=f"{instance_id}",Name=f"{new_ami_name}", NoReboot=True) 70 | return ami_response 71 | 72 | 73 | 74 | def check_state_new_ami(self, new_image_id): 75 | count = 0 76 | while True: 77 | client = boto3.client('ec2', region_name=f"{self.region}") 78 | ami_state_response = client.describe_images(ImageIds=new_image_id) 79 | time.sleep(self.timeout) 80 | print(ami_state_response["Images"][0]["State"]) 81 | if ami_state_response["Images"][0]["State"] == "available": 82 | return "available" 83 | if ami_state_response["Images"][0]["State"] != "available": 84 | count += 1 85 | if count == 15: 86 | logger.error("Connection Timeout Error You can Increase timout") 87 | break 88 | 89 | 90 | class CreateAmi(PrepareAmi): 91 | 92 | 93 | def __init__(self, stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout): 94 | super().__init__(stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout) 95 | 96 | 97 | def create_ami_vars(self): 98 | 99 | ssm_client = boto3.client('ssm', region_name=f"{region}") 100 | ec2_client = boto3.client('ec2', region_name=f"{region}") 101 | clf_client = boto3.client('cloudformation', region_name=f"{region}") 102 | 103 | 104 | 105 | try: 106 | version_new = self.get_parameter_version() + 1 107 | except json.decoder.JSONDecodeError as err: 108 | logger.error("Parameter Not Found Please Fix it") 109 | exit(1) 110 | 111 | 112 | version_old = self.get_parameter_version() 113 | new_ami_name = aminame + f"-{version_new}" 114 | old_ami_name = aminame + f"-{version_old}" 115 | 116 | 117 | @self.get_old_image_parameters(1) 118 | def old_instance_id(): 119 | return "Return Decorator Instace" 120 | print(old_instance_id) 121 | 122 | 123 | @self.get_old_image_parameters(2) 124 | def old_image_id(): 125 | return "Return Decorator Image Variable" 126 | print(old_image_id) 127 | 128 | 129 | @self.get_old_image_parameters(3) 130 | def old_image_name(): 131 | return "Return Decorator Old Image ID" 132 | print(old_image_name) 133 | 134 | 135 | new_ami_image_id = self.create_new_ami(old_instance_id,new_ami_name)["ImageId"] 136 | convert = [] 137 | result = ''.join(map(lambda x: x, new_ami_image_id[0:])) 138 | convert.append(result) 139 | check_new_ami_avaibility = self.check_state_new_ami(convert) 140 | 141 | 142 | if "available" in check_new_ami_avaibility: 143 | 144 | 145 | parameter = ssm_client.put_parameter(Name=f"{self.ssm_parameter}", Overwrite=True, Value=new_ami_image_id) 146 | logger.info("Value of {} is updated to {} version".format(ssm_parameter,parameter["Version"])) 147 | 148 | 149 | 150 | try: 151 | check_resp = ec2_client.describe_images(ImageIds=[old_image_id]) 152 | owner_id = check_resp["Images"][0]["OwnerId"] 153 | print(owner_id) 154 | except ClientError: 155 | print("Invalid Image ID Please use correct image id") 156 | 157 | 158 | ami_response = ec2_client.deregister_image(ImageId=old_image_id, DryRun=False) 159 | print(ami_response) 160 | snapshot_response = ec2_client.describe_snapshots(OwnerIds=[owner_id]) 161 | 162 | 163 | for i in range(len(snapshot_response["Snapshots"])): 164 | 165 | if old_image_id in snapshot_response["Snapshots"][i]["Description"]: 166 | snapid = snapshot_response["Snapshots"][i]["SnapshotId"] 167 | response = ec2_client.delete_snapshot(SnapshotId=snapid) 168 | if response["ResponseMetadata"]["HTTPStatusCode"] == 200: 169 | print(logger.info(f"Backup Successful Created: Deleted Old Image ID and Name - {old_image_id}--{old_ami_name} Created New Image ID and Name - {new_ami_image_id}--{new_ami_name} ")) 170 | 171 | 172 | 173 | response = clf_client.delete_stack(StackName=stack_name) 174 | print(logger.info(f"Stack {stack_name} Successful Deleted")) 175 | 176 | 177 | 178 | 179 | 180 | 181 | 182 | 183 | def lambda_handler(event, context): 184 | 185 | 186 | s3client = boto3.client('s3') 187 | bucket_name = event['Records'][0]['s3']['bucket']['name'] 188 | bucket_key = event['Records'][0]['s3']['object']['key'] 189 | if "status.json" in bucket_key: 190 | print("This is BucketKey {}".format(bucket_key)) 191 | obj = s3client.get_object(Bucket=bucket_name,Key=bucket_key) 192 | checkout = obj['Body'].read().decode('utf8') 193 | status = json.loads(checkout) 194 | 195 | print(status) 196 | 197 | 198 | 199 | if status["status"] == "uninstall": 200 | 201 | 202 | Reference_Class_AmiVars = CreateAmi(stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout) 203 | Reference_Class_AmiVars.create_ami_vars() 204 | 205 | 206 | 207 | if status["status"] == "install": 208 | 209 | 210 | s3 = boto3.client('s3') 211 | 212 | 213 | with open(cloud_formation_template, "rb") as f: 214 | s3.upload_fileobj(f, s3_bucket_cloudformation, cloud_formation_template) 215 | 216 | 217 | os.chdir('/tmp') 218 | 219 | 220 | with open(cloud_formation_template, 'wb') as f: 221 | s3.download_fileobj(s3_bucket_cloudformation, cloud_formation_template, f) 222 | 223 | 224 | 225 | with open(cloud_formation_template, 'r', encoding='utf-8') as content_file: 226 | content = json.load(content_file) 227 | 228 | 229 | content = json.dumps(content) 230 | cloud_formation_client = boto3.client('cloudformation') 231 | 232 | 233 | print(logger.info("Creating {}".format(stack_name))) 234 | response = cloud_formation_client.create_stack( 235 | StackName=stack_name, 236 | TemplateBody=content, 237 | Parameters=[{ 238 | 'ParameterKey': ssm_parameter_key, 239 | 'ParameterValue': ssm_parameter 240 | }] 241 | ) 242 | return response -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # aws-backup-restore-ec2 2 | AWS ec2 backup and recovery automation solution 3 | 4 | Read: If any project gets 500 stars, I will share a step by step video link of the same project. 5 | 6 | 7 | ![architecture](https://user-images.githubusercontent.com/123200995/213859537-c498b95c-3313-485e-b837-e245e7a8f987.PNG) 8 | 9 | I shared how I wrote a lambda boto3 python script for a solution that automates the backup and restore of an ec2 instance. 10 | 11 | Steps: 12 | 13 | 1) Create Your Image 14 | 2) Create your ssm from, store use format string as /EC2/AMI ID and update value your image id 15 | 3) Clone The repo 16 | 4) Open the code and update your variable (aws-backup-restore-ec2/backup-ec2/backup_ec2/aws_ami_backup_restore.py) 17 | > 18 | stack_instance_name="JenkinsInstance" #This is the value obtained from clf-temp.json for the tag filter, you can update it 19 | ssm_parameter="/EC2/AMI_ID" #This is the value obtained from the ssm parameter type string 20 | ssm_parameter_key="ImageId" #This is the parameter key, don't change it 21 | stack_name="jenkins-stack" #This is Just Stack name you can change it 22 | region="us-east-1" 23 | aminame="jenkins-ami" #This value is a ami name referenced from ssm parameter version For ex: jenkins-ami-20 24 | timeout=25 #This value specifies the available pending state ami if the image state is available and then unregisters the old image. 25 | cloud_formation_template="clf-temp.json" #This is cloudformation template for installing and uninstalling stack. You can modify thi file but template should json format 26 | s3_bucket_cloudformation="ami-recovery-cloudformation-bucket" #This is just bucket name you can create your own bucket and change value 27 | s3_bucket_event="os.environ.get('s3_bucket')" 28 | 29 | 5) cd aws-backup-restore-ec2/backup-ec2/backup_ec2 30 | 6) sam build 31 | 7) sam deploy --guided 32 | 8) after deploying sam you have to give permission to lambda function ec2,cloudformation,s3,ssm 33 | 9) upload status.json to lambda s3 bucket you can use 2 status "install" and "uninstall" look code below 34 | > 35 | if status["status"] == "uninstall": 36 | 37 | 38 | Reference_Class_AmiVars = CreateAmi(stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout) 39 | Reference_Class_AmiVars.create_ami_vars() 40 | 41 | 42 | 43 | if status["status"] == "install": 44 | 45 | 46 | s3 = boto3.client('s3') 47 | 48 | 49 | with open(cloud_formation_template, "rb") as f: 50 | s3.upload_fileobj(f, s3_bucket_cloudformation, cloud_formation_template) 51 | 52 | 53 | I used 2 classes and one function for this solution: the first class for preparing the image, the second for creating the image. the function just receives events from s3 and the deploy process also has a function 54 | 55 | > 56 | 57 | class PrepareAmi: 58 | 59 | 60 | 61 | def __init__(self, stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout): 62 | self.stack_instance_name = stack_instance_name 63 | self.ssm_parameter = ssm_parameter 64 | self.stack_name = stack_name 65 | self.region = region 66 | self.aminame = aminame 67 | self.timeout = timeout 68 | 69 | 70 | 71 | def get_parameter_version(self): 72 | ssm = boto3.client('ssm') 73 | parameter = ssm.get_parameter(Name=f"{self.ssm_parameter}", WithDecryption=True) 74 | parameter_response = parameter['Parameter']['Version'] 75 | return parameter_response 76 | 77 | 78 | 79 | def get_old_image_parameters(self,y): 80 | ec2 = boto3.resource('ec2') 81 | for instance in list(ec2.instances.filter(Filters=[{'Name': 'tag:aws:cloudformation:logical-id', 'Values': [f"*{self.stack_instance_name}*"]},{'Name': 'instance-state-name', 'Values': ['running']}])): 82 | def wrapper(*args): 83 | if y == 1: 84 | return instance.id 85 | if y == 2: 86 | return instance.image.id 87 | if y == 3: 88 | return instance.image.name 89 | return wrapper 90 | 91 | 92 | 93 | def create_new_ami(self, instance_id, new_ami_name): 94 | client = boto3.client('ec2', region_name=f"{self.region}") 95 | ami_response = client.create_image(InstanceId=f"{instance_id}",Name=f"{new_ami_name}", NoReboot=True) 96 | return ami_response 97 | 98 | 99 | 100 | def check_state_new_ami(self, new_image_id): 101 | count = 0 102 | while True: 103 | client = boto3.client('ec2', region_name=f"{self.region}") 104 | ami_state_response = client.describe_images(ImageIds=new_image_id) 105 | time.sleep(self.timeout) 106 | print(ami_state_response["Images"][0]["State"]) 107 | if ami_state_response["Images"][0]["State"] == "available": 108 | return "available" 109 | if ami_state_response["Images"][0]["State"] != "available": 110 | count += 1 111 | if count == 15: 112 | logger.error("Connection Timeout Error You can Increase timout") 113 | break 114 | 115 | > 116 | class CreateAmi(PrepareAmi): 117 | 118 | 119 | def __init__(self, stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout): 120 | super().__init__(stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout) 121 | 122 | 123 | def create_ami_vars(self): 124 | 125 | ssm_client = boto3.client('ssm', region_name=f"{region}") 126 | ec2_client = boto3.client('ec2', region_name=f"{region}") 127 | clf_client = boto3.client('cloudformation', region_name=f"{region}") 128 | 129 | 130 | 131 | try: 132 | version_new = self.get_parameter_version() + 1 133 | except json.decoder.JSONDecodeError as err: 134 | logger.error("Parameter Not Found Please Fix it") 135 | exit(1) 136 | 137 | 138 | version_old = self.get_parameter_version() 139 | new_ami_name = aminame + f"-{version_new}" 140 | old_ami_name = aminame + f"-{version_old}" 141 | 142 | 143 | @self.get_old_image_parameters(1) 144 | def old_instance_id(): 145 | return "Return Decorator Instace" 146 | print(old_instance_id) 147 | 148 | 149 | @self.get_old_image_parameters(2) 150 | def old_image_id(): 151 | return "Return Decorator Image Variable" 152 | print(old_image_id) 153 | 154 | 155 | @self.get_old_image_parameters(3) 156 | def old_image_name(): 157 | return "Return Decorator Old Image ID" 158 | print(old_image_name) 159 | 160 | 161 | new_ami_image_id = self.create_new_ami(old_instance_id,new_ami_name)["ImageId"] 162 | convert = [] 163 | result = ''.join(map(lambda x: x, new_ami_image_id[0:])) 164 | convert.append(result) 165 | check_new_ami_avaibility = self.check_state_new_ami(convert) 166 | 167 | 168 | if "available" in check_new_ami_avaibility: 169 | 170 | 171 | parameter = ssm_client.put_parameter(Name=f"{self.ssm_parameter}", Overwrite=True, Value=new_ami_image_id) 172 | logger.info("Value of {} is updated to {} version".format(ssm_parameter,parameter["Version"])) 173 | 174 | 175 | 176 | try: 177 | check_resp = ec2_client.describe_images(ImageIds=[old_image_id]) 178 | owner_id = check_resp["Images"][0]["OwnerId"] 179 | print(owner_id) 180 | except ClientError: 181 | print("Invalid Image ID Please use correct image id") 182 | 183 | 184 | ami_response = ec2_client.deregister_image(ImageId=old_image_id, DryRun=False) 185 | print(ami_response) 186 | snapshot_response = ec2_client.describe_snapshots(OwnerIds=[owner_id]) 187 | 188 | 189 | for i in range(len(snapshot_response["Snapshots"])): 190 | 191 | if old_image_id in snapshot_response["Snapshots"][i]["Description"]: 192 | snapid = snapshot_response["Snapshots"][i]["SnapshotId"] 193 | response = ec2_client.delete_snapshot(SnapshotId=snapid) 194 | if response["ResponseMetadata"]["HTTPStatusCode"] == 200: 195 | print(logger.info(f"Backup Successful Created: Deleted Old Image ID and Name - {old_image_id}--{old_ami_name} Created New Image ID and Name - {new_ami_image_id}--{new_ami_name} ")) 196 | 197 | 198 | 199 | response = clf_client.delete_stack(StackName=stack_name) 200 | print(logger.info(f"Stack {stack_name} Successful Deleted")) 201 | 202 | 203 | > 204 | 205 | def lambda_handler(event, context): 206 | 207 | 208 | s3client = boto3.client('s3') 209 | bucket_name = event['Records'][0]['s3']['bucket']['name'] 210 | bucket_key = event['Records'][0]['s3']['object']['key'] 211 | if "status.json" in bucket_key: 212 | print("This is BucketKey {}".format(bucket_key)) 213 | obj = s3client.get_object(Bucket=bucket_name,Key=bucket_key) 214 | checkout = obj['Body'].read().decode('utf8') 215 | status = json.loads(checkout) 216 | 217 | print(status) 218 | 219 | 220 | 221 | if status["status"] == "uninstall": 222 | 223 | 224 | Reference_Class_AmiVars = CreateAmi(stack_instance_name, ssm_parameter, stack_name, region, aminame, timeout) 225 | Reference_Class_AmiVars.create_ami_vars() 226 | 227 | 228 | 229 | if status["status"] == "install": 230 | 231 | 232 | s3 = boto3.client('s3') 233 | 234 | 235 | with open(cloud_formation_template, "rb") as f: 236 | s3.upload_fileobj(f, s3_bucket_cloudformation, cloud_formation_template) 237 | 238 | 239 | os.chdir('/tmp') 240 | 241 | 242 | with open(cloud_formation_template, 'wb') as f: 243 | s3.download_fileobj(s3_bucket_cloudformation, cloud_formation_template, f) 244 | 245 | 246 | 247 | with open(cloud_formation_template, 'r', encoding='utf-8') as content_file: 248 | content = json.load(content_file) 249 | 250 | 251 | content = json.dumps(content) 252 | cloud_formation_client = boto3.client('cloudformation') 253 | 254 | 255 | print(logger.info("Creating {}".format(stack_name))) 256 | response = cloud_formation_client.create_stack( 257 | StackName=stack_name, 258 | TemplateBody=content, 259 | Parameters=[{ 260 | 'ParameterKey': ssm_parameter_key, 261 | 'ParameterValue': ssm_parameter 262 | }] 263 | ) 264 | return response 265 | --------------------------------------------------------------------------------