infraestrutura como código
TRANSCRIPT
© 2016, Amazon Web Services, Inc. or its Affiliates. All rights reserved.
Glauber Gallego – Partner Solutions Architect
02/06/2016
Infraestrutura como código
O que levar desta sessão?
Como codificar
infraestrutura?É possível
codificar TUDO!
É aplicável a todo
tipo de empresa.
Construa e Opere Infraestrutura como código
Aplicação
Código Fonte
Interpretador
Estado desejado da aplicação
Infraestrutura
JSON
AWS Service API
Estado desejado da infraestrutura
Construa e Opere Infraestrutura como código
Aplicação
Código Fonte
Interpretador
Estado desejado da aplicação
Infraestrutura
JSON
AWS Service API
Estado desejado da infraestrutura
Construa e Opere Infraestrutura como código
Aplicação
Código Fonte
Interpretador
Estado desejado da aplicação
Infraestrutura
JSON
AWS Service API
Estado desejado da infraestrutura
Construa e Opere Infraestrutura como código
Aplicação
Código Fonte
Interpretador
Estado desejado da aplicação
Infraestrutura
JSON
AWS Service API
Estado desejado da infraestrutura
Infraestrutura como código:
Técnicas, práticas e ferramentas vindas do
desenvolvimento de software e aplicadas a
criação de recursos de infraestrutura
reutilizável, manutenível, extensível e
testável.
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
Source Code
Ops
CF Template
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
Source Code
Ops
CF Template
Code
Pipeline
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
Source Code
Ops
CF Template
Code
Pipeline
CloudFormation
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
Source Code
Ops
CF Template
Beta
Prod
Code
Pipeline
CloudFormation
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
Source Code
Ops
CF Template
Jenkins
Beta
Prod
Code
Pipeline
CloudFormation
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
CodeDeploy
Source Code
Ops
CF Template
Jenkins
Beta
Prod
Code
Pipeline
CloudFormation
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
CodeDeploy
Source Code
Ops
CF Template
Jenkins
Beta
Prod
Code
Pipeline
CloudFormation
Jenkins
Case: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
CodeDeploy
Source Code
Ops
CF Template
Jenkins
Beta
Prod
Code
Pipeline
CloudFormation
Jenkins
// Implement custom logic here
Look up AMI ID
Cross-stack: VPC ID, SG ID
Reverse an IP address
Case: Infraestrutura extensível (Lambda Custom Resources)
IaC Template
Case: Infraestrutura extensível (Lambda Custom Resources)
// Implement custom logic here
Look up AMI ID
Cross-stack: VPC ID, SG ID
Reverse an IP address
IaC Template
Case: Infraestrutura testável (Blue/Green Deployments)
AWS CodeDeployExampleApp V1
ExampleApp V2
IaC Template
Case: Infraestrutura testável (Blue/Green Deployments)
AWS CodeDeployExampleApp V1
ExampleApp V2
IaC Template
CloudFormation: conceitos e tecnologia
Arquivo JSON
Definição de parâmetros
Criação de recursos
Configuração de ações
Framework
Criação de Stack
Atualização de Stack
Detecção de erros & rollback
Recursos AWS configurados
Visibilidade dos recursos criados
Eventos auto chamáveis
Customizável
Template CloudFormation Stack
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template EC2InstanceSample: **WARNING** This template an Amazon EC2 instances. You will be billed for the AWS resources used if
you create a stack from this template.",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type" : "String"
},
"Environment": {
"Type" : "String",
"Default" : ”Dev",
"AllowedValues" : [”Mgmt", "Dev", ”Staging", "Prod"],
"Description" : "Environment that the instances will run in.”
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "AMI" : "ami-7f418316" },
"us-west-2" : { "AMI" : "ami-16fd7026" }
}
},
"Conditions" : {
”EnableEBSOptimized" : {"Fn::Equals" : [{"Ref" : " Environment "}, ”Prod"]},
},
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"EbsOptimized " : {"Fn::If": [ " EnableEBSOptimized ", {“true”}, {“false”}]},
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
"UserData" : { "Fn::Base64" : "80" }
}
}
},
"Outputs" : {
"InstanceId" : {
"Description" : "InstanceId of the newly created EC2 instance",
"Value" : { "Ref" : "Ec2Instance" }
},
"PublicDNS" : {
"Description" : "Public DNSName of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "PublicDnsName" ] }
}
}
}
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template EC2InstanceSample: **WARNING** This template an Amazon EC2 instances. You will be billed for the AWS resources used if
you create a stack from this template.",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type" : "String"
},
"Environment": {
"Type" : "String",
"Default" : ”Dev",
"AllowedValues" : [”Mgmt", "Dev", ”Staging", "Prod"],
"Description" : "Environment that the instances will run in.”
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "AMI" : "ami-7f418316" },
"us-west-2" : { "AMI" : "ami-16fd7026" }
}
},
"Conditions" : {
”EnableEBSOptimized" : {"Fn::Equals" : [{"Ref" : " Environment "}, ”Prod"]},
},
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"EbsOptimized " : {"Fn::If": [ " EnableEBSOptimized ", {“true”}, {“false”}]},
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
"UserData" : { "Fn::Base64" : "80" }
}
}
},
"Outputs" : {
"InstanceId" : {
"Description" : "InstanceId of the newly created EC2 instance",
"Value" : { "Ref" : "Ec2Instance" }
},
"PublicDNS" : {
"Description" : "Public DNSName of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "PublicDnsName" ] }
}
}
}
HEADERS
PARAMETERS
MAPPINGS
RESOURCES
OUTPUTS
CONDITIONALS
{
"AWSTemplateFormatVersion" : "2010-09-09",
"Description" : "AWS CloudFormation Sample Template EC2InstanceSample: **WARNING** This template an Amazon EC2 instances. You will be billed for the AWS resources used if
you create a stack from this template.",
"Parameters" : {
"KeyName" : {
"Description" : "Name of an existing EC2 KeyPair to enable SSH access to the instance",
"Type" : "String"
},
"Environment": {
"Type" : "String",
"Default" : ”Dev",
"AllowedValues" : [”Mgmt", "Dev", ”Staging", "Prod"],
"Description" : "Environment that the instances will run in.”
}
},
"Mappings" : {
"RegionMap" : {
"us-east-1" : { "AMI" : "ami-7f418316" },
"us-west-2" : { "AMI" : "ami-16fd7026" }
}
},
"Conditions" : {
”EnableEBSOptimized" : {"Fn::Equals" : [{"Ref" : " Environment "}, ”Prod"]},
},
"Resources" : {
"Ec2Instance" : {
"Type" : "AWS::EC2::Instance",
"Properties" : {
"KeyName" : { "Ref" : "KeyName" },
"EbsOptimized " : {"Fn::If": [ " EnableEBSOptimized ", {“true”}, {“false”}]},
"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},
"UserData" : { "Fn::Base64" : "80" }
}
}
},
"Outputs" : {
"InstanceId" : {
"Description" : "InstanceId of the newly created EC2 instance",
"Value" : { "Ref" : "Ec2Instance" }
},
"PublicDNS" : {
"Description" : "Public DNSName of the newly created EC2 instance",
"Value" : { "Fn::GetAtt" : [ "Ec2Instance", "PublicDnsName" ] }
}
}
}
HEADERS
PARAMETERS
MAPPINGS
RESOURCES
OUTPUTS
CONDITIONALS
Descrição do que a stack faz, o que ela contem, etc.
Valores de entrada para flexibilidade e customização
do template.
Instruções condicionais pré-definidas.
Condicionais definidas pela avaliação de referencias.
Definição de recursos AWS.
Valores de saída com os resultados da criação da
stack.
Parameters: "myInstanceType" : {
"Type" : "String",
"Default" : "t2.large",
"AllowedValues" : ["t2.micro", "t2.small", "t2.medium", "t2.large"],
"Description" : "Instance type for instances created, must be in the t2 family."
}
Mappings: "AWSInstanceType2Virt": {
"t2.micro": {"Virt": "HVM"},
"t2.small": {"Virt": "HVM"},
"t2.medium": {"Virt": "HVM"},
"t2.large": {"Virt": "HVM"},
}
Mappings: "AWSRegionVirt2AMI": {
"us-east-1": {
"PVM": "ami-50842d38",
"HVM": "ami-08842d60"
},
"us-west-2": {
"PVM": "ami-af86c69f",
"HVM": "ami-8786c6b7"
},
"us-west-1": {
"PVM": "ami-c7a8a182",
"HVM": "ami-cfa8a18a"
}
}
Exemplo de um Template (trecho)
Opção 1: EC2 UserData, disponível como propriedade de AWS::EC2::Instance
"Resources" : {"Ec2Instance" : {"Type" : "AWS::EC2::Instance","Properties" : {
"KeyName" : { "Ref" : "KeyName" },"SecurityGroups" : [ { "Ref" : "InstanceSecurityGroup" } ],"ImageId" : { "Fn::FindInMap" : [ "RegionMap", { "Ref" : "AWS::Region" }, "AMI" ]},"UserData" : { "Fn::Base64" : { "Fn::Join" : ["",[
"#!/bin/bash -ex","\n","yum -y install gcc-c++ make","\n","yum -y install mysql-devel sqlite-devel","\n","yum -y install ruby-rdoc rubygems ruby-mysql ruby-devel","\n","gem install --no-ri --no-rdoc rails","\n","gem install --no-ri --no-rdoc mysql","\n","gem install --no-ri --no-rdoc sqlite3","\n","rails new myapp","\n","cd myapp","\n","rails server -d","\n"]]}}
}}
Bootstrap de Aplicações e Atualizações
“WebAppHost" : {
"Type" : "AWS::EC2::Instance",
"Metadata" : {
"AWS:CloudFormation::Init" : {
"config" : {
"packages" : {
"yum" : {
”httpd" : [],
”php" : [],
"wordpress" : []
}
}
Bootstrap de Aplicações e AtualizaçõesOpção 2: AWS-CloudFormation-Init, meta-dado para instalação de pacotes, arquivos
e execução de comandos com ferramental nativo.
Demo: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
CodeDeploy
Source Code
Ops
CF Template
Jenkins
Beta
Prod
Code
Pipeline
CloudFormation
Jenkins
Demo: Infraestrutura manutenível (commit, test, deploy)
Devs
Code
Commit
CodeDeploy
Source Code
Ops
CF Template
Jenkins
Beta
Prod
Code
Pipeline
CloudFormation
Jenkins
CloudFormation
One Time Generation
Demo: Infraestrutura manutenível (commit, test, deploy)
Devs
CodeDeploy
Source Code
Ops
CF Template
Jenkins
Beta
Prod
Code
Pipeline
CloudFormation
Jenkins
CloudFormation
One Time Generation
GitHub
http://github.com/ggallego/
demo-summitsp-infraascode
Demo: Infraestrutura manutenível (commit, test, deploy)
Quick Learning Resources
AWS QuickLabLaunching & Managing an Application with CloudFormation
https://run.qwiklab.com/store_location?previous_urls=%2Ffocuses%2F1379%3Fsearch%3D31203
AWS CloudFormation Docs & Tutorialshttps://aws.amazon.com/documentation/cloudformation/
https://aws.amazon.com/cloudformation/aws-cloudformation-templates/
https://aws.amazon.com/cloudformation/aws-cloudformation-articles-and-tutorials/
Quick Start Deployments
https://aws.amazon.com/quickstart/