2017SpringITPF-Scalable WordPress in AWS · Scalable WordPress in AWS –Elastic Beanstalk Stephen...
Transcript of 2017SpringITPF-Scalable WordPress in AWS · Scalable WordPress in AWS –Elastic Beanstalk Stephen...
Tools
• Terraform• https://www.terraform.io/
• UIUCAWS• https://aws.illinois.edu/• [email protected]
• Labs• https://github.com/sbutler/itpf-sp2017-wordpress-terraform• https://github.com/sbutler/itpf-sp2017-wordpress-wordpress
SharedData- RDS
• AWSRelationalDatabaseService(RDS)• MySQL,MariaDB,orAurora• PostgreSQLorAurora• Oracle,MSSQLServer
• Managedupdates,backups• HotstandbyandreadreplicasMariaDB Ohio Single Multi-AZ
db.t2.micro $0.017/hr ($12.24) $0.034/hr ($24.48)
db.t2.medium $0.068/hr ($48.96) $0.136/hr ($97.92)
db.m4.large $0.175/hr ($126.00) $0.350/hr ($252.00)
GPStorage $0.115perGB-mo $0.230perGB-mo
SharedData- RDS
• MySQLvsMariaDB vsAurora• MySQL:officialrelease,moststable• MariaDB:communityrelease,newerfeaturesandbugfixes• Aurora:AWSoptimized(x5performance)withMySQLcompatibility
• Optionalencryptionofstorage• UseInnoDB tables!
SharedData- RDS
• Backups• Snapshots• MomentarilyfreezesI/O
• Maintenance• Majororminorupdates• Configurationchanges• Anythingthatrequiresarestart
SharedData- EFS
• AWSElasticFileSystem(EFS)• NFSv4.1protocol• Unlimitedstorage
• Highlyavailablewithinaregion,acrossAZ's• Notautomaticallybackedup• Pricing(Ohio):$0.30perGB-mo
SharedData- EFS
• Baselineratebasedonamountofstoreddata• 50MiB/sperTiB;50KiB/sperGiB• 1GiB ->50KiB/s• 500GiB ->25MiB/s• 10TiB ->500MiB/s
• BurstRates• <1TiB ->100MiB/s• Additional100MiB/sburstperTiB stored
• Initialburstcreditof2.1TiB
SharedData- EFS
• ExampleincludesasingleEFS• efs-wordpress.tf
• Withacleverdirectorystructure,possibletoshareaninstance
WebHosting- EB
• AWSElasticBeanstalk(EB)• Tomcat,.NETonIIS,PHP,Python,Ruby,Docker,etc• Abstractstheinfrastructureforwebapplications
• Loadbalancing,multi-az,scaling,healthmonitoring,maintenance• ApplicationdeploymentandupdatingOhio Price
t2.micro $0.012/hr ($8.64)
t2.medium $0.047/hr ($33.84)
m4.large $0.100/hr ($72.00)
ELB $0.025/hr ($18.00)
WebHosting- EB
• Application• collectionofalltheEBcomponents
• ApplicationVersion• deployablepackage(code)
• Environment• infrastructureresources
• EnvironmentConfiguration• Settings/optionsthatcustomizetheenvironment
• ConfigurationTemplate• Settings/optionsthatcanbeappliedtomultipleenvironments
WebHosting– EBEnv
• ManagedActions:updatesperformedbyAWS• Performsrollingupdates(optional)• !!EC2instancescreatedfromtheappversion!!• Donotmanuallychangeaninstance• Donotstoreappdataontheinstance(see:RDS,EFS,S3)
WebHosting– EBConfig
• Determinessettingsandoptionsforenvironmentcomponents• VPCsubnets,etc• EC2instancetypes,securitygroups,etc• ELBconfiguration,healthcheck,protocols,etc• ASGsize,thresholds,policies,etc• Managedactionswindow
• Mergedfrommultiplesources1. Directenvironmentsettings2. Configurationtemplates3. ApplicationVersion.ebextensions4. Defaultvalues
WebHosting- EB
• Exampleincludesasingleapp,config template,andenv• eb-wordpress.tf
• CustomizeDNSwithRoute53• Potentiallycreatemoreenvironmentsfordev,test,etc
WordPress- Lab
• UploadApplicationVersion• Canuse"makeeb-appver"togenerateanewone
• DeployApplicationVersiontoenvironment• Waitforupdatetocomplete
WordPress
• CreateanApplicationVersion(.zip)forWordPress• Appfilesincludedinthe.zipfile
• EverythinginyourstandardWPinstall• wp-content/plugins;wp-content/themes• wp-content/mu-plugins*
• UsercontentstoredinRDSoronEFS• wp-content/uploads;wp-content/cache;wp-content/w3tc-config• wp-content/blogs.dir*
• wp-config.php loadssettingsfromEB• HealthCheck• ConnectEFS
WordPress– Standard
• Startwith.zipfromhttps://wordpress.org• Removewp-content/uploadsdirectory
• Addplugins• Addthemes• Extra:.ebextensions/wpcli.config
• Installswp-clicommand
WordPress– wp-config.php
• Startwiththewp-config-sample.php• Remember:weaddedourWPsettingsasenvironmentvariables!• Use$_SERVER['XXX']forDBandsaltsettings
• Customizewhateverelseyou'dlike
WordPress– HealthCheck
• aws-eb-ping.php• SimplePHPcheck• DoesnotcheckthehealthofWordPressitself
• .ebextensions/healthchecks.config• ModifytheconfigurationofourEBEnv• ConfigurestheELBandEBtocheck/aws-eb-ping.php
WordPress- EFS
• EBdoesnotautomaticallyconnecttoEFS• Remember:howdidwedoitintheterraformlession?• .ebextensions/efs-mount.config
• WritesEFSconfig filesusingEBenvironment• Containersetup:
• MountsEFS• Symlinks userdatadirectories
• Alsoperiodicallyupdates.htaccess
Monitoring
• InstancelogsinCloudWatch Logs• SNSTopictogenerateemails
• EBpublishesnotifications• RDSpublishesnotifications• CloudWatch Alarmsforotherinterestingmetrics
Monitoring- Alarms
• CloudWatch Alarmstomonitorotherimportantmetrics• EFSI/Olimit• EFSburstcreditbalance• RDSconnections
Questions
• StephenJ.Butler• [email protected]
• UIUCAWS• https://aws.illinois.edu/• [email protected]
• Terraform• https://www.terraform.io/
• Labs• https://github.com/sbutler/itpf-sp2017-wordpress-terraform• https://github.com/sbutler/itpf-sp2017-wordpress-wordpress
Tools
• Terraform• https://www.terraform.io/
• UIUCAWS• https://aws.illinois.edu/• [email protected]
• Labs• https://github.com/sbutler/itpf-sp2017-terraform
Introduction– WhatisIaC?
Wikipedia- InfrastructureasCodeInfrastructureascode(IaC)istheprocessofmanagingandprovisioningcomputerdatacentersthroughmachine-readabledefinitionfiles,ratherthanphysicalhardwareconfigurationorinteractiveconfigurationtools.
Terraform- InfrastructureasCodeInfrastructureisdescribedusingahigh-levelconfigurationsyntax.Thisallowsablueprintofyourdatacentertobeversionedandtreatedasyouwouldanyothercode.Additionally,infrastructurecanbesharedandre-used.
Introduction– WhyIaC?
• RepeatableDeployments(dev,test,prod)• Shareabledeploymentstrategies• Versionedconfigurationchanges• Predictableconfigurationchanges• Reduceinfrastructurerelatederrorsandrisk
Introduction– Terraform
• OpenSourceprojectforIaC• Multipleplatforms• Multipleproviders• MoreflexiblethanCloudFormation• (FasterthanCloudFormation)
TerraformLayout
• Directoryof".tf" files• Personalpreference
File/Subdirectory Description
providers.tf Setupfor terraformproviders
variables.tf Variablesusedbytheconfiguration
outputs.tf Outputsfortheconfiguration
files/ Staticfilesreferenced inaconfig
modules/ Localterraform modules*
scripts/ Local orremotescriptstorunforprovisioning*
templates/ Dynamictemplatefiles forthe"template"provider
variables/ Variable(".tfvar")filestochange aconfig
Providers
• Providersthebackendforterraformcomponents• aws,google,azurerm• mysql,http,archive,template_file• https://www.terraform.io/docs/providers/
• Examine"providers.tf"
Resources- Overview
• Somethingthatexistsintheinfrastructure• EC2instance;ELB;EBEnvironment;IAMRole;…• https://www.terraform.io/docs/providers/aws/
• Whatterraformmanages(Create,Update,Destroy)• Musthaveatype andauniquename
resource "aws_instance" "example" {# attributes and blocks
}
Resources- Create
• Exampleinstances.tf• Addattributes,plan,andapply
Attribute Value
ami ami-7bfcd81e
instance_type t2.micro
key_name workshop
Resources- Update
• Exampleinstances.tf• Addtags blockforName andNetID,plan,andapply.
resource "aws_instance" "example" {# attributestags {
Name = "…"NetID = "…"
}}
Variables- Overview
• Why:greaterreuseandflexibility• string:"abc","1","2"• list:["foo","bar","baz"]• map*:{"foo"="a"}
• Mustbedeclared,andhaveauniquenamevariable "my_example" {
type = "string"description = "Variable named my_example"
}
# Referenced "${var.NAME}"some_attribute = "${var.my_example}"
Variables- Sources
• Prompt• CommandLine:• terraformplan-var foo=1-var bar=2
• Files• terraformplan-var-filevariables/project.tfvars
• Canspecifymultiples;usesordertheyweregiven
Variables- Lab• variables.tf:addstringvariablesnetid,key_name,instance_type• instances.tf:modifytouseappropriatevariablesvariable "netid" {
type = "string"description = "Contact for resources in this config"
}
resource "aws_instance" "example" {# attributestags {
Name = "…"NetID = "${var.netid}"
}}
Variables– terraform.tfvars
• Willautomaticallyloadterraform.tfvars• Impliedinevercommand:-var-fileterraform.tfvars
• Uncommentinstance_type andkey_name• Addnetid
Outputs- Overview
• Easilydisplayusefulresults• Mustbedeclaredandhaveauniquename• Valueisanattributeonresources,data,variables,etc
output "my_resource_output" {value = "${TYPE.NAME.ATTRIBUTE}"
}
# Example: "${aws_instance.example.id}"
Outputs- Lab
• Example:outputs.tf• aws_instance.example.public_ip• aws_instance.example.id
output "example_id" {value = "${aws_instance.example.id}"
}
Data- Overview
• Why:gathersinformationforuseinconfigs• Remotesources:ACMcerts;AMI;EIP;etc• Localsources:renderedtemplates,IAMPolicyDocs,etc
• Referencedbyotherdatablocksorresources• Validatevaluesspecifiedinvariables• Datablockshaveatype andaunqiue namedata "aws_subnet" "public" {
id = "${var.public_subnet}"}
# Referenced "${data.TYPE.NAME.ATTRIBUTE}"some_attribute = "${data.aws_subnet.public.id}"
Data- Lab
• Example:instances.tf• AdddatablocktofindthelatestAmazonLinuxAMIid• Runterraformplantoseewhathappens
data "aws_ami" "amazon_linux" {most_recent = trueowners = [ "amazon" ]filter {name = "name"; values = ["amzn-ami-*-gp2"]}filter {name = "virtualization-type";values = ["hvm"]}filter {name = "architecture"; values = ["x86_64"]}
}
resouce "aws_instance" "example" {ami = "${data.aws_ami.amazon_linux.id}"# attributes and blocks
}
Resources- Update
• Stop!ChangingtheAMIwillrecreatetheresource• Terraformshowsthiswith"-/+"insteadof"~".• PreventingrecreationonAMIchanges:
data "aws_ami" "amazon_linux" {most_recent = true# …
}
resouce "aws_instance" "example" {ami = "${data.aws_ami.amazon_linux.id}"# attributes and blocks
lifecycle { ignore_changes = [ "ami" ] }}
Templates- Overview
• Generatestringstobeusedforresources,otherdata,etc• Templatesareakindofdata;acceptsvars touseinthetemplate• Best:storetemplatestringinitsownfile
Templates- Example
data "template_file" "hello_world" {template = "${file("templates/hello_world.txt")}"vars {
name = "John Doe"message = "How are you doing today?"
}}
# Referenced "${data.template_file.NAME.rendered}"an_attr = "${data.template_file.hello_world.rendered}"
Hello, ${name}!
${message}
Cloud-Init - Overview
• Cloud-Init isawaytocustomizeyourinstancesonfirstboot• Boothooks• Configuration:timezone,packages,files,runcommands• Firstbootscripts• Upstartscripts• https://cloudinit.readthedocs.io/en/latest/
• MIMEDocumentspecifiedintheinstanceUserData• Canbebuiltusingaterraformdatablock• Particularlyusefulwithtemplates!
Cloud-Init - Lab
• Usecloud-init toconnectyourinstancetoEFS;usingTechServicesdevelopedscript• WriteanEFSconfig file• /etc/opt/uiuc-techservices/efs/sharedfs.conf• ContainssettingsformountingtheEFSvolume
• efs_filesystem_id• mount_target (optional- /mnt/sharedfs)• nfs_options (optional– AWSdefaults)
• Runafirstbootscript(files/efs.sh)• Readsallconfig files,mountsthem,andupdatesfstab
Cloud-Init – Lab
• Configurationtemplatedata "template_file" "cloudinit_config" {
template = "${file("templates/cloudinit_config.yml")}"vars {
efs_id = "${var.sharedfs_id}"}
}
packages:- nfs-utils
write_files:- path: /etc/opt/uiuc-techservices/efs/sharedfs.confowner: root:rootpermission: '0644'content: efs_filesystem_id=${efs_id}
Cloud-Init – Labdata "template_cloudinit_config" "userdata" {
part {content_type = "text/cloud-config"
content = "${data.template_file.cloudinit_conf.rendered}"}part {
content_type = "text/x-shellscript"filename = "efs.sh"content = "${file("files/efs.sh")}"
}}
resource "aws_instance" "example" {# attributesuser_data = "${data.template_cloudinit_config.userdata.rendered}"
}
Cloud-Init - Lab
• Runplanandchecktheoutput• Fixerrors• Willrequirerecreatingtheinstance
• Runapply• SSHandcheckthat/mnt/sharedfs existsandiswritable
Questions
• StephenJ.Butler• [email protected]
• UIUCAWS• https://aws.illinois.edu/• [email protected]
• Terraform• https://www.terraform.io/
• Labs• https://github.com/sbutler/itpf-sp2017-terraform