AWS Official Blog

AWS Elastic Beanstalk – Environment Resource Support + Updated PHP Runtime

by Jeff Barr | on | in Amazon CloudWatch, Amazon DynamoDB, AWS Elastic Beanstalk |

AWS Elastic Beanstalk can now provision and configure AWS resources to power your application. In conjunction with a new version of our PHP runtime, you get more control and more flexibility with less code.

In October, we announced the Elastic Beanstalk configuration files and talked about how they can help you configure EC2 instances without creating and maintaining custom AMIs. We have extended these configuration files to allow you to provision and configure resources such as SQS queues and DynamoDB tables for your Elastic Beanstalk application. The configuration file format is YAML-based, and you can find the details in the AWS Elastic Beanstalk Developer Guide.

We are also announcing an updated PHP runtime that supports configuration files as well as the seamless integration with Amazon RDS and Amazon VPC – see my previous blog post for details. The PHP runtime supports 5.3 and 5.4. You can also install dependencies using Composer. Visit the Elastic Beanstalk Developer Guide to learn more about the updated PHP runtime and for walkthroughs of how to deploy CakePHP and Symfony2.

I’d lke to show you how simple and powerful it is to use Elastic Beanstalk to create and configure the new environment resources. Ill use the updated PHP runtime and show you how to configure DynamoDB backed session management. You can find the full example in the AWS Elastic Beanstalk Developer Guide.

Here’s what you need to do:

  • Launch an Elastic Beanstalk PHP environment (see the AWS Elastic Beanstalk Developer Guide).
  • Create an IAM role with enough permission to read and write to the DynamoDB table (lets call it SessionRole).
  • Add an .ebextensions directory to your application.
  • In that directory, create a configuration file called dynamodb.config with the following content:
    Resources:
      SessionTable:
        Type: AWS::DynamoDB::Table
        Properties:
          KeySchema:
            HashKeyElement:
              AttributeName: Index
              AttributeType: S
          ProvisionedThroughput:
            ReadCapacityUnits: 1
            WriteCapacityUnits: 1
  • Add the role to the Elastic Beanstalk environment (this and all of the subsequent snippets also belong in the dynamodb.config file):
    AWSEBAutoScalingLaunchConfiguration:
        Properties:
          IamInstanceProfile:
            Fn::GetOptionSetting:
              OptionName: IAMInstanceProfileARN
              DefaultValue: “arn:aws:iam:: 123456789123:instance-profile/SessionRole”
  • Pass the details of the DynamoDB table to the EC2 instances in your environment:
    files:
      “/var/app/sessiontable”:
        mode: “000444”
        content: |
          ` {“Ref” : “SessionTable” }`
          ` {“Ref” : “AWS::Region” }`
  • Add a CloudWatch Alarm so that you are alerted when requests to DynamoDB get throttled:
    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
  • Make sure to include a require for the AWS SDK for PHP 2 in your composer.json file:
    “require” : {
                “aws/aws-sdk-php” : “*”
            }
  • Finally, add the following code to your PHP application to connect it to the configuration file:
    <?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’);

    // 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’ => ‘Index’));

    ?>

As you can see, you can use this new Elastic Beanstalk feature to set up and configure the AWS resources needed by your application.

– Jeff, with lots of help from Saad