Although AWS CloudFormation takes care of provisioning all the resources, you still must ..... using the aws cloudformat
Bootstrapping Applications via AWS CloudFormation With AWS CloudFormation, you write templates to define the set of resources that you need to run your applications. These resources might include Amazon Elastic Cloud Compute (EC2) instances, Amazon Relational Fn::GetAtt": ["SampleDB", "Endpoint.Address"]}, ":", {"Ref": "Ref": "Ref": "Ref": "WebServerPort"} ]]}} } } }
The instance must have a script that retrieves the string from the Amazon EC2 metaRef": "AWS::StackId" }, "\n", "region=", { "Ref": "AWS::Region" }, "\n" ]]}, "mode" : "000400", "owner" : "root", "group" : "root" }, "/etc/cfn/hooks.d/cfn-auto-reloader.conf": { "content": { "Fn::Join": [ "", [ "[cfn-auto-reloader-hook]\n", "triggers=post.update\n", "path=Resources.LaunchConfig.Metadata.AWS::CloudFormation::Init\n", "action=/opt/aws/bin/cfn-init ", " --stack ", { "Ref" : "AWS::StackName" }, " --resource LaunchConfig ", " --configsets wordpress_install ", " --region ", { "Ref" : "AWS::Region" }, "\n" ]]}, "mode" : "000400", "owner" : "root", "group" : "root" } }, "services" : { "sysvinit" : { "cfn-hup" : { "enabled" : "true", "ensureRunning" : "true", "files" : ["/etc/cfn/cfn-hup.conf", "/etc/cfn/hooks.d/cfn-auto-reloader.conf"] } } } }, "install_wordpress" : { "packages" : { "yum" : { "php" : [], "php-mysql" : [], "mysql" : [], "httpd" : [] } }, "sources" : { "/var/www/html" : "http://wordpress.org/latest.tar.gz" }, "files" : { "/tmp/create-wp-config" : { "content" : { "Fn::Join" : [ "", [
Bootstrapping Applications via AWS CloudFormation
20
config.php\n"
}
}
"#!/bin/bash\n", "cp /var/www/html/wordpress/wp-config-sample.php /var/www/html/wordpress/wp-config.php\n", "sed -i \"s/'database_name_here'/'",{ "Ref" : "DBName" }, "'/g\" wp-config.php\n", "sed -i \"s/'username_here'/'",{ "Ref" : "DBUsername" }, "'/g\" wp-config.php\n", "sed -i \"s/'password_here'/'",{ "Ref" : "DBPassword" }, "'/g\" wp-config.php\n", "sed -i \"s/'localhost'/'",{ "Fn::GetAtt" : [ "DBInstance", "Endpoint.Address" ] }, "'/g\" wp]]}, "mode" : "000500", "owner" : "root", "group" : "root"
} }, "commands" : { "01_configure_wordpress" : { "command" : "/tmp/create-wp-config", "cwd" : "/var/www/html/wordpress" } }, "services" : { "sysvinit" : { "httpd" : { "enabled" : "true", "ensureRunning" : "true" } } }
}, "Properties": { "ImageId" : { "Fn::FindInMap" : [ "AWSRegionArch2AMI", { "Ref" : "AWS::Region" }, { "Fn::FindInMap" : [ "AWSInstanceType2Arch", { "Ref" : "InstanceType" }, "Arch" ] } ]
},
"InstanceType" : { "Ref" : "InstanceType" }, "SecurityGroups" : [ {"Ref" : "WebServerSecurityGroup"} ], "KeyName" : { "Ref" : "KeyName" }, "UserData" : { "Fn::Base64" : { "Fn::Join" : ["", [ "#!/bin/bash -xe\n", "yum update aws-cfn-bootstrap\n", "/opt/aws/bin/cfn-init ", " --stack ", { "Ref" : "AWS::StackName" }, " --resource LaunchConfig ", " --configsets wordpress_install ", " --region ", { "Ref" : "AWS::Region" }, "\n",
},
}
]]}}
"/opt/aws/bin/cfn-signal -e $? '", { "Ref" : "WebServerWaitHandle" }, "'\n"
"WebServerWaitHandle" : { "Type" : "AWS::CloudFormation::WaitConditionHandle" }, "WebServerWaitCondition" : { "Type" : "AWS::CloudFormation::WaitCondition", "DependsOn" : "WebServerGroup", "Properties" : { "Handle" : {"Ref" : "WebServerWaitHandle"}, "Timeout" : "900" } }, "DBInstance" : { "Type": "AWS::RDS::DBInstance", "Properties": {
Bootstrapping Applications via AWS CloudFormation
21
},
}
"DBName" : "Engine" : "MultiAZ" : "MasterUsername" : "DBInstanceClass" : "DBSecurityGroups" : "AllocatedStorage" : "MasterUserPassword":
{ "Ref" : "DBName" }, "MySQL", { "Ref": "MultiAZDatabase" }, { "Ref" : "DBUsername" }, { "Ref" : "DBClass" }, [{ "Ref" : "DBSecurityGroup" }], { "Ref" : "DBAllocatedStorage" }, { "Ref" : "DBPassword" }
"DBSecurityGroup": { "Type": "AWS::RDS::DBSecurityGroup", "Properties": { "DBSecurityGroupIngress": { "EC2SecurityGroupName": { "Ref": "WebServerSecurityGroup"} }, "GroupDescription" : "Frontend Access" } } }, "Outputs" : { "WebsiteURL" : { "Value" : { "Fn::Join" : ["", ["http://", { "Fn::GetAtt" : [ "ElasticLoadBalancer", "DNSName" ]}, "/wordpress" ]]}, "Description" : "WordPress Website" } } }
Getting the AWS CloudFormation Helper Scripts and Templates The AWS CloudFormation helper scripts are available from the following locations: •
• • • •
The Amazon Linux AMI has the AWS CloudFormation helper scripts installed by default in /opt/aws/bin. The Windows AMIs provided by Amazon Web Services have the AWS CloudFormation helper scripts and the ability to execute scripts in user data installed by default. The AWS helper scripts are available in the Amazon Linux AMI yum repository (the package name is aws-cfn-bootstrap). In addition, the helpers are available in other formats. For more details go to: http://aws.amazon.com/developertools/AWS-CloudFormation/4026240853893296 The templates that are used in the previous examples are available, along with many other sample templates, on the AWS CloudFormation sample template site at http://aws.amazon.com/cloudformation/aws-cloudformation-templates/
Bootstrapping Applications via AWS CloudFormation
22