Continuous Integration

Integrating or mergin the code changes frequently - at least once per day. (CodeCommit / GitHub)

 

Continuous Delivery

Automating the build, test and deployment functions. (CodeBuild and CodeDeploy)

 

Continuous Deployment

Fully automated release process, code is deployed into Staging or Production as soon as it has successfully passed through the release pipeline. (CodePipeline)

[AWS Developer Guru] CI/CD_github

[AWS Developer Guru] CI/CD_github_02

Resource: https://d1.awsstatic.com/whitepapers/DevOps/practicing-continuous-integration-continuous-delivery-on-AWS.pdf

Lab: CodeCommit From CLI 

 

CodeDeploy

Work with EC2 instances, on-premises & Lambda

  • Quickly release new features
  • Avoid downtime during deployments
  • Avoid the risks associated with manual processes.

Deployment Approaches

1. In-Place

The application is stopped on each instance and the new release is installed. Also known as Rolling update.

[AWS Developer Guru] CI/CD_elastic_03

For example, a Load balancer has three instances. When doing the In-placed deployment. 

  • The application is stopped on the first instance.
  • The instance will be out of service during the deployment so capacity is reduced.
  • You should configure your Elastic Load Balancer to stop sending requests to the instance.

Rollback

No easy way to rollback, you need to deploy previous version all over again. Cause a big downtime.

When to use?

Great when deploying the first time. Because there is no second version during the first time.

2. Blue / Green

New instances are provisioned and new release is installed on the new instances. Blue represents the active deployment, green is the new release.

[AWS Developer Guru] CI/CD_json_04

  • Blue represents the current version of our application.
  • CodeDeploy provisions new instances in green.
  • The new Revision is deployed to the Green environment.
  • The Green instances are registered with the Elastic Load Balancer
  • Traffic is routed away from the old enironment.
  • Blue environment is eventually terminated.

Rollback

It is easy because we just need to switch the Load Balacner to Blue env from Green env again. Only works if you didn't already terminate your old environment!

[AWS Developer Guru] CI/CD_json_05

 

CodeDeploy AppSpec File

[AWS Developer Guru] CI/CD_ide_06

[AWS Developer Guru] CI/CD_ide_07

 

CodeDeploy Lifecycle Event Hooks

Mainly needed for In-Placed deployment

Divide into three main Phases

  1. De-register instances from a Load Balancer
  2. The real nuts & bolts of the application deployemnt
  3. Re-register instances with the Load Balancer

[AWS Developer Guru] CI/CD_elastic_03  [AWS Developer Guru] CI/CD_github_09

It makes senses, you need to first stop traffic go to V1.0 from load balacner; then install Revision; Last register load balancer to V2.

[AWS Developer Guru] CI/CD_sed_10

 

Phase1

What it does is mainly Block Traffic.

[AWS Developer Guru] CI/CD_github_11

 

Phase2

What it does is stop previous running application, download new version, install new version, running new application and Validate/testing.

[AWS Developer Guru] CI/CD_ide_12

Phase3

What it does is connect load balancer

[AWS Developer Guru] CI/CD_sed_13

 

[AWS Developer Guru] CI/CD_elastic_14

 

CodePipeline

[AWS Developer Guru] CI/CD_sed_15

  • Integrates with AWS & Third-Party Tools

ECS 

[AWS Developer Guru] CI/CD_github_16

[AWS Developer Guru] CI/CD_elastic_17

Elastic Beanstalk

[AWS Developer Guru] CI/CD_json_18

 

CloudFormation

Infrastructure As Code

CloudFormation allows you to manage, configure, and provision AWS infrastructue as YAML or JSON code.

 

Basic template

Template snippets

  • The Resource Section is the only Mandatory of CloudFormation template.
  • The Transform section is used to reference additional code stored in S3, allowing for code re-use. E.g Lambda code or template nippets / reusable pieces of CloudFormation code.

[AWS Developer Guru] CI/CD_json_19

  • The Parameters section is used to Input custom values
  • The Conditions section is used for provision resources based on environment.
  • The Mappings section allows you to create custom mappings like Region: AMI

[AWS Developer Guru] CI/CD_json_20

[AWS Developer Guru] CI/CD_github_21

 

Exampe YML template:

AWSTemplateFormatVersion: 2010-09-09

Description: Template to create an EC2 instance and enable SSH

Parameters: 
  KeyName:
    Description: Name of SSH KeyPair
    Type: 'AWS::EC2::KeyPair::KeyName'
    ConstraintDescription: Provide the name of an existing SSH key pair

Resources:
  MyEC2Instance:
    Type: 'AWS::EC2::Instance'
    Properties:
      InstanceType: t2.micro
      ImageId: ami-0bdb1d6c15a40392c
      KeyName: !Ref KeyName
      SecurityGroups:
       - Ref: InstanceSecurityGroup
      Tags:
        - Key: Name
          Value: My CF Instance
  InstanceSecurityGroup:
    Type: 'AWS::EC2::SecurityGroup'
    Properties:
      GroupDescription: Enable SSH access via port 22
      SecurityGroupIngress:
        IpProtocol: tcp
        FromPort: 22
        ToPort: 22
        CidrIp: 0.0.0.0/0

Outputs: 
  InstanceID:
    Description: The Instance ID
    Value: !Ref MyEC2Instance

[AWS Developer Guru] CI/CD_sed_22

You can find `ImageId` when you create a new EC2 instances and remember to choose the correct region.

 

Serverless Application Model

Define and provision serverless applications using CloudFormation

SAM CLI

sam package: Packages your application and uploads to S3

sam deploy: Deploys your serverless app using CloudFormation.

[AWS Developer Guru] CI/CD_elastic_23

 

Nested CloudFormation Stacks

Enable re-use of CloudFormation code for common use cases: For example, you have a load balancer which used again and again.

Instead of copying out the code each time, create a standard template for each commn use case and reference from within your CloudFormation tempalte.

[AWS Developer Guru] CI/CD_github_24

[AWS Developer Guru] CI/CD_ide_25

 

Lab: CloudFormation Nested Stack

 

 

[AWS Developer Guru] CI/CD_github_26

[AWS Developer Guru] CI/CD_sed_27

[AWS Developer Guru] CI/CD_json_28

[AWS Developer Guru] CI/CD_json_29

[AWS Developer Guru] CI/CD_github_30

 


 

 

[AWS Developer Guru] CI/CD_sed_31

Transform: Reuse code and reference Lambda code snippet.

 

[AWS Developer Guru] CI/CD_elastic_32

For serverless related, Lambda is in Transform section