簡體   English   中英

使用 Cloudformation 為 EBS 快照創建 Cloudwatch 事件

[英]Create Cloudwatch Event for EBS Snapshot using Cloudformation

我正在嘗試創建 cloudwatch 預定事件以拍攝我的 ebs 快照。 我是 cloudformation 的新手,對它不太熟悉,這就是為什么要實現這一點很復雜。 我正在附加我當前的模板,該模板生成我的 ec2 實例並將默認卷從 10gb 覆蓋到 20gb。 我想在完全相同的已創建卷上創建一個 cloudwatch 事件,以獲取從該模板創建的該卷的快照。 如果有人可以幫助我使用 cloudformation 語法設置目標事件,我會很高興。

Parameters:
  KeyName:
    Description: The EC2 Key Pair to allow SSH access to the instance
    Type: 'AWS::EC2::KeyPair::KeyName'
Resources:
  Ec2Instance:
    Type: 'AWS::EC2::Instance'
    DependsOn:
      - InstanceSecurityGroup
      - CWIAMRole
      - EC2CWInstanceProfile
    Properties:
      KeyName: !Ref KeyName
      ImageId: ami-057a963e8be173b19
      InstanceType: t3a.micro
      IamInstanceProfile: !Ref EC2CWInstanceProfile
      NetworkInterfaces:
        - AssociatePublicIpAddress: 'True'
          DeleteOnTermination: 'True'
          DeviceIndex: '0'
          # Add subnet id below
          SubnetId: subnet-031c6fb8172d780aa
          GroupSet:
            - !Ref InstanceSecurityGroup
      BlockDeviceMappings:
        - DeviceName: /dev/xvda
          Ebs:
            VolumeType: gp2
            DeleteOnTermination: 'true'
            VolumeSize: '20'
  LambdaSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: Enable SSH access via port 22
      # Add you vpc id below
      VpcId: vpc-02e91d5d082e3a097
      GroupName: DS Lambda Security Group
  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    DependsOn:
      - LambdaSecurityGroup
    Properties:
      GroupDescription: Enable SSH access via port 22
      # Add you vpc id below
      VpcId: vpc-02e91d5d082e3a097
      GroupName: DS DB Instance Security Group
      SecurityGroupIngress:
        - IpProtocol: tcp
          FromPort: '22'
          ToPort: '22'
          # Add vpn ip below for e.g 192.168.78.2/32
          CidrIp: 0.0.0.0/0
        - IpProtocol: tcp
          FromPort: '5432'
          ToPort: '5432'
          SourceSecurityGroupId: !Ref LambdaSecurityGroup
  CWIAMRole:
    Type: 'AWS::IAM::Role'
    Properties:
      AssumeRolePolicyDocument:
        Version: 2012-10-17
        Statement:
          - Effect: Allow
            Principal:
              Service:
                - ec2.amazonaws.com
            Action:
              - 'sts:AssumeRole'
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/CloudWatchAgentAdminPolicy'
      RoleName: DS_CW_AGENT_ROLE
  EC2CWInstanceProfile:
    Type: 'AWS::IAM::InstanceProfile'
    Properties:
      InstanceProfileName: EC2CWInstanceProfile
      Roles:
        - !Ref CWIAMRole
  S3VPCEndpoint:
    Type: 'AWS::EC2::VPCEndpoint'
    Properties:
      RouteTableIds:
        - 'rtb-031f3057458433643'
      ServiceName: com.amazonaws.ap-southeast-1.s3
      VpcId: vpc-02e91d5d082e3a097

可悲的是,你不能輕易做到這一點 原因是 Instance 資源沒有返回其根卷的 id。

此外,您無法創建獨立的AWS::EC2::Volume資源並將其用作實例中的根卷。 這僅適用於附加卷。

獲取根設備的卷 ID 的唯一方法是開發自定義資源 這將采用lambda function的形式,它將采用實例 id,並使用 AWS SDK 查找卷 id 並返回到雲結構。 使用該卷 ID,您可以創建 CloudWatch 事件規則。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM