Top Banner
Automate All The Things Software Defined Infrastructure with AWS CloudFormation, Docker and Jenkins
84

Automate All AWS Things.key

Feb 14, 2017

Download

Documents

vanquynh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Automate All AWS Things.key

Automate All The Things

Software Defined Infrastructure with

AWS CloudFormation, Docker and Jenkins

Page 2: Automate All AWS Things.key
Page 3: Automate All AWS Things.key

Mark Fischer

• 20 Years of Web Application Development

• 5 Years of Infrastructure Tools Development

• 2 Years AWS Cloud Automation Development

Page 4: Automate All AWS Things.key

Overview

• Codify Infrastructure Decisions

• Document Deployment Processes

• Ensure Repeatable Operations

• Empower Developers and Product Owners

Page 5: Automate All AWS Things.key

huh, it worked last time

Page 6: Automate All AWS Things.key
Page 7: Automate All AWS Things.key
Page 8: Automate All AWS Things.key
Page 9: Automate All AWS Things.key

are you sure you installed the fizbuzz_x86_64

library correctly?

Page 10: Automate All AWS Things.key
Page 11: Automate All AWS Things.key

how are we going to do user training in prod?

we need another dev environment

*appologies to piecomic.com

Page 12: Automate All AWS Things.key

Automation Progression

Manual Infrastructure Provisioning

➡ CloudFormation

Manual Environment Configuration ➡ Docker

Manual Code Deployment ➡ Jenkins

Page 13: Automate All AWS Things.key

Infrastructure Provisioning

A few years ago Operations Staff 1 Week

Last year Better Operations Proceedures 1 Day

Now DevOps & AWS 10 Minutes

Time for me to get new infrastructure provisioned

Page 14: Automate All AWS Things.key

Manual AWS EC2 InstanceProvision a simple EC2 Instance for some testing and

experimentation

Page 15: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 16: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 17: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 18: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 19: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 20: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 21: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 22: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 23: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 24: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 25: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 26: Automate All AWS Things.key

Manual AWS EC2 Instance

Page 27: Automate All AWS Things.key

Security Group

SSH Key

Page 28: Automate All AWS Things.key
Page 29: Automate All AWS Things.key

Security Group Security GroupSecurity Group

DB Subnet Group

X

DB Option Group

10+ Separate Resources

Page 30: Automate All AWS Things.key

Security Group SSH Key

Page 31: Automate All AWS Things.key

CloudFormationCodify Infrastructure Deployment

Page 32: Automate All AWS Things.key

CloudFormation

"AWS CloudFormation gives developers and systems administrators an easy way to create and manage a collection of related AWS resources, provisioning and updating them in an orderly and predictable fashion."

https://aws.amazon.com/cloudformation/

Page 33: Automate All AWS Things.key

CloudFormation

• JSON Text Document

• Defines AWS Resources

• Defines Resource Relationships

• Input Parameters for Flexibility

• Provisioning and De-Provisioning

Now With

100%

More Y

AML!

Page 34: Automate All AWS Things.key

CloudFormation

• Originally All JSON Text Files

• Recently Added YAML Support

