例: DynamoDB、CloudWatch、SNS - AWS Elastic Beanstalk

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

例: DynamoDB、CloudWatch、SNS

この設定ファイルでは、AWS SDK for PHP 2 を使用する PHP ベースのアプリケーションに対するセッションハンドラとして DynamoDB テーブルを設定します。この例を使用するには、IAM インスタンスプロファイルが必要です。これは、お客様の環境内のインスタンスに追加され、DynamoDB テーブルにアクセスするために使用されます。

このステップで使用するサンプルは、「DynamoDB session Support example」からダウンロードできます。サンプルには以下のファイルが含まれています。

  • サンプルアプリケーション、index.php

  • DynamoDB テーブルおよび他の AWS リソースを設定し、Elastic Beanstalk 環境内のアプリケーションをホストする EC2 インスタンスにソフトウェアをインストールする、設定ファイル、dynamodb.config

  • options.config のデフォルト設定をこの特定のインストールに固有の設定で上書きする、設定ファイル dynamodb.config

index.php

<?php // Include the SDK using the Composer autoloader require '../vendor/autoload.php'; use Aws\DynamoDb\DynamoDbClient; // Grab the session table name and region from the configuration file list($tableName, $region) = file(__DIR__ . '/../sessiontable'); $tableName = rtrim($tableName); $region = rtrim($region); // Create a DynamoDB client and register the table as the session handler $dynamodb = DynamoDbClient::factory(array('region' => $region)); $handler = $dynamodb->registerSessionHandler(array('table_name' => $tableName, 'hash_key' => 'username')); // Grab the instance ID so we can display the EC2 instance that services the request $instanceId = file_get_contents("http://169.254.169.254/latest/meta-data/instance-id"); ?> <h1>Elastic Beanstalk PHP Sessions Sample</h1> <p>This sample application shows the integration of the Elastic Beanstalk PHP container and the session support for DynamoDB from the AWS SDK for PHP 2. Using DynamoDB session support, the application can be scaled out across multiple web servers. For more details, see the <a href="https://aws.amazon.com/php/">PHP Developer Center</a>.</p> <form id="SimpleForm" name="SimpleForm" method="post" action="index.php"> <?php echo 'Request serviced from instance ' . $instanceId . '<br/>'; echo '<br/>'; if (isset($_POST['continue'])) { session_start(); $_SESSION['visits'] = $_SESSION['visits'] + 1; echo 'Welcome back ' . $_SESSION['username'] . '<br/>'; echo 'This is visit number ' . $_SESSION['visits'] . '<br/>'; session_write_close(); echo '<br/>'; echo '<input type="Submit" value="Refresh" name="continue" id="continue"/>'; echo '<input type="Submit" value="Delete Session" name="killsession" id="killsession"/>'; } elseif (isset($_POST['killsession'])) { session_start(); echo 'Goodbye ' . $_SESSION['username'] . '<br/>'; session_destroy(); echo 'Username: <input type="text" name="username" id="username" size="30"/><br/>'; echo '<br/>'; echo '<input type="Submit" value="New Session" name="newsession" id="newsession"/>'; } elseif (isset($_POST['newsession'])) { session_start(); $_SESSION['username'] = $_POST['username']; $_SESSION['visits'] = 1; echo 'Welcome to a new session ' . $_SESSION['username'] . '<br/>'; session_write_close(); echo '<br/>'; echo '<input type="Submit" value="Refresh" name="continue" id="continue"/>'; echo '<input type="Submit" value="Delete Session" name="killsession" id="killsession"/>'; } else { echo 'To get started, enter a username.<br/>'; echo '<br/>'; echo 'Username: <input type="text" name="username" id="username" size="30"/><br/>'; echo '<input type="Submit" value="New Session" name="newsession" id="newsession"/>'; } ?> </form>

.ebextensions/dynamodb.config

