Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Automating Infrastructures with AWS Cloud Forma...

Sponsored · SiteGround - Reliable hosting with speed, security, and support you can count on. →

Automating Infrastructures with AWS Cloud Formation and Chef

Automating Infrastructures with AWS Cloud Formation and Chef

Avatar for Jeremy Przygode

Jeremy Przygode

May 02, 2013

Other Decks in Technology

Transcript

  1. 10

  2. 11

  3. • Create Chef roles and cookbooks • Build pre-loaded AMIs

    • Create CFN template • Launch • Manage Change – Chef – CFN 12
  4. • Why? – Preload Chef version – Preload dependencies •

    Benefits – Improves launch speed – Reduces external dependencies – More Control 13 • How? – We use Chef to configure AMIs.
  5. 15 "KeyName": { "Type": "String", "Description": "Name of an existing

    EC2 KeyPair to enable SSH access to the web server", "Default": “stratalux-AWS-US-West2" }, "InstanceType": { "Description": "Application Server EC2 instance type", "Type": "String", "Default": "m1.small", "AllowedValues": ["t1.micro", "m1.small", "m1.medium", "m1.large", "m1.xlarge", "m2.xlarge", "m2.2xlarge", "m2.4xlarge", "c1.medium", "c1.xlarge", "cc1.4xlarge", "cc2.8xlarge", "cg1.4xlarge"], "ConstraintDescription": "must be a valid EC2 instance type." },
  6. 16 "ChefOrganization": { "Description": "The Organization name used in Hosted

    Chef.", "Type": "String", "Default": “stratalux" }, "ChefEnvironment": { "Description": "The Chef Environment.", "Type": "String", "Default": “Production" }, "ChefRunList": { "Description": "The Chef runlist.", "Type": "String", "Default": "role[application_server]" },
  7. 17 "DomainName": { "Description": "Server Domain Name.", "Type": "String", "Default":

    “strata.1ux.it" }, "ChefServerPrivateKeyBucket": { "Description": "S3 bucket containing validation private key for Chef Server", "Type": "String", "Default": "stlx.cfn" }
  8. 18 "Mappings" : { "RegionMap" : { "us-east-1" : {

    "AMI" : "ami-76f0061f" }, "us-west-1" : { "AMI" : "ami-655a0a20" }, "eu-west-1" : { "AMI" : "ami-7fd4e10b" }, "ap-southeast-1" : { "AMI" : "ami-72621c20" } } }
  9. • Resources 19 “AppServerAccess ": { "Type": "AWS::EC2::SecurityGroup", "Properties": {

    "GroupDescription": “AppServerAccess", "SecurityGroupIngress": [ { "IpProtocol": "tcp", "FromPort": "8080", "ToPort": "8080", "CidrIp": "0.0.0.0/0"} ]}},
  10. 20 • Needed AWS Resources – IAM User & Access

    Key – S3 Bucket Policy – Auto Scaling Group – Launch Configuration – Security Groups – Load Balancer – Scaling Policy – Cloudwatch Alarms – WaitConditions – SNS topic
  11. “AppServerAccess ": { "Type": "AWS::EC2::SecurityGroup", "Properties": { "GroupDescription": “AppServerAccess", "SecurityGroupIngress":

    [ { "IpProtocol": "tcp", "FromPort": "8080", "ToPort": "8080", "CidrIp": "0.0.0.0/0"} ]}}, 21
  12. "AppServerGroup": { "Type": "AWS::AutoScaling::AutoScalingGroup", "Properties": { "AvailabilityZones": { "Fn::GetAZs": ""},

    "LaunchConfigurationName": { "Ref": "ChefClient"}, "LoadBalancerNames": [ {"Ref": “appserverstratalux1uxit"} ], "MinSize": "2", "MaxSize": "10" }}, 22
  13. "CPUAlarmHigh": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": "Scale-up if CPU

    > 75% for 2 minutes", "MetricName": "CPUUtilization", "Namespace": "AWS/EC2", "Statistic": "Average", "Period": "60", "EvaluationPeriods": "2", "Threshold": "75", "AlarmActions": [{ "Ref": "AppServerScaleUpPolicy"}], "Dimensions": [{ "Name": "AutoScalingGroupName", "Value": { "Ref": "AppServerGroup"}}], "ComparisonOperator": "GreaterThanThreshold“ }}, 25
  14. "CPUAlarmLow": { "Type": "AWS::CloudWatch::Alarm", "Properties": { "AlarmDescription": "Scale-down if CPU

    < 75% for 10 minutes", "MetricName": "CPUUtilization", "Namespace": "AWS/EC2", "Statistic": "Average", "Period": "300", "EvaluationPeriods": "2", "Threshold": "75", "AlarmActions": [{ "Ref": "AppServerScaleDownPolicy"}], "Dimensions": [{ "Name": "AutoScalingGroupName", "Value": { "Ref": "AppServerGroup"}}], "ComparisonOperator": "LessThanThreshold" }}, 26
  15. “appserverstrata1uxit": { "Type": "AWS::ElasticLoadBalancing::LoadBalancer", "Properties": { "AvailabilityZones": { "Fn::GetAZs": ""},

    "HealthCheck": { "HealthyThreshold": "10", "Interval": "30", "Target": "HTTP:8080/healthcheck", "Timeout": "2", "UnhealthyThreshold": "2" }, "Listeners": [{ "InstancePort": "8080", "LoadBalancerPort": "80", "Protocol": "HTTP", "PolicyNames": [] }]}}, 27
  16. "ChefClient": { "Type": "AWS::AutoScaling::LaunchConfiguration", "DependsOn": "BucketPolicy", "Metadata": { "AWS::CloudFormation::Init": {

    "config": { "packages": { "rubygems": { "chef": [],"ohai": ["0.6.4"] }, "apt": { "build-essential": [],"ruby": [],"rubygems": [] }}, 28
  17. "files": { "/etc/chef/first-boot.json": { "content": { "run_list": { "Ref": "ChefRunList“

    }, "mode": "000644", "owner": "root", "group": "root" }}, "/home/ubuntu/.s3cfg": { "content": { "Fn::Join": ["", ["[default]\n", "access_key = ", { "Ref": "HostKeys" \}, "\n", "secret_key = ", { "Fn::GetAtt": ["HostKeys", "SecretAccessKey"] }, "\n", "use_https = True\n"]]}, "mode": "000644", "owner": "ubuntu", "group": "ubuntu" }, 29
  18. "Properties": { "SecurityGroups": [ { "Ref": “AppServerAccess" } ], "ImageId":

    { "Fn::FindInMap": ["AWSRegionArch2AMI", { "Ref": "AWS::Region" }, { "Fn::FindInMap": ["AWSInstanceType2Arch", { "Ref": "InstanceType" }, "Arch“ ]}]}, "KeyName": { "Ref": "KeyName" }, "InstanceType": { "Ref": "InstanceType" } 30
  19. "UserData": { "Fn::Base64": { "Fn::Join": ["", ["#!/bin/bash -v\n", "function error_exit\n",

    "{\n", " cfn-signal -e 1 -r \"$1\" '", { "Ref": "ChefClientWaitHandle“ }, "chef-client -j /etc/chef/first-boot.json ||”, “error_exit 'Failed to initialize host via chef client' \n", "# If all went well, signal success\n", "cfn-signal -e $? -r 'Chef Server configuration' '", { "Ref": "ChefClientWaitHandle" }, "'\n“ } 31
  20. 32

  21. 33

  22. 34

  23. 35

  24. 36

  25. Jeremy Przygode [email protected] www.stratalux.com @stratalux Special thanks to JD Hancock

    for the awesome images! http://www.flickr.com/photos/jdhancock/ 37