automated-actions/AWS_EBS_VOLUME_LOST/SydneySummitDemo/aws_ebs_vol_lost_importantapp-cfn.yaml [113:199]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                        response = client.describe_images(Owners=['amazon'], Filters=filters)

                        ami = newest_image(response['Images'])
                        responseData = {'ImageId': ami['ImageId']}
                        cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
                  except Exception as e:
                      responseData = {'Error': str(e)}
                      cfnresponse.send(event, context, cfnresponse.FAILED, responseData)

  # Create AMI Snapshot
  CustomCreateSnap:
    Type: Custom::CreateSnap
    Version: 1.0
    Properties:
      ServiceToken: !GetAtt LambdaCreateSnap.Arn
      InstanceId: !Ref EC2Instance
      Region: !Ref 'AWS::Region'
  LambdaCreateSnap:
        Type: "AWS::Lambda::Function"
        Properties:
          Handler: index.lambda_handler
          Role: !GetAtt ExecutionRole.Arn
          Runtime: python3.6
          Timeout: 120
          Code:
            ZipFile: |
              import boto3
              import cfnresponse

              def lambda_handler(event, context):
                  try:
                      if event['RequestType'] == 'Delete':
                        responseData = {'Delete': 'SUCCESS'}
                        cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
                      else:
                        instance = event['ResourceProperties']['InstanceId']
                        region = event['ResourceProperties']['Region']

                        client = boto3.client('ec2', region_name=region)

                        getvolume = client.describe_instances(
                            InstanceIds=[instance],
                        )

                        createami = client.create_image(
                            InstanceId=instance,
                            Name=instance,
                            NoReboot=True
                        )
                        responseData = {'AmiId': createami['ImageId'], 'VolumeId': getvolume['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']}
                        cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
                  except Exception as e:
                      responseData = {'Error': str(e)}
                      cfnresponse.send(event, context, cfnresponse.FAILED, responseData)

  # IAM Execution Role for Lambda Functions
  ExecutionRole:
      Type: "AWS::IAM::Role"
      Properties:
        AssumeRolePolicyDocument:
          Version: 2008-10-17
          Statement:
            -
              Effect: Allow
              Principal:
                Service:
                  - events.amazonaws.com
                  - lambda.amazonaws.com
                  - states.amazonaws.com
              Action:
                - sts:AssumeRole
        ManagedPolicyArns:
          - arn:aws:iam::aws:policy/AdministratorAccess

Outputs:
  EC2Instance:
    Description: EC2 Instance
    Value: !Ref EC2Instance
  EIP:
    Description: Elastic IP
    Value: !Ref EIP0
  VolumeId:
    Description: Volume ID for mock PHD Event
    Value: !GetAtt CustomCreateSnap.VolumeId
  AMI:
    Description: AMI created for backup
    Value: !GetAtt CustomCreateSnap.AmiId
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



automated-actions/AWS_EBS_VOLUME_LOST/stepbystep/step_2_app_reinvent_workshop.yml [158:244]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
                        response = client.describe_images(Owners=['amazon'], Filters=filters)

                        ami = newest_image(response['Images'])
                        responseData = {'ImageId': ami['ImageId']}
                        cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
                  except Exception as e:
                      responseData = {'Error': str(e)}
                      cfnresponse.send(event, context, cfnresponse.FAILED, responseData)

  # Create AMI Snapshot
  CustomCreateSnap:
    Type: Custom::CreateSnap
    Version: 1.0
    Properties:
      ServiceToken: !GetAtt LambdaCreateSnap.Arn
      InstanceId: !Ref EC2Instance
      Region: !Ref 'AWS::Region'
  LambdaCreateSnap:
        Type: "AWS::Lambda::Function"
        Properties:
          Handler: index.lambda_handler
          Role: !GetAtt ExecutionRole.Arn
          Runtime: python3.6
          Timeout: 120
          Code:
            ZipFile: |
              import boto3
              import cfnresponse

              def lambda_handler(event, context):
                  try:
                      if event['RequestType'] == 'Delete':
                        responseData = {'Delete': 'SUCCESS'}
                        cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
                      else:
                        instance = event['ResourceProperties']['InstanceId']
                        region = event['ResourceProperties']['Region']

                        client = boto3.client('ec2', region_name=region)

                        getvolume = client.describe_instances(
                            InstanceIds=[instance],
                        )

                        createami = client.create_image(
                            InstanceId=instance,
                            Name=instance,
                            NoReboot=True
                        )
                        responseData = {'AmiId': createami['ImageId'], 'VolumeId': getvolume['Reservations'][0]['Instances'][0]['BlockDeviceMappings'][0]['Ebs']['VolumeId']}
                        cfnresponse.send(event, context, cfnresponse.SUCCESS, responseData)
                  except Exception as e:
                      responseData = {'Error': str(e)}
                      cfnresponse.send(event, context, cfnresponse.FAILED, responseData)

  # IAM Execution Role for Lambda Functions
  ExecutionRole:
      Type: "AWS::IAM::Role"
      Properties:
        AssumeRolePolicyDocument:
          Version: 2008-10-17
          Statement:
            -
              Effect: Allow
              Principal:
                Service:
                  - events.amazonaws.com
                  - lambda.amazonaws.com
                  - states.amazonaws.com
              Action:
                - sts:AssumeRole
        ManagedPolicyArns:
          - arn:aws:iam::aws:policy/AdministratorAccess

Outputs:
  EC2Instance:
    Description: EC2 Instance
    Value: !Ref EC2Instance
  EIP:
    Description: Elastic IP
    Value: !Ref EIP0
  VolumeId:
    Description: Volume ID for mock PHD Event
    Value: !GetAtt CustomCreateSnap.VolumeId
  AMI:
    Description: AMI created for backup
    Value: !GetAtt CustomCreateSnap.AmiId
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



