AWS News Blog

AWS CloudFormation Supports Amazon DynamoDB and Amazon CloudFront Dynamic Content

Today, AWS CloudFormation is adding support for Amazon DynamoDB and Amazon CloudFront dynamic content. You can now describe related AWS resources such as EC2 instances, Amazon DynamoDB tables, and Amazon CloudFront distributions using CloudFormation templates. These templates can fully encapsulate the resources needed to run your application on AWS and can be version controlled alongside application source.

Amazon DynamoDB
You can easily provision and manage Amazon DynamoDB tables using CloudFormation. For example, if your application stores session state in Amazon DynamoDB, you can create the table using the CloudFormation snippet below:

“my-sessions-table” : {
              “Type” : “AWS::DynamoDB::Table” ,
              “Properties” : {
                  “KeySchema” : {
                      “HashKeyElement” : {
                        “AttributeName” : “id” ,
                        “AttributeType” : “AmazonDynamoDB::TYPE_STRING”
                      }
                  } ,
                  “ProvisionedThroughput” : {
                      “ReadCapacityUnits” : “5” ,
                      “WriteCapacityUnits” : “10”
                  }                            
              }
        }

Using the CloudFormation template format, you can quickly launch multiple similar stacks and customize each using the provided parameters. Should your application require more provisioned throughput, you can simply update a running CloudFormation stack and provide different parameters.

Amazon CloudFront Dynamic Content
In a previous post, I talked about how you can now use Amazon CloudFront to distribute static, dynamic, and streaming content. Now you can provision and update Amazon CloudFront distributions using CloudFormation templates. This includes support for multiple origins and dynamic content. To get started:

  1. You can configure multiple origin servers for your Amazon CloudFront distributions. In the example below, I host all of my pictures in S3 and I send my dynamic content to an Elastic Load Balancer:
    “app-distribution” : {
                “Type” : “AWS::CloudFront::Distribution” ,
                “Properties” : {
                    “DistributionConfig” : {
                        “DefaultRootObject” : “index.php” ,
                        “Origins” : [ {
                                “Id” : “S3 Origin” ,
                                “DomainName” : { “Fn::GetAtt” : [ “S3OriginBucket” , “DomainName” ] } ,
                                “S3OriginConfig” : { }
                            } , {
                                “Id” : “Dynamic Origin” ,
                                “DomainName” : { “Fn::GetAtt” : [ “WebServer” , “PublicDnsName” ] } ,
                                “CustomOriginConfig” : {
                                    “OriginProtocolPolicy” : “match-viewer”
                                }
                            }
                        ] ,
                        “DefaultCacheBehavior” : {
                            “TargetOriginId” : “Dynamic Origin” ,
                            “ForwardedValues” : {
                                “QueryString” : “true”
                            } ,
                            “ViewerProtocolPolicy” : “allow-all”
                        } ,
                        “CacheBehaviors” : [ {
                                “TargetOriginId” : “S3 Origin” ,
                                “ForwardedValues” : {
                                    “QueryString” : “false”
                                } ,
                                “ViewerProtocolPolicy” : “allow-all” ,
                                “MinTTL” : “500” ,
                                “PathPattern” : “*.jpg”
                            } ,
                            {
                                “TargetOriginId” : “Dynamic Origin” ,
                                “ForwardedValues” : {
                                    “QueryString” : “true”
                                } ,
                                “ViewerProtocolPolicy” : “allow-all” ,
                                “MinTTL” : “500” ,
                                “PathPattern” : “*.php”
                            }
                        ] ,
                        “Comment” : “Sample multi-origin CloudFront distribution created using CloudFormation.” ,
                        “Logging” : {
                            “Bucket” : { “Fn::GetAtt” : [ “S3LoggingBucket” , “DomainName” ] } ,
                            “Prefix” : “CloudFrontDistributionSampleLogs”
                        } ,
                        “Enabled” : “true”            
                    }
                }
            }
  2. You can use query string parameters to customize your pages. In the example below, Im forwarding query string parameters to my PHP origin:
    “CacheBehaviors” : [ {
                                “TargetOriginId” : “Dynamic Origin” ,
                                “ForwardedValues” : {
                                    “QueryString” : “true”
                                } ,
                                “ViewerProtocolPolicy” : “allow-all” ,
                                “PathPattern” : “*.php”
                            }
                        ]
  3. You can configure cache behaviors for different portions of the website. Here, Im setting a lower minTTL for my static content:
    {
      “TargetOriginId” : “S3 Origin” ,
      “ForwardedValues” : {
          “QueryString” : “false”
          } ,

        “ViewerProtocolPolicy” : “allow-all”,
        “MinTTL” : “500”,
        “PathPattern” : “*.jpg”
    }

Of course, if you need to make adjustments to any of the above settings, you can simply modify the template and then update your running CloudFormation stacks. CloudFormation takes care of the rest.

As usual, CloudFormation has also expanded its list of sample templates to provide more examples on how to use Amazon DynamoDB and Amazon CloudFront dynamic content. You can also visit the AWS CloudFormation User Guide to get started.

— Jeff;

P.S: We are looking for highly motivated software developers and product managers who want to work on AWS CloudFormation. If you are interested, send us your resume to aws-cloudformation-jobs at amazon.com.