"Resources": { "VpcEcsEas": { "Type": "AWS::EC2::VPC", "Properties": { "CidrBlock" : { "Ref": "VPCcidr" }, "EnableDnsSupport": true, "EnableDnsHostnames": true, "Tags" : [ { "Key": "Name", "Value": { "Ref": "VPCName" } } ] } }, "InternetGateway": { "Type": "AWS::EC2::InternetGateway", "Properties": { "Tags" : [ { "Key": "Name", "Value": { "Fn::Join": [ "", [ { "Ref": "VPCName" }, " Internet Gateway" ] ] } } ] } }, "InternetGatewayAttachment": { "Type": "AWS::EC2::VPCGatewayAttachment", "Properties": { "InternetGatewayId": { "Ref": "InternetGateway" }, "VpcId": { "Ref": "VpcEcsEas" } } },

Page 35: Automate All AWS Things.key

• Parameters (Input Variables)

• Metadata

• Mappings

• Conditions

• Resources

• Outputs

--- AWSTemplateFormatVersion: "2010-09-09"

Parameters: # Pick Zone-A or Zone-B where this EC2 instance will be deployed. AZChoice: Description: "Availability Zone" Type: String AllowedValues: - "Zone-A" - "Zone-B"

Mappings: # The two availability zones where this EC2 instance can be deployed in. ZoneMap: Zone-A: subnet: "subnet-e1c2f584" zone: "us-west-2a" Zone-B: subnet: "subnet-f28fda85" zone: "us-west-2b"

Resources: # Deploys an EC2 instance with some tags. Ec2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap ["OSImageMap", !Ref "OSType", "64"] KeyName: !Ref "KeyName" InstanceType: !Ref "InstanceType" AvailabilityZone: !FindInMap ["ZoneMap", !Ref "AZChoice", "zone"] NetworkInterfaces: - AssociatePublicIpAddress: "true" DeviceIndex: "0" GroupSet: - !Ref "InstanceSecurityGroup" SubnetId: !FindInMap ["ZoneMap", !Ref "AZChoice", "subnet"] Tags: - Key: "Name" Value: !Ref "HostName"

Outputs: InstancePublicIP: Description: "The Public IP address of the instance" Value: !GetAtt Ec2Instance.PublicIp

TemplateAnatomy

Page 36: Automate All AWS Things.key

# #### EC2 Instance # # Deploys the EC2 instance with some tags. Ec2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap ["OSImageMap", !Ref "OSType", "64"] KeyName: !Ref "KeyName" InstanceType: !Ref "InstanceType" AvailabilityZone: !FindInMap ["ZoneMap", !Ref "AZChoice", "zone"] NetworkInterfaces: - AssociatePublicIpAddress: "true" DeviceIndex: "0" GroupSet: - !Ref "InstanceSecurityGroup" SubnetId: !FindInMap ["ZoneMap", !Ref "AZChoice", "subnet"] Tags: - Key: "Name" Value: !Ref "HostName"

# #### Instance Security Group # # Security group for the EC2 instance, that allows you to SSH into the instance InstanceSecurityGroup: Type: "AWS::EC2::SecurityGroup" Properties: GroupDescription: "Allow ssh to client host" VpcId: !Ref "VPCID" SecurityGroupIngress: - IpProtocol: "tcp" FromPort: "22" ToPort: "22" CidrIp: "0.0.0.0/0" Tags: - Key: "Name" Value: !Sub "${HostName} Security Group"

# #### Instance Role # # This is the IAM role that will be applied to the EC2 Instance. Any AWS specific # permissions that the node might need should be defined here. # EnvInstanceRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service:

• AWS Resources are codified in the Template

• Relationships Established

TemplateAnatomy

Page 37: Automate All AWS Things.key

# #### EC2 Instance # # Deploys the EC2 instance with some tags. Ec2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap ["OSImageMap", !Ref "OSType", "64"] KeyName: !Ref "KeyName" InstanceType: !Ref "InstanceType" AvailabilityZone: !FindInMap ["ZoneMap", !Ref "AZChoice", "zone"] NetworkInterfaces: - AssociatePublicIpAddress: "true" DeviceIndex: "0" GroupSet: - !Ref "InstanceSecurityGroup" SubnetId: !FindInMap ["ZoneMap", !Ref "AZChoice", "subnet"] Tags: - Key: "Name" Value: !Ref "HostName"

# #### Instance Security Group # # Security group for the EC2 instance, that allows you to SSH into the instance InstanceSecurityGroup: Type: "AWS::EC2::SecurityGroup" Properties: GroupDescription: "Allow ssh to client host" VpcId: !Ref "VPCID" SecurityGroupIngress: - IpProtocol: "tcp" FromPort: "22" ToPort: "22" CidrIp: "0.0.0.0/0" Tags: - Key: "Name" Value: !Sub "${HostName} Security Group"

# #### Instance Role # # This is the IAM role that will be applied to the EC2 Instance. Any AWS specific # permissions that the node might need should be defined here. # EnvInstanceRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service:

Page 38: Automate All AWS Things.key

# #### EC2 Instance # # Deploys the EC2 instance with some tags. Ec2Instance: Type: "AWS::EC2::Instance" Properties: ImageId: !FindInMap ["OSImageMap", !Ref "OSType", "64"] KeyName: !Ref "KeyName" InstanceType: !Ref "InstanceType" AvailabilityZone: !FindInMap ["ZoneMap", !Ref "AZChoice", "zone"] NetworkInterfaces: - AssociatePublicIpAddress: "true" DeviceIndex: "0" GroupSet: - !Ref "InstanceSecurityGroup" SubnetId: !FindInMap ["ZoneMap", !Ref "AZChoice", "subnet"] Tags: - Key: "Name" Value: !Ref "HostName"

# #### Instance Security Group # # Security group for the EC2 instance, that allows you to SSH into the instance InstanceSecurityGroup: Type: "AWS::EC2::SecurityGroup" Properties: GroupDescription: "Allow ssh to client host" VpcId: !Ref "VPCID" SecurityGroupIngress: - IpProtocol: "tcp" FromPort: "22" ToPort: "22" CidrIp: "0.0.0.0/0" Tags: - Key: "Name" Value: !Sub "${HostName} Security Group"

# #### Instance Role # # This is the IAM role that will be applied to the EC2 Instance. Any AWS specific # permissions that the node might need should be defined here. # EnvInstanceRole: Type: AWS::IAM::Role Properties: AssumeRolePolicyDocument: Version: '2012-10-17' Statement: - Effect: Allow Principal: Service:

• Reference resources created within the template or passed in via parameters

• Order doesn't matter. CloudFormation builds its own dependency graph

Page 39: Automate All AWS Things.key

Deploying a Template

Page 40: Automate All AWS Things.key

Deploying a Template

Page 41: Automate All AWS Things.key

Deploying a Template

Page 42: Automate All AWS Things.key

Deploying a Template

Page 43: Automate All AWS Things.key

Deploying a Template

Page 44: Automate All AWS Things.key

Deploying a Template

Page 45: Automate All AWS Things.key

Deploying a Template

Page 46: Automate All AWS Things.key

Deploying a Template

Page 47: Automate All AWS Things.key

Deploying a Template

Page 48: Automate All AWS Things.key

Deploying a Template

Page 49: Automate All AWS Things.key

Un-Deploying a Template

Page 50: Automate All AWS Things.key

Un-Deploying a Template

Page 51: Automate All AWS Things.key

Un-Deploying a Template

Page 52: Automate All AWS Things.key

Un-Deploying a Template

Page 53: Automate All AWS Things.key

Un-Deploying a Template

Page 54: Automate All AWS Things.key

Un-Deploying a Template

Security Group

Page 55: Automate All AWS Things.key

Command Line Deployments

• Filling out complex CloudFormation forms is still tedious

• Can create CloudFormation deployments via the aws-cli tools

• Parameters are fed in via a JSON parameters file

Page 56: Automate All AWS Things.key

Command Line Deployments[ { "ParameterKey": "HostName", "ParameterValue": "fischerm-ec2-demo" }, { "ParameterKey": "KeyName", "ParameterValue": "FischermUAPilots" }, { "ParameterKey": "VPCID", "ParameterValue": "vpc-12a98977" }, { "ParameterKey": "AZChoice", "ParameterValue": "Zone-A" }, { "ParameterKey": "InstanceType", "ParameterValue": "t2.micro" }, { "ParameterKey": "OSType", "ParameterValue": "Amazon-Linux" } ]

--- # EC2 Basic CloudFormation Deployment # ----------------------------------------- # # This CloudFormation template will deploy a single EC2 instance with # its own security group.

AWSTemplateFormatVersion: "2010-09-09"

# Parameters # ---------- # # These are the input parameters for this template. All of these parameters # must be supplied for this template to be deployed. Parameters: # HostName to be used in tagging the EC2 instance. HostName: Type: String Description: "Enter the name of the host or service, ie 'Civil Engineering Structures App', or 'UITS Cloud Services Testing', etc."

# SSH Key Pair to be used on the application EC2 instances for emergency administrative access. KeyName: Description: "Amazon EC2 Key Pair" Type: "AWS::EC2::KeyPair::KeyName" # VPCID is the ID of the VPC where this template will be deployed. VPCID: Description: "Target VPC" Type: "AWS::EC2::VPC::Id" AllowedValues: - "vpc-12a98977"

# Pick Zone-A or Zone-B where this EC2 instance will be deployed. AZChoice: Description: "Availability Zone" Type: String AllowedValues: - "Zone-A" - "Zone-B"

# Default EC2 Instance Type for Application instances. InstanceType: Description: "EC2 Instance Type" Type: String Default: "t2.micro" AllowedValues:

Page 57: Automate All AWS Things.key

Command Line Deployments

Page 58: Automate All AWS Things.key

Command Line Deployments

Page 59: Automate All AWS Things.key

Configuration As Code

• CloudFormation allows you to codify your infrastructure deployments

• Each template deployment will be identical to previous ones

• Plain text files can be versioned and stored in source control

Page 60: Automate All AWS Things.key

Configuration As Code

Page 61: Automate All AWS Things.key

Configuration As Code

Page 62: Automate All AWS Things.key

UA CloudFormation Catalog

https://bitbucket.org/ua-ecs/service-catalog

Page 63: Automate All AWS Things.key

Docker

• Identify, codify, and encapsulate application dependencies

Page 64: Automate All AWS Things.key

Configuring new App Server

• Following notes from the last time

• Hopefully I wrote everything down…

Page 65: Automate All AWS Things.key

FROM php:5.6-apache

# Add application dependencies. RUN apt-get update && apt-get install -y \ freetds-common \ freetds-bin \ freetds-dev \ libapache2-mod-auth-cas \ libcurl4-openssl-dev \ libldap-2.4-2 \ libldap2-dev \ libxml2 \ libxml2-dev \ unixodbc \ vim

# Install Well Behaved Extensions RUN docker-php-ext-install \ bcmath \ curl \ json \ ldap \ mbstring \ mssql \ opcache \ pdo_mysql \ soap

# Copy over our application COPY app/ /var/www/html/

# Run our custom startup script CMD ["startup.sh"]

docker git repository

built docker image

Page 66: Automate All AWS Things.key

Run Image Anywhere That Supports Docker

• Only need to install Docker on a host, no other dependencies

• Lots of Docker enabled environments

• AWS ECR & Elastic Beanstalk

• Azure

• Linode / Digital Ocean / etc.

docker run -d --name yourapp \ -p 80:80 -h yourapp.example.com \ -e "PHP_db_user=ausername" \ -e "PHP_db_pass=secret" \ yourproj/dockerimage

Page 67: Automate All AWS Things.key

JenkinsDevOps Glue

Page 68: Automate All AWS Things.key

Jenkins

• Really Fancy cron

• Configure jobs to run on-demand or scheduled

• Control access to jobs by user

• Store secrets encrypted & pass into jobs as they're run

Page 69: Automate All AWS Things.key

Jenkins

• Lots of built-in functionality

• Check out a git repository

• Build a Java Project

• Run shell scripts

• Integrations with services such as Slack, email, SMS, etc

• Chain jobs together on success or failure

Page 70: Automate All AWS Things.key

docker projectapp source

Nexus

chef cookbooks

cookbooks.tar.gz

template libraryCloudFormation

TemplateCloudFormation

StackOpsWorks

Stack (Environment)Instance

Deployment ApplicationInstances

Jenkins

Secrets

Jenkins

Jenkins

Jenkins docker repository

Page 71: Automate All AWS Things.key

docker projectapp source

Nexus

chef cookbooks

cookbooks.tar.gz

template libraryCloudFormation

TemplateCloudFormation

StackOpsWorks

Stack (Environment)Instance

Deployment ApplicationInstances

Jenkins

Secrets

Jenkins

Jenkins

Jenkins docker repository

Page 72: Automate All AWS Things.key

docker projectapp source

Nexus

chef cookbooks

cookbooks.tar.gz

template libraryCloudFormation

TemplateCloudFormation

StackOpsWorks

Stack (Environment)Instance

Deployment ApplicationInstances

Jenkins

Secrets

Jenkins

Jenkins

Jenkins docker repository

Page 73: Automate All AWS Things.key

docker projectapp source

Nexus

chef cookbooks

cookbooks.tar.gz

template libraryCloudFormation

TemplateCloudFormation

StackOpsWorks

Stack (Environment)Instance

Deployment ApplicationInstances

Jenkins

Secrets

Jenkins

Jenkins

Jenkins docker repository

Page 74: Automate All AWS Things.key

docker projectapp source

Nexus

chef cookbooks

cookbooks.tar.gz

template libraryCloudFormation

TemplateCloudFormation

StackOpsWorks

Stack (Environment)Instance

Deployment ApplicationInstances

Jenkins

Secrets

Jenkins

Jenkins

Jenkins docker repository

Page 75: Automate All AWS Things.key

Define Multiple Jobs

Page 76: Automate All AWS Things.key

Configuring Jobs

Page 77: Automate All AWS Things.key

Checkout git Repo

Page 78: Automate All AWS Things.key

Reference Secrets

Page 79: Automate All AWS Things.key

Build shell Script

Page 80: Automate All AWS Things.key
Page 81: Automate All AWS Things.key

Jenkins

• Restrict access to Jobs

• Certain people can create jobs

• Certain people can run jobs

• Certain people manage secrets

• Allows you to abstract AWS deployment capabilities

• A single AWS IAM User Credential can be used for multiple Jenkins Jobs

• IAM Credentials never leave Jenkins, stay encrypted

Page 82: Automate All AWS Things.key

Jenkins

• Examples

• App developers can provision & de-provision new environments

• Business Analysts can perform database refreshes (Load new Prod data to Dev for example)

• DevOps staff can manage Jenkins jobs without needing to setup AWS IAM Credentials for Job runners

Page 83: Automate All AWS Things.key

Sticking Points

• Automation takes more time up front to get right

• IAM Permissions

• Persistant File Storage

• Try and use RDS / S3 as much as possible

• EFS makes this slightly easier (AWS managed NFS service)

Page 84: Automate All AWS Things.key

Thank Youhttps://arizona.box.com/v/automate-things

fin