

本文為英文版的機器翻譯版本，如內容有任何歧義或不一致之處，概以英文版為準。

# 範例：SQS、CloudWatch 和 SNS
<a name="customize-environment-resources-sqs"></a>

此範例將 Amazon SQS 佇列和佇列深度的警示新增到環境。在此範例中所顯示的屬性，是您必須針對這些資源的各個設定的最低要求屬性。您可從 [SQS、SNS 和 CloudWatch](https://elasticbeanstalk.s3.amazonaws.com/extensions/SNS.config) 下載此範例。

**注意**  
此範例會建立您可能需要付費 AWS 的資源。如需 AWS 定價的詳細資訊，請參閱 [https://aws.amazon.com/pricing/](https://aws.amazon.com/pricing/)。有些服務是 AWS 免費用量方案的一部分。若您是新客戶，可以免費試用這些服務。如需詳細資訊，請參閱[https://aws.amazon.com/free/](https://aws.amazon.com/free/)。

要使用此範例，請依照下列項目：

1. 在原始碼套件的最上層目錄建立 `[.ebextensions](ebextensions.md)` 目錄。

1. 以 `.config` 延伸建立兩個組態檔案，並且置於 `.ebextensions` 目錄。一個組態檔案定義資源，另一個組態檔案定義選項。

1. 將您的應用程式部署至 Elastic Beanstalk。

   YAML 憑藉一致的縮排。請在取代範例組態檔中的內容時，讓縮排層級一致，並確認您的文字編輯器使用空格而非定位字元進行縮排。

建立組態檔案 (例如 sqs.config)，此檔案是用來定義資源。在此範例中，我們會建立 SQS 佇列，並定義 `VisbilityTimeout` 資源中的 `MySQSQueue` 屬性。然後，我們會建立 SNS `Topic`，並指定在警示觸發時，傳送電子郵件到 `someone@example.com`。最後，我們會建立 CloudWatch 警示，此警示會在佇列增加到超過 10 筆訊息時觸發。在 `Dimensions` 屬性中，我們會指定維度的名稱，以及代表維度測量值的值。我們會使用 `Fn::GetAtt`，來從 `QueueName` 傳回 `MySQSQueue` 的值。

```
#This sample requires you to create a separate configuration file to define the custom options for the SNS topic and SQS queue.
Resources:
  MySQSQueue:
    Type: AWS::SQS::Queue
    Properties: 
      VisibilityTimeout:
        Fn::GetOptionSetting:
          OptionName: VisibilityTimeout
          DefaultValue: 30
  AlarmTopic:
    Type: AWS::SNS::Topic
    Properties: 
      Subscription:
        - Endpoint:
            Fn::GetOptionSetting:
              OptionName: AlarmEmail
              DefaultValue: "nobody@amazon.com"
          Protocol: email
  QueueDepthAlarm:
    Type: AWS::CloudWatch::Alarm
    Properties:
      AlarmDescription: "Alarm if queue depth grows beyond 10 messages"
      Namespace: "AWS/SQS"
      MetricName: ApproximateNumberOfMessagesVisible
      Dimensions:
        - Name: QueueName
          Value : { "Fn::GetAtt" : [ "MySQSQueue", "QueueName"] }
      Statistic: Sum
      Period: 300
      EvaluationPeriods: 1
      Threshold: 10
      ComparisonOperator: GreaterThanThreshold
      AlarmActions:
        - Ref: AlarmTopic
      InsufficientDataActions:
        - Ref: AlarmTopic

Outputs :
  QueueURL: 
    Description : "URL of newly created SQS Queue"
    Value : { Ref : "MySQSQueue" }
  QueueARN :
    Description : "ARN of newly created SQS Queue"
    Value : { "Fn::GetAtt" : [ "MySQSQueue", "Arn"]}
  QueueName :
    Description : "Name newly created SQS Queue"
    Value : { "Fn::GetAtt" : [ "MySQSQueue", "QueueName"]}
```

如需本範例組態檔案所使用的資源的詳細資訊，請參閱下列參考：
+ [AWS::SQS::Queue](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sqs-queues.html)
+ [AWS::SNS::Topic](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-sns-topic.html)
+ [AWS::CloudWatch::Alarm](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-cw-alarm.html)

建立另一個名為 `options.config` 的組態檔案，並定義自訂選項設定。

```
option_settings:
  "aws:elasticbeanstalk:customoption":
     VisibilityTimeout : 30
     AlarmEmail : "nobody@example.com"
```

這些行會指示 Elastic Beanstalk 從組態檔案 （範例中的 options.config) 中的 **VisibilityTimeout ** 和訂閱端點值取得 VisibilityTimeout 和訂閱端點屬性的值，該組態檔案包含具有 **aws：elasticbeanstalk：customoption** 區段的 option\_settings 區段，其中包含包含要使用的實際值的名稱值對。 **VisibilityTimeout ** 在上列的範例中，這表示將會使用 30 和「nobody@amazon.com」來做為值。如需有關 `Fn::GetOptionSetting` 的詳細資訊，請參閱 [函數](ebextensions-functions.md)。