ci/cd: from zero to full build, test and deployment automation - a complete step-by-step...

61
A complete step-by-step walk-through implementing CI/CD for a microservice with Jenkins, CloudFormation and Lambda

Upload: aoe

Post on 08-Jan-2017

936 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

A complete step-by-step walk-through implementing

CI/CD for a microservice with Jenkins, CloudFormation

and Lambda

Page 2: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 3: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

& aoepeople!

Page 4: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

E-Commerce: Magento

CMS: TYPO3

Portals: ZF, Symfony,…

Mobile Searchperience: ElasticSearch

250+ people world-wide

(in 8 locations)

Global Enterprise Projects

Infrastructure: AWS

Page 5: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 6: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

CloudFormation Lambda Jenkins

Pipeline plugin?

Page 7: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

everything sounds better with “continuous”

Page 8: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

is a software development practice

where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.”

- Martin Fowler, ThoughtWorks Chief Scientist

is the natural extension of Continuous

Integration: an approach in which teams ensure that every change to the system is releasable, and that we can release any version at the push of a button. Continuous Delivery aims to make releases boring, so we can deliver frequently and get fast feedback on what users care about.” https://www.thoughtworks.com/continuous-delivery

Page 9: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

is a software development practice

where members of a team integrate their work frequently, usually each person integrates at least daily - leading to multiple integrations per day. Each integration is verified by an automated build (including test) to detect integration errors as quickly as possible. Many teams find that this approach leads to significantly reduced integration problems and allows a team to develop cohesive software more rapidly.”

- Martin Fowler, ThoughtWorks Chief Scientist

is the natural extension of Continuous

Integration: an approach in which teams ensure that every change to the system is releasable, and that we can release any version at the push of a button. Continuous Delivery aims to make releases boring, so we can deliver frequently and get fast feedback on what users care about.” https://www.thoughtworks.com/continuous-delivery

Page 10: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Prod

Page 11: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Prod type “YESIKNOWWHATIMDOING” + click

Page 12: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Build Stage Prod

Page 13: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Deploy to Stage

Deploy to Prod

Build Test

Test

Commit

Trying to get as far as possible to the right

✘ ✘

Page 14: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Test Deploy to Stage

Test Deploy to Prod

Build

… …

Commit

( )*

Page 15: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 16: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Selenium

Page 17: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

(Half-baked)

AMI

Build

Page 18: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Test Deploy to Stage

Test Deploy to Prod

Build

… …

Commit

Page 19: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Static Code

Analysis Build

Unit Tests

Deploy to “tst”

Integration Tests

Performance Tests

Infrastructure Tests

Selenium Tests

Deploy to “prod”

Page 20: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Pet Cattle

Page 21: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 22: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

not disposable disposable

Page 23: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 24: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 25: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

nano

Page 26: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 27: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 28: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

GET / Get current counter value

PUT / Increase counter and return increased value

DELETE / Reset counter to 0

Page 29: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

(yes, that would be a perfect use-case

for Lambda + API Gateway

+ ElastiCache/DynamoDB…)

Load

Balancer S3 Bucket

www.example.com

Auto Scaling group

MySQL

Database

api.example.com

static html

Page 30: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Load

Balancer

Auto Scaling group

S3 Bucket

www.example.com

MySQL

Database

api.example.com

disposable/ immutable

Page 31: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Load

Balancer

Auto Scaling group

S3 Bucket

www.example.com

MySQL

Database

api.example.com

Auto Scaling group

1. Count Instances

2. Launch new ASG and wait until all

instances are healthy

Page 32: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Load

Balancer

Auto Scaling group

S3 Bucket

www.example.com

MySQL

Database

api.example.com

Auto Scaling group

3. Attach to ELB and wait until all

instances are “In Service”

4. Detach old ASG from ELB

Page 33: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Load

Balancer

Auto Scaling group

S3 Bucket

www.example.com

MySQL

Database

api.example.com

Auto Scaling group

5. Delete old ASG (by deleting it’s CFN stack)

Page 34: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

… … …

Backup

Restore

deploy

trigger job

Page 35: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Internet

gateway

NAT

Gateway

Route

Table

pu

blic

su

bn

ets

private

su

bn

ets

Page 36: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Bastion

Webserver

Database

ELB

0.0.0.0/0:80+443 <my_ip>:22

<elb_sg>:80 <bastion_sg>:22

<websrv_sg>:3306

public

su

bn

ets

private

su

bn

ets

Page 37: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

CloudFormation Lambda StackFormation

Page 38: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

pre-process

Userdata

Lambda JS “Template”

superset of CFN template (JSON)

Parameters lookup from other stacks, env vars,…

Template

Parameter

Values

create/ update

Stack policies, Tags,…

Page 39: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Stack magento-stage-build-5

Stack magento-stage-build-6

Stack magento-prod-build-6

Stack magento-prod-build-5

CloudFormation Template

merge & pre-process

“CloudFormation+X” Template(s)

+ Dynamic Parameters

+ Stack Policies

+ Behavior

+ Tags …

