Tagged: AWS

Continious Deployment Using Bitbucket and AWS code Deploy

Continuous deployment [CD] can be achieved using following simple steps if you are using bitbucket for code and aws for hosting.

Requirement

Pre-requisite

  • there is a repository in bitbucket account, bitbucket pipeline is enabled
  • AWS account has
    • one s3 bucket,
    • one IAM user, having aws codedeploy access, and s3 access
    • AWS code deploy group
    • instance added under that group for deployment

bitbucket pipeline YAML

image: atlassian/default-image:2
pipelines:
  custom:
    project-1:
      - step:
          name: Preparing the package
          script:
            - mkdir <projectname>
            - rsync -a -v src/* <projectname>/
            - cd <projectname>
            - zip -r ../<projectname>.zip *
          artifacts:
            - <projectname>.zip

      - step:
          name: Upload to S3 Bucket
          services:
            - docker
          script:
          
            - pipe: atlassian/aws-code-deploy:0.2.10
              variables:
                AWS_ACCESS_KEY_ID: $S3_ACCESS_KEY
                AWS_SECRET_ACCESS_KEY: $S3_SECRET_KEY
                AWS_DEFAULT_REGION: eu-west-1
                COMMAND: 'upload'
                APPLICATION_NAME: <<project1>>
                ZIP_FILE: '<projectname>.zip'
                S3_BUCKET: $S3_BUCKET

      - step:
          name: Deploy Project 
          deployment: project-1
          services:
          - docker
          script:
          - pipe: atlassian/aws-code-deploy:0.2.10
            variables:
              AWS_ACCESS_KEY_ID: $S3_ACCESS_KEY
              AWS_SECRET_ACCESS_KEY: $S3_SECRET_KEY
              AWS_DEFAULT_REGION: eu-west-1
              COMMAND: 'deploy'
              APPLICATION_NAME: <<project1>>
              DEPLOYMENT_GROUP: <aws deployment group name>
              IGNORE_APPLICATION_STOP_FAILURES: 'true'
              FILE_EXISTS_BEHAVIOR: 'OVERWRITE'
              S3_BUCKET: $S3_BUCKET
              WAIT: 'true'
  • save this content, with a filename as bitbucket-pipelines.yml
  • push this file in bitbucket repo under root of the repo
  • there are a few variables and values need modification $ represent variable, and for these, <> or <<>>, you can replace with your own name.
  • a variable can be defined under Repository settings > pipelines > repository variable, with same name.
Variable Name
  • This is manual triggered, however, you can set up the trigger points, means push, merge etc.

what it will do

  • it will zip the code from the path you have given with command
 mkdir <projectname>
            - rsync -a -v src/* <projectname>/
            - cd <projectname>
            - zip -r ../<projectname>.zip *
  • zip will be moved to s3 before deployment, for backup purpose
 pipe: atlassian/aws-code-deploy:0.2.10
              variables:
                AWS_ACCESS_KEY_ID: $S3_ACCESS_KEY
                AWS_SECRET_ACCESS_KEY: $S3_SECRET_KEY
                AWS_DEFAULT_REGION: eu-west-1
                COMMAND: 'upload'
                APPLICATION_NAME: <<project1>>
                ZIP_FILE: '<projectname>.zip'
                S3_BUCKET: $S3_BUCKET
  • Deploy the zip artifact from S3 to ec2 using AWS code deploy. [requires AWS code deploy group name]
  • application name is the name , you define under aws code deploy .
name: Deploy Project 
          deployment: project-1
          services:
          - docker
          script:
          - pipe: atlassian/aws-code-deploy:0.2.10
            variables:
              AWS_ACCESS_KEY_ID: $S3_ACCESS_KEY
              AWS_SECRET_ACCESS_KEY: $S3_SECRET_KEY
              AWS_DEFAULT_REGION: eu-west-1
              COMMAND: 'deploy'
              APPLICATION_NAME: <<project1>>
              DEPLOYMENT_GROUP: <aws deployment group name>
              IGNORE_APPLICATION_STOP_FAILURES: 'true'
              FILE_EXISTS_BEHAVIOR: 'OVERWRITE'
              S3_BUCKET: $S3_BUCKET
              WAIT: 'true'

More about pipeline : https://support.atlassian.com/bitbucket-cloud/docs/configure-bitbucket-pipelinesyml/

AWS code deploy app spec Hooks

version: 0.0
os: linux

files:
  - source: ./
    destination: </var/www/html/project-1/>

hooks:
  BeforeInstall:
    - location: <deployment_scripts/before.sh>
      runas: root

You can define appspec.yml in your code, that can contain hooks, which will be executed during code deploy
hooks : https://docs.aws.amazon.com/codedeploy/latest/userguide/reference-appspec-file-structure-hooks.html

Setting Up Moodle with AWS cloudfront CDN

before writting further , following is my exisitng architecture , where

  • ALB and ec2 are being utilized for Compute
  • RDS for database
  • EFS for moodledata
  • Redis for cache

Moodle version is 3.8

My Domain is pointing to Loadbalancer and site is being served.

Now , My Objective is to deliver the site utilizing AWS Cloudront CDN.

Setting Up Cloudfront :-

just navigate to aws cloud front service and start creating a distribution. you will find a web form to fill following sections.

  • Origin Setting
  • Cache Setting
  • Distribution Setting

Origin Setting:-

origin setting
  • Origin Domain Name : Select the ALB or ec2 on which the application is setup. It is the only thing which is requried.
    • origin path : Put the path if your site is runing in a directory or you just want to deliver the defined directory content, otherwise leave it blank.

Cache Settings :-

Cache Settings
  • Viewer Protocol policy is main thing to handle and it will depend upon the bhaviour of moodle implementation\
    • whether your site is using ssl
    • if you are enforcing https to user
    • if loadbalancer is utilized to offload ssl

as we force https, so in my case, i have set it up to HTTPS only

Distribution Settings:-

Distribution Settings
  • If site is using https then you need to import certificate through AWS ACM service, then it will be available for selection
  • Cloud front will provide a unique domain name , <d3e5asd3gad9wckaz>.cloudfront.net. If you want to use your own domain, you can put that domian under CNAME. so that, the same will be accessible by your domain as well.
  • you can put logs into s3 bucket, loggin and log prefix is optional
Final Architecutre

So we are done with setting up. It will take few minutes to deploy. Once it is deployed, you can try accesssing with the CNAME, and moodle should be served through cloudfront.

Notes:-

  • here we are delivering the complete site with cloudfront
  • only GET Request are cached, POST and other actions are forwarded directly
  • one can control caching behaviour by various ways
    • header values
    • apache mod header setting and values
    • maximum time to cache
  • one can put pre and post hook to execute , means, you can manipulate request , before it reaches to the web server and response, before it is reached to end user by invoking Lambda.
Pros :-
  • You can utilize best of CDN to deliver the content, it will help to imprvoe performance by caching , more control on each request, end user will get the speed as it content is being delivered from nearest location

Note :- Origin Response Timeout can be maximum set to 60 seconds. Although there should not be any process which takes more than that, ,but heavy process which are processed on demand or report downloads can lead to 504 timeout. The same condition is with ALB, but we can increase that beyond 60 seconds.