LoginSignup
0
0

More than 1 year has passed since last update.

EC2のLifecyclePolicyでCrossRegionCopyRules設定につまずいた話

Last updated at Posted at 2022-04-13

以下2点について備忘録に残したく不定期ながら投稿していこうと思います

  • Docや過去記事どおりに解決できなかったこと(はまったポイント)
  • 自分の開発のポイント

投稿初回お見苦しい点ありましたらご容赦ください

記事の内容

EC2のライフサイクルポリシーをCloudFormationで作成する際に、公式ドキュメント推奨のTargetではCrossRegionCopyRulesが設定出来ず、非推奨のTargetRegionでdeployした記録です

buildとdeployにsam cliを使いました

ライフサイクルポリシー公式Doc

作業環境

  • Windows10 / WSL2(Ubuntu20.04)
  • aws-cli/1.22.92 Python/3.8.10 Linux/5.13.0-1021-aws botocore/1.24.37
  • SAM CLI, version 1.46.0

やりたかったこと

EC2の運用手間を減らすため初めからAMIバックアップの自動化を試みました

AMIの自動バックアップはData Lifecycle ManagerのポリシータイプEBS-backed AMI ポリシー を選ぶと可能です

動作確認でこちらを参考にさせて頂きました
Amazon Data Lifecycle Manager でクロスアカウントのスナップショットコピーを設定

template.yaml

動作の確認後にAWS公式Docのtemplateを参考にtemplate.yamlを作りました
公式Doc sample template

ここでAWS::DLM::LifecyclePolicy CrossRegionCopyRuleを見るとCrossRegion設定にはTargetを推奨しているようです

Target

The target Region or the Amazon Resource Name (ARN) of the target Outpost for the snapshot copies.
Use this parameter instead of TargetRegion. Do not specify both.

TargetRegion

Avoid using this parameter when creating new policies. Instead, use Target to specify a target Region or a target Outpost for snapshot copies.
For policies created before the Target parameter was introduced, this parameter indicates the target Region for snapshot copies.

推奨に従いTargetでus-east-1を指定します

template.yaml
AWSTemplateFormatVersion: '2010-09-09'
Transform: AWS::Serverless-2016-10-31
Description: >
  SAM Template DLM Cross Reasion seting for EC2_K

# ------------------------------------------------------------#
# Input Parameters
# ------------------------------------------------------------# 
Parameters:
  ProjectName:
    Type: String
    Default: EC2-K
  TargetRegion:
    Type: String
    Default: us-east-1

Resources:
# ------------------------------------------------------------#
# AMI Backup Cycle Policy
# ------------------------------------------------------------#    
  EC2LifiCyclePolicy:
    Type: AWS::DLM::LifecyclePolicy
    Properties:
      Description: Lifecycle Policy using CloudFormation
      State: ENABLED
      ExecutionRoleArn: !Sub arn:aws:iam::${AWS::AccountId}:role/service-role/AWSDataLifecycleManagerDefaultRoleForAMIManagement
      PolicyDetails:
        ResourceTypes:
        - INSTANCE
        PolicyType: IMAGE_MANAGEMENT
        TargetTags:
        - Key: AMI_DLM
          Value: 'on'
        Schedules:
        - Name: !Sub Daily Backup ${ProjectName} AMI
          CreateRule:
            Interval: 24
            IntervalUnit: HOURS
            Times:
            - '17:00'
          RetainRule:
            Count: 7
          CopyTags: true
          CrossRegionCopyRules:
          - Encrypted: True
            Target: !Sub ${TargetRegion}
            RetainRule:
              Interval: 1
              IntervalUnit: DAYS
            CopyTags: True

Deploy

$ sam build
$ sam deploy --guided

するとdeploy中にerrorが発生します

The following parameters(s) are invalid: {Target} (Service: AmazonDLM; Status Code: 400; Error Code: InvalidRequestException; Request ID: 1bbbb8d6-e794-4aef-9002-ccb893ebab6e; Proxy: null)

対策

Targetを非推奨のTargetRegionに変更することでdeployが可能でした(実際には余計な紆余曲折がありましたが割愛します)

変更前
Target: !Sub ${TargetRegion}
変更後
TargetRegion: !Sub ${TargetRegion}

まとめ

CloudFormationでDLMのCrossRegionCopyRuleのregion設定するにはTargetRegionを使う必要がありました(2022.4.14時点)

素直に非推奨も試せばよかったのですが裏読みして少し時間を溶かしたので自戒を込めて記録します

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0