Blueprint magento-{env:ENVIRONMENT}-build-{env:BUILD}

Blueprint magento-{env:ENVIRONMENT}-setup

Stack magento-stage-setup

Stack magento-prod-setup

Page 40: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

blueprints: - stackname: 'magento-{env:BUILD}' template: 'magento.template' stackPolicy: 'policy.json' OnFailure: 'DO_NOTHING' parameters: Build: '{env:BUILD}' KeyPair: '{var:KeyPair}' VPC: '{resource:setupstack:VPC}' Subnet: '{resource:setupstack:Subnet}' InstanceSg: '{resource:setupstack:InstanceSg}' InstanceProfile: '{output:setupstack:InstanceProfile}' BootAmi: 'ami-06116566' tags: Environment: 'prod' Build: '{env:BUILD}'

enforce “immutability” by denying updates!

Page 41: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

"UserData": { "Fn::Base64": { "Fn::Join" : [ "", [ "#!/usr/bin/env bash\n", "yum -y update\n", "echo \"Hello World\"\n" ]]}}

"UserData": { "Fn::Base64": { "Fn::FileContent": “userdata.sh” }}

Page 42: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

"LoadBalancerName": { "Fn::Join" : [ "-", [ "elb", { "Ref": "EnvironmentName" }, "magento"] ]}

"LoadBalancerName": “elb-{Ref: EnvironmentName}-magento”

Page 43: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

"SecurityGroupIngress": [{ "IpProtocol": "tcp", "FromPort": "80", "ToPort": "80", "CidrIp": “1.2.3.4/32" }]

"SecurityGroupIngress": [{ "IpProtocol": "tcp", "FromPort": "80", "ToPort": "80", "CidrIp": "1.2.3.4/32" /* Fabrizio’s home office IP */ }]

Page 44: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

> stackformation ec2:ssh -t Environment:prod –c Type –c Build

filter by tag

Please select an instance [0] i-1033ed9b (Type: Frontend; Environment: prod; Build: 477) [1] i-4ff36ec8 (Type: Backend; Environment: prod ; Build: 477) [2] i-5ab4322b (Type: Worker; Environment: prod; Build: 477) [3] i-705ad42f (Type: Worker; Environment: prod; Build: 476) >

• will take jump hosts into account (ProxyCommand)

• auto-detects your local

(encrypted) private keys

• multiplexed ssh connections

• run commands directly

Page 45: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 46: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Build X

Build

Build X+1

Environment A (e.g. prod) Environment B (e.g. stage)

VPC

Static Resources

Build Build

Build X Build X+1

S3 Buckets Artifacts, Jenkins Backup, Static Website

Manual Setup ACM, HostedZone, KeyPair

IAM Setup Instance Roles,…

VPC VPC, Subnets, Bastion,…

Static Resources ELB, DB, Security Groups, DNS

Build ASG, Alarms, Scale Policies,…

manually

from devbox (via Stack- Formation)

via Jenkins Deployment Pipeline (via StackFormation)

these are the immutable ones!

Page 47: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

- Instance Counter - Password Generator - GreenBlue Switcher - Stack Deleter

Page 48: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

"CountInstances": { "Type": "Custom::InstanceCounter", "Properties": { "ServiceToken": {"Ref": "InstanceCounter"}, "AutoScalingGroupTags": [ {"Key": "Environment", "Value": "prod"}, {"Key": "Type", "Value": "Frontend"} ], "Min": 1, "Max": 10, "Factor": "1.5" } },

"FrontendAsg": { "Type": "AWS::AutoScaling::AutoScalingGroup", "Properties": { [...] "DesiredCapacity": {"Fn::GetAtt": ["CountInstances", "Count"]}, "Tags": [ {"Key": "Environment", "Value": "prod", "PropagateAtLaunch": true}, {"Key": "Type", "Value": “Frontend", "PropagateAtLaunch": true} ] } },

Page 49: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 50: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 51: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 52: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 53: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

env-{env:Environment}-setup

vpc

vpc-subnets

vpc-bastion

env-{env:Environment}-website

jenkins cfn-lambdahelper

s3-jenkinsbackup

s3-artifacts

iam

env-{env:Environment}-deploy{env:DEPLOY_ID}

Page 54: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

env-{env:Environment}-setup

vpc

vpc-subnets

vpc-bastion

env-{env:Environment}-website

jenkins cfn-lambdahelper

s3-jenkinsbackup

s3-artifacts

iam

env-{env:Environment}-deploy{env:DEPLOY_ID}

api-tst.aoeplay.net

www-tst.aoeplay.net

Instance Profile

backup/restore

upload artifacts

download artifacts

Password Generator

StackDeleter InstanceCounter GreenBlueSwicher

store functions

Page 55: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 56: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 57: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 58: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 59: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Page 60: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda
Page 61: CI/CD: From Zero To Full Build, Test and Deployment Automation - A complete step-by-step walk-through implementing CI/CD for a Microservice with Jenkins, CloudFormation and Lambda

Follow me on twitter!

My blog