Resources: SessionTable: Type: AWS::DynamoDB::Table Properties: KeySchema: HashKeyElement: AttributeName: Fn::GetOptionSetting: OptionName : SessionHashKeyName DefaultValue: "username" AttributeType: Fn::GetOptionSetting: OptionName : SessionHashKeyType DefaultValue: "S" ProvisionedThroughput: ReadCapacityUnits: Fn::GetOptionSetting: OptionName : SessionReadCapacityUnits DefaultValue: 1 WriteCapacityUnits: Fn::GetOptionSetting: OptionName : SessionWriteCapacityUnits DefaultValue: 1 SessionWriteCapacityUnitsLimit: Type: AWS::CloudWatch::Alarm Properties: AlarmDescription: { "Fn::Join" : ["", [{ "Ref" : "AWSEBEnvironmentName" }, " write capacity limit on the session table." ]]} Namespace: "AWS/DynamoDB" MetricName: ConsumedWriteCapacityUnits Dimensions: - Name: TableName Value: { "Ref" : "SessionTable" } Statistic: Sum Period: 300 EvaluationPeriods: 12 Threshold: Fn::GetOptionSetting: OptionName : SessionWriteCapacityUnitsAlarmThreshold DefaultValue: 240 ComparisonOperator: GreaterThanThreshold AlarmActions: - Ref: SessionAlarmTopic InsufficientDataActions: - Ref: SessionAlarmTopic SessionReadCapacityUnitsLimit: Type: AWS::CloudWatch::Alarm Properties: AlarmDescription: { "Fn::Join" : ["", [{ "Ref" : "AWSEBEnvironmentName" }, " read capacity limit on the session table." ]]} Namespace: "AWS/DynamoDB" MetricName: ConsumedReadCapacityUnits Dimensions: - Name: TableName Value: { "Ref" : "SessionTable" } Statistic: Sum Period: 300 EvaluationPeriods: 12 Threshold: Fn::GetOptionSetting: OptionName : SessionReadCapacityUnitsAlarmThreshold DefaultValue: 240 ComparisonOperator: GreaterThanThreshold AlarmActions: - Ref: SessionAlarmTopic InsufficientDataActions: - Ref: SessionAlarmTopic SessionThrottledRequestsAlarm: Type: AWS::CloudWatch::Alarm Properties: AlarmDescription: { "Fn::Join" : ["", [{ "Ref" : "AWSEBEnvironmentName" }, ": requests are being throttled." ]]} Namespace: AWS/DynamoDB MetricName: ThrottledRequests Dimensions: - Name: TableName Value: { "Ref" : "SessionTable" } Statistic: Sum Period: 300 EvaluationPeriods: 1 Threshold: Fn::GetOptionSetting: OptionName: SessionThrottledRequestsThreshold DefaultValue: 1 ComparisonOperator: GreaterThanThreshold AlarmActions: - Ref: SessionAlarmTopic InsufficientDataActions: - Ref: SessionAlarmTopic SessionAlarmTopic: Type: AWS::SNS::Topic Properties: Subscription: - Endpoint: Fn::GetOptionSetting: OptionName: SessionAlarmEmail DefaultValue: "nobody@amazon.com" Protocol: email files: "/var/app/sessiontable": mode: "000444" content: | `{"Ref" : "SessionTable"}` `{"Ref" : "AWS::Region"}` "/var/app/composer.json": mode: "000744" content: { "require": { "aws/aws-sdk-php": "*" } } container_commands: "1-install-composer": command: "cd /var/app; curl -s http://getcomposer.org/installer | php" "2-install-dependencies": command: "cd /var/app; php composer.phar install" "3-cleanup-composer": command: "rm -Rf /var/app/composer.*"

サンプルの設定ファイルでは、最初に DynamoDB テーブルを作成して、主キー構造と、十分なリソースを割り当てて必要なスループットを提供するためのキャパシティーユニットを設定します。次に、WriteCapacity および ReadCapacity に対する CloudWatch アラームを作成します。アラームしきい値を超過した場合にメールを "nobody@amazon.com" に送信する SNS トピックを作成します。

環境の AWS リソースを作成して設定した後、EC2 インスタンスをカスタマイズする必要があります。files キーを使用して DynamoDB テーブルの詳細を環境の EC2 インスタンスに渡し、AWS SDK for PHP 2 の composer.json ファイルに「require」を追加します。最後に、コンテナコマンドを実行して、コンポーザーおよび必要な依存関係をインストールし、インストーラを削除します。

.ebextensions/options.config

option_settings: "aws:elasticbeanstalk:customoption": SessionHashKeyName : username SessionHashKeyType : S SessionReadCapacityUnits : 1 SessionReadCapacityUnitsAlarmThreshold : 240 SessionWriteCapacityUnits : 1 SessionWriteCapacityUnitsAlarmThreshold : 240 SessionThrottledRequestsThreshold : 1 SessionAlarmEmail : me@example.com

SessionAlarmEmail 値をアラーム通知用の E メールに置き換えます。options.config ファイルには、dynamodb.config で定義されている変数の一部に使用する値が含まれます。たとえば、dynamodb.config には以下のような行があります。

Subscription: - Endpoint: Fn::GetOptionSetting: OptionName: SessionAlarmEmail DefaultValue: "nobody@amazon.com"

これらの行は、Elastic Beanstalk に対し、Endpoint プロパティの値を、option_settings セクションと、使用する実際の値を含む名前と値のペアを含む aws:elasticbeanstalk:customoption セクションが含まれている設定ファイル (このサンプルアプリケーションでは options.config) の SessionAlarmEmail の値から取得するように指示します。上の例では、これは SessionAlarmEmail に値 nobody@amazon.com が割り当てられることを意味します。

この例で使用されている CloudFormation リソースの詳細については、以下を参照してください。