

Aviso de fin de soporte: el 20 de mayo de 2026, AWS finalizará el soporte para AWS IoT Events. Después del 20 de mayo de 2026, ya no podrás acceder a la AWS IoT Events consola ni a AWS IoT Events los recursos. Para obtener más información, consulta [AWS IoT Events el fin del soporte](https://docs.aws.amazon.com/iotevents/latest/developerguide/iotevents-end-of-support.html).

Las traducciones son generadas a través de traducción automática. En caso de conflicto entre la traducción y la version original de inglés, prevalecerá la version en inglés.

# Creación de una función Lambda en AWS IoT Events
<a name="alarms-create-lambda"></a>

AWS IoT Events proporciona una función Lambda que permite a las alarmas enviar y recibir notificaciones por correo electrónico y SMS.

## Requisitos
<a name="alarms-lambda-requirements"></a>

Se imponen los siguientes requisitos al crear una función de Lambda para las alarmas:
+ Si la alarma envía notificaciones por SMS, asegúrese de que Amazon SNS esté configurado para entregar mensajes SMS.
  + Para obtener más información, consulte la siguiente documentación sobre :
    + [Mensajes de texto móviles con Amazon SNS](https://docs.aws.amazon.com/sns/latest/dg/sns-mobile-phone-number-as-subscriber.html) e [identidades de origen para los mensajes SMS de Amazon SNS en la guía para desarrolladores de *Amazon Simple* Notification](https://docs.aws.amazon.com/sns/latest/dg/channels-sms-originating-identities.html) Service.
    + [¿Qué es la mensajería SMS para usuarios finales de AWS?](https://docs.aws.amazon.com/sms-voice/latest/userguide/what-is-sms-mms.html) en la *Guía AWS SMS del usuario*.
+ Si la alarma envía notificaciones por correo electrónico o SMS, debe tener una función de IAM que le permita AWS Lambda trabajar con Amazon SES y Amazon SNS.

  

  Ejemplo de política:

------
#### [ JSON ]

****  

  ```
  {
      "Version":"2012-10-17",		 	 	 
      "Statement": [
          {
              "Effect": "Allow",
              "Action": [
                  "ses:GetIdentityVerificationAttributes",
                  "ses:SendEmail",
                  "ses:VerifyEmailIdentity"
              ],
              "Resource": "*"
          },
          {
              "Effect": "Allow",
              "Action": [
                  "sns:Publish",
                  "sns:OptInPhoneNumber",
                  "sns:CheckIfPhoneNumberIsOptedOut",
                  "sms-voice:DescribeOptedOutNumbers"
              ],
              "Resource": "*"
          },
          {
              "Effect": "Deny",
              "Action": "sns:Publish",
              "Resource": "arn:aws:sns:*:*:*"
          },
          {
            "Effect" : "Allow",
            "Action" : [
              "logs:CreateLogGroup",
              "logs:CreateLogStream",
              "logs:PutLogEvents"
            ],
            "Resource" : "*"
          }
      ]
  }
  ```

------
+ Debe elegir la misma AWS región para ambos AWS IoT Events tipos y. AWS Lambda Para obtener la lista de regiones admitidas, consulte [Puntos de conexión y cuotas de AWS IoT Events](https://docs.aws.amazon.com/general/latest/gr/iot-events.html) y [Puntos de conexión y cuotas de AWS Lambda](https://docs.aws.amazon.com/general/latest/gr/lambda-service.html) en *Referencia general de Amazon Web Services*.

# Implementar una función Lambda para usar AWS IoT Events CloudFormation
<a name="alarms-create-lambda-cfn"></a>

En este tutorial se utiliza una CloudFormation plantilla para implementar una función Lambda. Esta plantilla crea automáticamente un rol de IAM que permite a la función de Lambda trabajar con Amazon SES y Amazon SNS.

A continuación, se muestra cómo usar el AWS Command Line Interface (AWS CLI) para crear una CloudFormation pila.

1. <a name="install-cli"></a>En la terminal de tu dispositivo, ejecuta `aws --version` para comprobar si has instalado el AWS CLI. Para obtener más información, consulte [Instalación o actualización de la versión más reciente de la AWS CLI](https://docs.aws.amazon.com/cli/latest/userguide/getting-started-install.html) en la *Guía del usuario de AWS Command Line Interface *.

1. <a name="configure-cli"></a>Ejecute `aws configure list` para comprobar si ha configurado AWS CLI la AWS región que tiene todos sus AWS recursos para este tutorial. Para obtener más información, consulte [Establecer y ver los ajustes de configuración mediante comandos](https://docs.aws.amazon.com/cli/latest/userguide/cli-configure-files.html#cli-configure-files-methods) en la *Guía del AWS Command Line Interface usuario*

1. Descargue la CloudFormation plantilla [NotificationLambda.Template.yaml.zip](samples/notificationLambda.template.yaml.zip).
**nota**  
Si encuentra dificultades para descargar el archivo, la plantilla también está disponible en [CloudFormation plantilla](alarms-create-lambda.md#cfn-template).

1. Descomprima el contenido y guárdelo localmente como `notificationLambda.template.yaml`.

1. Abra un terminal en su dispositivo y vaya hasta el directorio en que descargó el archivo `notificationLambda.template.yaml`.

1. Para crear una pila CloudFormation , ejecuta el siguiente comando:

   ```
   aws cloudformation create-stack --stack-name notificationLambda-stack --template-body file://notificationLambda.template.yaml --capabilities CAPABILITY_IAM
   ```

Puede modificar esta CloudFormation plantilla para personalizar la función Lambda y su comportamiento.

**nota**  
AWS Lambda Reintenta los errores de función dos veces. Si la función no tiene capacidad suficiente para gestionar todas las solicitudes entrantes, los eventos podrían esperar en la cola durante horas o días para su envío a la función. Puede configurar una cola de mensajes no entregados (DLQ) en la función para capturar los eventos que no se procesaron correctamente. Para obtener más información, consulte [Invocación asíncrona](https://docs.aws.amazon.com/lambda/latest/dg/invocation-async.html) en la *Guía para desarrolladores de AWS Lambda *.

También puede crear o configurar la pila en la CloudFormation consola. Para obtener más información, consulte [Trabajo con pilas](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/stacks.html), en la *Guía del usuario de AWS CloudFormation *.

# Creación de una función Lambda personalizada para AWS IoT Events
<a name="alarms-create-custom-lambda"></a>

Puede crear una función de Lambda o modificar la proporcionada con AWS IoT Events.

Se imponen los siguientes requisitos al crear una función de Lambda personalizada.
+ Agregue permisos que permitan a la función Lambda realizar acciones específicas y acceder AWS a los recursos.
+ Si usa la función Lambda proporcionada por AWS IoT Events, asegúrese de elegir el motor de ejecución de Python 3.7.

Ejemplo de función de Lambda:

```
import boto3
import json
import logging
import datetime
logger = logging.getLogger()
logger.setLevel(logging.INFO)
ses = boto3.client('ses')
sns = boto3.client('sns')
def check_value(target):
  if target:
    return True
  return False

# Check whether email is verified. Only verified emails are allowed to send emails to or from.
def check_email(email):
  if not check_value(email):
    return False
  result = ses.get_identity_verification_attributes(Identities=[email])
  attr = result['VerificationAttributes']
  if (email not in attr or attr[email]['VerificationStatus'] != 'Success'):
      logging.info('Verification email for {} sent. You must have all the emails verified before sending email.'.format(email))
      ses.verify_email_identity(EmailAddress=email)
      return False
  return True

# Check whether the phone holder has opted out of receiving SMS messages from your account
def check_phone_number(phone_number):
  try:
    result = sns.check_if_phone_number_is_opted_out(phoneNumber=phone_number)
    if (result['isOptedOut']):
        logger.info('phoneNumber {} is not opt in of receiving SMS messages. Phone number must be opt in first.'.format(phone_number))
        return False
    return True
  except Exception as e:
    logging.error('Your phone number {} must be in E.164 format in SSO. Exception thrown: {}'.format(phone_number, e))
    return False

def check_emails(emails):
  result = True
  for email in emails:
      if not check_email(email):
          result = False
  return result

def lambda_handler(event, context):
  logging.info('Received event: ' + json.dumps(event))
  nep = json.loads(event.get('notificationEventPayload'))
  alarm_state = nep['alarmState']
  default_msg = 'Alarm ' + alarm_state['stateName'] + '\n'
  timestamp = datetime.datetime.utcfromtimestamp(float(nep['stateUpdateTime'])/1000).strftime('%Y-%m-%d %H:%M:%S')
  alarm_msg = "{} {} {} at {} UTC ".format(nep['alarmModelName'], nep.get('keyValue', 'Singleton'), alarm_state['stateName'], timestamp)
  default_msg += 'Sev: ' + str(nep['severity']) + '\n'
  if (alarm_state['ruleEvaluation']):
    property = alarm_state['ruleEvaluation']['simpleRule']['inputProperty']
    default_msg += 'Current Value: ' + str(property) + '\n'
    operator = alarm_state['ruleEvaluation']['simpleRule']['operator']
    threshold = alarm_state['ruleEvaluation']['simpleRule']['threshold']
    alarm_msg += '({} {} {})'.format(str(property), operator, str(threshold))
  default_msg += alarm_msg + '\n'

  emails = event.get('emailConfigurations', [])
  logger.info('Start Sending Emails')
  for email in emails:
    from_adr = email.get('from')
    to_adrs = email.get('to', [])
    cc_adrs = email.get('cc', [])
    bcc_adrs = email.get('bcc', [])
    msg = default_msg + '\n' + email.get('additionalMessage', '')
    subject = email.get('subject', alarm_msg)
    fa_ver = check_email(from_adr)
    tas_ver = check_emails(to_adrs)
    ccas_ver = check_emails(cc_adrs)
    bccas_ver = check_emails(bcc_adrs)
    if (fa_ver and tas_ver and ccas_ver and bccas_ver):
      ses.send_email(Source=from_adr,
                     Destination={'ToAddresses': to_adrs, 'CcAddresses': cc_adrs, 'BccAddresses': bcc_adrs},
                     Message={'Subject': {'Data': subject}, 'Body': {'Text': {'Data': msg}}})
      logger.info('Emails have been sent')

  logger.info('Start Sending SNS message to SMS')
  sns_configs = event.get('smsConfigurations', [])
  for sns_config in sns_configs:
    sns_msg = default_msg + '\n' + sns_config.get('additionalMessage', '')
    phone_numbers = sns_config.get('phoneNumbers', [])
    sender_id = sns_config.get('senderId')
    for phone_number in phone_numbers:
        if check_phone_number(phone_number):
          if check_value(sender_id):
            sns.publish(PhoneNumber=phone_number, Message=sns_msg, MessageAttributes={'AWS.SNS.SMS.SenderID':{'DataType': 'String','StringValue': sender_id}})
          else:
            sns.publish(PhoneNumber=phone_number, Message=sns_msg)
          logger.info('SNS messages have been sent')
```

Para obtener más información, consulte [¿Qué es AWS Lambda?](https://docs.aws.amazon.com/lambda/latest/dg/welcome.html) en la *Guía para desarrolladores de AWS Lambda *.

## CloudFormation plantilla
<a name="cfn-template"></a>

Utilice la siguiente CloudFormation plantilla para crear la función Lambda.

```
AWSTemplateFormatVersion: '2010-09-09'
Description: 'Notification Lambda for Alarm Model'
Resources:
  NotificationLambdaRole:
    Type: AWS::IAM::Role
    Properties:
      AssumeRolePolicyDocument:
        Statement:
          - Effect: Allow
            Principal:
              Service: lambda.amazonaws.com
            Action: sts:AssumeRole
      Path: "/"
      ManagedPolicyArns:
        - 'arn:aws:iam::aws:policy/AWSLambdaExecute'
      Policies:
        - PolicyName: "NotificationLambda"
          PolicyDocument:
            Version: "2012-10-17"		 	 	 
            Statement:
              - Effect: "Allow"
                Action:
                  - "ses:GetIdentityVerificationAttributes"
                  - "ses:SendEmail"
                  - "ses:VerifyEmailIdentity"
                Resource: "*"
              - Effect: "Allow"
                Action:
                  - "sns:Publish"
                  - "sns:OptInPhoneNumber"
                  - "sns:CheckIfPhoneNumberIsOptedOut"
                  - "sms-voice:DescribeOptedOutNumbers"
                Resource: "*"
              - Effect: "Deny"
                Action:
                  - "sns:Publish"
                Resource: "arn:aws:sns:*:*:*"
  NotificationLambdaFunction:              
    Type: AWS::Lambda::Function
    Properties:
      Role: !GetAtt NotificationLambdaRole.Arn
      Runtime: python3.7
      Handler: index.lambda_handler
      Timeout: 300
      MemorySize: 3008
      Code:
        ZipFile: |
          import boto3
          import json
          import logging
          import datetime
          logger = logging.getLogger()
          logger.setLevel(logging.INFO)
          ses = boto3.client('ses')
          sns = boto3.client('sns')
          def check_value(target):
            if target:
              return True
            return False

          # Check whether email is verified. Only verified emails are allowed to send emails to or from.
          def check_email(email):
            if not check_value(email):
              return False
            result = ses.get_identity_verification_attributes(Identities=[email])
            attr = result['VerificationAttributes']
            if (email not in attr or attr[email]['VerificationStatus'] != 'Success'):
                logging.info('Verification email for {} sent. You must have all the emails verified before sending email.'.format(email))
                ses.verify_email_identity(EmailAddress=email)
                return False
            return True

          # Check whether the phone holder has opted out of receiving SMS messages from your account
          def check_phone_number(phone_number):
            try:
              result = sns.check_if_phone_number_is_opted_out(phoneNumber=phone_number)
              if (result['isOptedOut']):
                  logger.info('phoneNumber {} is not opt in of receiving SMS messages. Phone number must be opt in first.'.format(phone_number))
                  return False
              return True
            except Exception as e:
              logging.error('Your phone number {} must be in E.164 format in SSO. Exception thrown: {}'.format(phone_number, e))
              return False

          def check_emails(emails):
            result = True
            for email in emails:
                if not check_email(email):
                    result = False
            return result

          def lambda_handler(event, context):
            logging.info('Received event: ' + json.dumps(event))
            nep = json.loads(event.get('notificationEventPayload'))
            alarm_state = nep['alarmState']
            default_msg = 'Alarm ' + alarm_state['stateName'] + '\n'
            timestamp = datetime.datetime.utcfromtimestamp(float(nep['stateUpdateTime'])/1000).strftime('%Y-%m-%d %H:%M:%S')
            alarm_msg = "{} {} {} at {} UTC ".format(nep['alarmModelName'], nep.get('keyValue', 'Singleton'), alarm_state['stateName'], timestamp)
            default_msg += 'Sev: ' + str(nep['severity']) + '\n'
            if (alarm_state['ruleEvaluation']):
              property = alarm_state['ruleEvaluation']['simpleRule']['inputProperty']
              default_msg += 'Current Value: ' + str(property) + '\n'
              operator = alarm_state['ruleEvaluation']['simpleRule']['operator']
              threshold = alarm_state['ruleEvaluation']['simpleRule']['threshold']
              alarm_msg += '({} {} {})'.format(str(property), operator, str(threshold))
            default_msg += alarm_msg + '\n'

            emails = event.get('emailConfigurations', [])
            logger.info('Start Sending Emails')
            for email in emails:
              from_adr = email.get('from')
              to_adrs = email.get('to', [])
              cc_adrs = email.get('cc', [])
              bcc_adrs = email.get('bcc', [])
              msg = default_msg + '\n' + email.get('additionalMessage', '')
              subject = email.get('subject', alarm_msg)
              fa_ver = check_email(from_adr)
              tas_ver = check_emails(to_adrs)
              ccas_ver = check_emails(cc_adrs)
              bccas_ver = check_emails(bcc_adrs)
              if (fa_ver and tas_ver and ccas_ver and bccas_ver):
                ses.send_email(Source=from_adr,
                               Destination={'ToAddresses': to_adrs, 'CcAddresses': cc_adrs, 'BccAddresses': bcc_adrs},
                               Message={'Subject': {'Data': subject}, 'Body': {'Text': {'Data': msg}}})
                logger.info('Emails have been sent')

            logger.info('Start Sending SNS message to SMS')
            sns_configs = event.get('smsConfigurations', [])
            for sns_config in sns_configs:
              sns_msg = default_msg + '\n' + sns_config.get('additionalMessage', '')
              phone_numbers = sns_config.get('phoneNumbers', [])
              sender_id = sns_config.get('senderId')
              for phone_number in phone_numbers:
                  if check_phone_number(phone_number):
                    if check_value(sender_id):
                      sns.publish(PhoneNumber=phone_number, Message=sns_msg, MessageAttributes={'AWS.SNS.SMS.SenderID':{'DataType': 'String','StringValue': sender_id}})
                    else:
                      sns.publish(PhoneNumber=phone_number, Message=sns_msg)
                    logger.info('SNS messages have been sent')
```