workshops/ec2-auto-scaling-with-multiple-instance-types-and-purchase-options/spot-interruption-handler.yaml [20:88]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Handler: index.handler
      Role: !GetAtt LambdaFunctionRole.Arn
      Code:
        ZipFile: !Sub |
          import boto3

          def handler(event, context):
          
              instanceId = event['detail']['instance-id']
              instanceAction = event['detail']['instance-action']
              
              print("Handling spot instance interruption notification for instance {id}".format(id=instanceId))
              
              # Describe tags for the instance that will be interrupted
              try:
                  ec2client = boto3.client('ec2')
                  describeInstance = ec2client.describe_instances(InstanceIds=[instanceId])
                  describeTags = describeInstance['Reservations'][0]['Instances'][0]
              except Exception as e:
                  print("No action being taken. Unable to describe tags for instance id:", instanceId)
                  print(e)
                  raise
          
              # Check if the instance belongs to ASG
              asg=''
              for tag in describeTags['Tags']:
                  if tag['Key'] =='aws:autoscaling:groupName':
                      asg=tag['Value']
          
              if asg=='':
                  print("No action taken. Instance {id} does not belong to an AutoScaling group.".format(id=instanceId))
                  return
              else:
                  print("Instance {id} belongs to AutoScaling Group {asg_name}. Detaching instance...".format(id=instanceId,asg_name=asg))
                  try:
                      # detach instance from ASG
                      asgclient = boto3.client('autoscaling')
                      response = asgclient.detach_instances(InstanceIds=[instanceId],AutoScalingGroupName=asg,ShouldDecrementDesiredCapacity=False)
                  except Exception as e:
                      print("No action taken. Unable to detach instance {id} from AutoScaling Group {asg_name}".format(id=instanceId,asg_name=asg))
                      print(e)
                      raise

              print(response['Activities'][0]['Cause'])
              return
      Runtime: python3.7

  LambdaFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"
      Policies:
      - PolicyName: lambdaExecution-SpotInterruptHandlerPolicy
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: Allow
            Action:
            - logs:CreateLogStream
            - logs:PutLogEvents
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



workshops/running-amazon-ec2-workloads-at-scale/spot-interruption-handler.yaml [9:77]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
      Handler: index.handler
      Role: !GetAtt LambdaFunctionRole.Arn
      Code:
        ZipFile: !Sub |
          import boto3

          def handler(event, context):
          
              instanceId = event['detail']['instance-id']
              instanceAction = event['detail']['instance-action']
              
              print("Handling spot instance interruption notification for instance {id}".format(id=instanceId))
              
              # Describe tags for the instance that will be interrupted
              try:
                  ec2client = boto3.client('ec2')
                  describeInstance = ec2client.describe_instances(InstanceIds=[instanceId])
                  describeTags = describeInstance['Reservations'][0]['Instances'][0]
              except Exception as e:
                  print("No action being taken. Unable to describe tags for instance id:", instanceId)
                  print(e)
                  raise
          
              # Check if the instance belongs to ASG
              asg=''
              for tag in describeTags['Tags']:
                  if tag['Key'] =='aws:autoscaling:groupName':
                      asg=tag['Value']
          
              if asg=='':
                  print("No action taken. Instance {id} does not belong to an AutoScaling group.".format(id=instanceId))
                  return
              else:
                  print("Instance {id} belongs to AutoScaling Group {asg_name}. Detaching instance...".format(id=instanceId,asg_name=asg))
                  try:
                      # detach instance from ASG
                      asgclient = boto3.client('autoscaling')
                      response = asgclient.detach_instances(InstanceIds=[instanceId],AutoScalingGroupName=asg,ShouldDecrementDesiredCapacity=False)
                  except Exception as e:
                      print("No action taken. Unable to detach instance {id} from AutoScaling Group {asg_name}".format(id=instanceId,asg_name=asg))
                      print(e)
                      raise

              print(response['Activities'][0]['Cause'])
              return
      Runtime: python3.7

  LambdaFunctionRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Version: '2012-10-17'
        Statement:
        - Effect: Allow
          Principal:
            Service:
            - lambda.amazonaws.com
          Action:
          - sts:AssumeRole
      Path: "/"
      Policies:
      - PolicyName: lambdaExecution-SpotInterruptHandlerPolicy
        PolicyDocument:
          Version: '2012-10-17'
          Statement:
          - Effect: Allow
            Action:
            - logs:CreateLogStream
            - logs:PutLogEvents
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



