インスタンスの Amazon Machine Image (AMI) の作成は、バックアップと復旧で使用される一般的なプロセスです。災害対策アーキテクチャの一部として、AMI を別の AWS リージョン にコピーすることも選択できます。一般的なメンテナンスタスクを自動化することで、フェイルオーバーが必要な問題が発生した場合にダウンタイムを短縮できます。AWS Systems Managerオートメーションアクションが、これを実現するのに役立ちます。Automation は AWS Systems Manager のツールです。
次の AWS Systems Manager ランブックの例では、これらのアクションを実行します。
-
aws:executeAwsApi
オートメーションアクションを使用して、AMI を作成します。 -
aws:waitForAwsResourceProperty
オートメーションアクションを使用して、AMI の可用性を確認します。 -
aws:executeScript
オートメーションアクションを使用して、AMI を送信先リージョンにコピーします。
---
description: Custom Automation Backup and Recovery Example
schemaVersion: '0.3'
assumeRole: "{{ AutomationAssumeRole }}"
parameters:
AutomationAssumeRole:
type: String
description: "(Required) The ARN of the role that allows Automation to perform
the actions on your behalf. If no role is specified, Systems Manager Automation
uses your IAM permissions to use this runbook."
default: ''
InstanceId:
type: String
description: "(Required) The ID of the EC2 instance."
default: ''
mainSteps:
- name: createImage
action: aws:executeAwsApi
onFailure: Abort
inputs:
Service: ec2
Api: CreateImage
InstanceId: "{{ InstanceId }}"
Name: "Automation Image for {{ InstanceId }}"
NoReboot: false
outputs:
- Name: newImageId
Selector: "$.ImageId"
Type: String
nextStep: verifyImageAvailability
- name: verifyImageAvailability
action: aws:waitForAwsResourceProperty
timeoutSeconds: 600
inputs:
Service: ec2
Api: DescribeImages
ImageIds:
- "{{ createImage.newImageId }}"
PropertySelector: "$.Images[0].State"
DesiredValues:
- available
nextStep: copyImage
- name: copyImage
action: aws:executeScript
timeoutSeconds: 45
onFailure: Abort
inputs:
Runtime: python3.8
Handler: crossRegionImageCopy
InputPayload:
newImageId : "{{ createImage.newImageId }}"
Script: |-
def crossRegionImageCopy(events,context):
import boto3
#Initialize client
ec2 = boto3.client('ec2', region_name='us-east-1')
newImageId = events['newImageId']
ec2.copy_image(
Name='DR Copy for ' + newImageId,
SourceImageId=newImageId,
SourceRegion='us-west-2'
)